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
|
|
|
/** |
27
|
|
|
* Holds the entries of the collection |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $elements; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Collection constructor |
35
|
|
|
* |
36
|
|
|
* @param array $elements |
37
|
|
|
*/ |
38
|
|
|
public function __construct(array $elements = array()) |
39
|
|
|
{ |
40
|
|
|
$this->elements = array(); |
41
|
|
|
|
42
|
|
|
foreach ($elements as $name => $value) { |
43
|
|
|
$this->set($name, $value); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritDoc |
49
|
|
|
*/ |
50
|
|
|
public function add($value) |
51
|
|
|
{ |
52
|
|
|
$this->elements[] = $value; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
60
|
|
|
public function set($key, $value) |
61
|
|
|
{ |
62
|
|
|
if ($this->exists($key)) { |
63
|
|
|
throw ElementAlreadyExistsException::withKey($key); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->elements[$key] = $value; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritDoc |
73
|
|
|
*/ |
74
|
|
|
public function exists($key) |
75
|
|
|
{ |
76
|
|
|
return array_key_exists($key, $this->elements); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
*/ |
82
|
|
|
public function get($key) |
83
|
|
|
{ |
84
|
|
|
if (!$this->exists($key)) { |
85
|
|
|
throw ElementNotExistsException::withKey($key); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->elements[$key]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritDoc |
93
|
|
|
*/ |
94
|
|
|
public function count() |
95
|
|
|
{ |
96
|
|
|
return count($this->elements); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|