-
Notifications
You must be signed in to change notification settings - Fork 8
/
Agent.php
54 lines (43 loc) · 1.51 KB
/
Agent.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
51
52
53
54
<?php namespace Locker\XApi;
use Locker\XApi\Errors\Error as Error;
class Agent extends Element {
use TypedElement;
protected $props = [
'name' => 'Locker\XApi\Str',
'mbox' => 'Locker\XApi\Mailto',
'mbox_sha1sum' => 'Locker\XApi\Sha1',
'openid' => 'Locker\XApi\IRI',
'account' => 'Locker\XApi\Account',
'objectType' => 'Locker\XApi\ObjectType'
];
protected $required_props = ['objectType'];
protected $type = 'Agent';
protected $type_prop = 'objectType';
public function __construct($value = null) {
$this->addDefaults();
parent::__construct($value);
}
private function countIdentifiers() {
$identifiers = ['mbox', 'mbox_sha1sum', 'openid', 'account'];
return count(array_filter($identifiers, function ($identifier) {
return isset($this->value->{$identifier});
}));
}
protected function identifierError($used_identifiers) {
return "A `".get_class($this)."` must have exactly one identifier not `$used_identifiers`";
}
protected function validateIdentifiers($used_identifiers) {
return $used_identifiers === 1;
}
public function validate() {
$errors = $this->validateTypedElement();
// Gets the used identifiers.
$used_identifiers = $this->countIdentifiers();
$hasOneIdent = $this->validateIdentifiers($used_identifiers);
// Checks that only one identifier is used.
if (!$hasOneIdent) {
$errors[] = new Error($this->identifierError($used_identifiers));
}
return array_merge($errors, parent::validate());
}
}