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
|
|
|
* An xAPI document's data. |
18
|
|
|
* |
19
|
|
|
* Document data are immutable. This means that they can be accessed like an array. |
20
|
|
|
* But you can only do this to read data. Thus an {@link UnsupportedOperationException} |
21
|
|
|
* is thrown when you try to unset data or to manipulate them. |
22
|
|
|
* |
23
|
|
|
* @author Christian Flothmann <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class DocumentData implements \ArrayAccess |
26
|
|
|
{ |
27
|
|
|
private $data = array(); |
28
|
|
|
|
29
|
|
|
public function __construct(array $data = array()) |
30
|
|
|
{ |
31
|
|
|
$this->data = $data; |
32
|
12 |
|
} |
33
|
|
|
|
34
|
12 |
|
/** |
35
|
12 |
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
|
|
public function offsetExists($offset): bool |
38
|
|
|
{ |
39
|
|
|
return isset($this->data[$offset]); |
40
|
3 |
|
} |
41
|
|
|
|
42
|
3 |
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
|
|
public function offsetGet($offset) |
46
|
|
|
{ |
47
|
|
|
if (!isset($this->data[$offset])) { |
48
|
3 |
|
throw new \InvalidArgumentException(sprintf('No data for name "%s" registered.', $offset)); |
49
|
|
|
} |
50
|
3 |
|
|
51
|
|
|
return $this->data[$offset]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
3 |
|
* |
57
|
|
|
* @throws UnsupportedOperationException Documents are immutable |
58
|
3 |
|
*/ |
59
|
|
|
public function offsetSet($offset, $value): void |
60
|
|
|
{ |
61
|
|
|
throw new UnsupportedOperationException('A document is immutable.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
3 |
|
* |
67
|
|
|
* @throws UnsupportedOperationException Documents are immutable |
68
|
|
|
*/ |
69
|
|
|
public function offsetUnset($offset): void |
70
|
|
|
{ |
71
|
|
|
throw new UnsupportedOperationException('A document is immutable.'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns all data as an array. |
76
|
|
|
*/ |
77
|
|
|
public function getData(): array |
78
|
|
|
{ |
79
|
|
|
return $this->data; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|