-
Notifications
You must be signed in to change notification settings - Fork 8
/
StatementBase.php
50 lines (41 loc) · 1.72 KB
/
StatementBase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php namespace Locker\XApi;
use Locker\XApi\Errors\Error as Error;
class StatementBase extends Element {
protected $props = [
'actor' => 'Locker\XApi\Actor',
'verb' => 'Locker\XApi\Verb',
'object' => 'Locker\XApi\StatementObject',
'result' => 'Locker\XApi\Result',
'context' => 'Locker\XApi\Context',
'timestamp' => 'Locker\XApi\Timestamp',
'attachments' => 'Locker\XApi\Attachments'
];
protected $required_props = ['actor', 'verb', 'object'];
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') {
$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());
}
}