Skip to content

Commit

Permalink
Merge pull request #7 from LearningLocker/issue/valid_context
Browse files Browse the repository at this point in the history
Validates context for activity.
  • Loading branch information
ryasmi committed Apr 12, 2016
2 parents cd1b345 + e06189d commit a239144
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/StatementBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit a239144

Please sign in to comment.