1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Basic array implementation of a Collection |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/PHPExif/php-exif-common for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2016 Tom Van Herreweghe <[email protected]> |
7
|
|
|
* @license http://github.com/PHPExif/php-exif-common/blob/master/LICENSE MIT License |
8
|
|
|
* @category PHPExif |
9
|
|
|
* @package Common |
10
|
|
|
* @codeCoverageIgnore |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace PHPExif\Common\Collection; |
14
|
|
|
|
15
|
|
|
use PHPExif\Common\Exception\Collection\ElementAlreadyExistsException; |
16
|
|
|
use PHPExif\Common\Exception\Collection\ElementNotExistsException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ArrayCollection class |
20
|
|
|
* |
21
|
|
|
* @category PHPExif |
22
|
|
|
* @package Common |
23
|
|
|
*/ |
24
|
|
|
class ArrayCollection implements Collection |
25
|
|
|
{ /** |
|
|
|
|
26
|
|
|
* Holds the entries of the collection |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $elements; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Collection constructor |
34
|
|
|
* |
35
|
|
|
* @param array $elements |
36
|
|
|
*/ |
37
|
|
|
public function __construct(array $elements = array()) |
38
|
|
|
{ |
39
|
|
|
$this->elements = array(); |
40
|
|
|
|
41
|
|
|
foreach ($elements as $name => $value) { |
42
|
|
|
$this->set($name, $value); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritDoc |
48
|
|
|
*/ |
49
|
|
|
public function add($value) |
50
|
|
|
{ |
51
|
|
|
$this->elements[] = $value; |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritDoc |
58
|
|
|
*/ |
59
|
|
|
public function set($key, $value) |
60
|
|
|
{ |
61
|
|
|
if ($this->exists($key)) { |
62
|
|
|
throw ElementAlreadyExistsException::withKey($key); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->elements[$key] = $value; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritDoc |
72
|
|
|
*/ |
73
|
|
|
public function exists($key) |
74
|
|
|
{ |
75
|
|
|
return array_key_exists($key, $this->elements); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public function get($key) |
82
|
|
|
{ |
83
|
|
|
if (!$this->exists($key)) { |
84
|
|
|
throw ElementNotExistsException::withKey($key); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->elements[$key]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
|
|
public function count() |
94
|
|
|
{ |
95
|
|
|
return count($this->elements); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|