1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the xAPI package. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Flothmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xabbuh\XApi\Model; |
13
|
|
|
|
14
|
|
|
use Xabbuh\XApi\Common\Exception\UnsupportedOperationException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* xAPI statement extensions. |
18
|
|
|
* |
19
|
|
|
* @author Christian Flothmann <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class Extensions implements \ArrayAccess |
22
|
|
|
{ |
23
|
|
|
private $extensions; |
24
|
|
|
|
25
|
|
|
public function __construct(\SplObjectStorage $extensions = null) |
26
|
|
|
{ |
27
|
|
|
$this->extensions = array(); |
28
|
|
|
|
29
|
|
|
if (null !== $extensions) { |
30
|
|
|
foreach ($extensions as $iri) { |
31
|
|
|
if (!$iri instanceof IRI) { |
32
|
|
|
throw new \InvalidArgumentException(sprintf('Expected an IRI instance as key (got %s).', is_object($iri) ? get_class($iri) : gettype($iri))); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->extensions[$iri->getValue()] = $extensions[$iri]; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function offsetExists($offset) |
44
|
|
|
{ |
45
|
|
|
if (!$offset instanceof IRI) { |
46
|
|
|
throw new \InvalidArgumentException(sprintf('Expected an IRI instance as key (got %s).', is_object($offset) ? get_class($offset) : gettype($offset))); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return isset($this->extensions[$offset->getValue()]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function offsetGet($offset) |
56
|
|
|
{ |
57
|
|
|
if (!$offset instanceof IRI) { |
58
|
|
|
throw new \InvalidArgumentException(sprintf('Expected an IRI instance as key (got %s).', is_object($offset) ? get_class($offset) : gettype($offset))); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!isset($this->extensions[$offset->getValue()])) { |
62
|
|
|
throw new \InvalidArgumentException(sprintf('No extension for key "%s" registered.', $offset->getValue())); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->extensions[$offset->getValue()]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function offsetSet($offset, $value) |
72
|
|
|
{ |
73
|
|
|
throw new UnsupportedOperationException('xAPI statement extensions are immutable.'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function offsetUnset($offset) |
80
|
|
|
{ |
81
|
|
|
throw new UnsupportedOperationException('xAPI statement extensions are immutable.'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getExtensions() |
85
|
|
|
{ |
86
|
|
|
$extensions = new \SplObjectStorage(); |
87
|
|
|
|
88
|
|
|
foreach ($this->extensions as $iri => $value) { |
89
|
|
|
$extensions->attach(IRI::fromString($iri), $value); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $extensions; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function equals(Extensions $otherExtensions) |
96
|
|
|
{ |
97
|
|
|
if (count($this->extensions) !== count($otherExtensions->extensions)) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
foreach ($this->extensions as $iri => $value) { |
102
|
|
|
if (!array_key_exists($iri, $otherExtensions->extensions)) { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($this->extensions[$iri] != $otherExtensions->extensions[$iri]) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|