From e06189d9465619ebb7df0c6f89fbca6ed10e62cd Mon Sep 17 00:00:00 2001 From: Ryan Smith <0ryansmith1994@gmail.com> Date: Tue, 18 Aug 2015 17:14:59 +0100 Subject: [PATCH] Validates context for activity. --- src/StatementBase.php | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/StatementBase.php b/src/StatementBase.php index eee5118..6a16f81 100644 --- a/src/StatementBase.php +++ b/src/StatementBase.php @@ -13,19 +13,38 @@ class StatementBase extends Element { ]; protected $required_props = ['actor', 'verb', 'object']; - public function validate() { - $errors = []; + private function validateVoider($errors) { $verb_id = $this->getPropValue('verb.id'); $object_type = $this->getPropValue('object.objectType'); // Validates voider. - if ( - $verb_id === 'http://adlnet.gov/expapi/verbs/voided' && - $object_type !== 'StatementRef' - ) { + if ($verb_id === 'http://adlnet.gov/expapi/verbs/voided' && $object_type !== 'StatementRef') { $errors[] = new Error("`object.objectType` must be `StatementRef` not `$object_type` when voiding"); } + return $errors; + } + + private function validateContextForActivity($errors) { + $object_type = $this->getPropValue('object.objectType'); + $is_activity = $object_type === 'Activity'; + + // Validates context for activity. + if ($this->getPropValue('context.revision') !== null && !$is_activity) { + $errors[] = new Error("`context.revision` must only be used if `object.objectType` is `Activity` not `$object_type`"); + } + if ($this->getPropValue('context.platform') !== null && !$is_activity) { + $errors[] = new Error("`context.platform` must only be used if `object.objectType` is `Activity` not `$object_type`"); + } + + return $errors; + } + + public function validate() { + $errors = []; + $errors = $this->validateVoider($errors); + $errors = $this->validateContextForActivity($errors); + return array_merge($errors, parent::validate()); } }