Skip to content

Commit e06189d

Browse files
committed
Validates context for activity.
1 parent 09f16de commit e06189d

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/StatementBase.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,38 @@ class StatementBase extends Element {
1313
];
1414
protected $required_props = ['actor', 'verb', 'object'];
1515

16-
public function validate() {
17-
$errors = [];
16+
private function validateVoider($errors) {
1817
$verb_id = $this->getPropValue('verb.id');
1918
$object_type = $this->getPropValue('object.objectType');
2019

2120
// Validates voider.
22-
if (
23-
$verb_id === 'http://adlnet.gov/expapi/verbs/voided' &&
24-
$object_type !== 'StatementRef'
25-
) {
21+
if ($verb_id === 'http://adlnet.gov/expapi/verbs/voided' && $object_type !== 'StatementRef') {
2622
$errors[] = new Error("`object.objectType` must be `StatementRef` not `$object_type` when voiding");
2723
}
2824

25+
return $errors;
26+
}
27+
28+
private function validateContextForActivity($errors) {
29+
$object_type = $this->getPropValue('object.objectType');
30+
$is_activity = $object_type === 'Activity';
31+
32+
// Validates context for activity.
33+
if ($this->getPropValue('context.revision') !== null && !$is_activity) {
34+
$errors[] = new Error("`context.revision` must only be used if `object.objectType` is `Activity` not `$object_type`");
35+
}
36+
if ($this->getPropValue('context.platform') !== null && !$is_activity) {
37+
$errors[] = new Error("`context.platform` must only be used if `object.objectType` is `Activity` not `$object_type`");
38+
}
39+
40+
return $errors;
41+
}
42+
43+
public function validate() {
44+
$errors = [];
45+
$errors = $this->validateVoider($errors);
46+
$errors = $this->validateContextForActivity($errors);
47+
2948
return array_merge($errors, parent::validate());
3049
}
3150
}

0 commit comments

Comments
 (0)