Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}