1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tidal/WampWatch package. |
5
|
|
|
* (c) 2016 Timo Michna <timomichna/yahoo.de> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tidal\WampWatch\Model\Property; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use Tidal\WampWatch\Model\Contract\Property\ObjectCollectionInterface; |
16
|
|
|
|
17
|
|
|
class ObjectCollection extends Collection implements ObjectCollectionInterface |
18
|
|
|
{ |
19
|
|
|
private $objectConstrain; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Set a constrain on a class or interface name the collection's items must be an instance of. |
23
|
|
|
* Paramater 1 $cls expects a fully qualified class name. |
24
|
|
|
* |
25
|
|
|
* @param string $cls A Fully qualified class name |
26
|
|
|
*/ |
27
|
|
|
public function setObjectConstrain($cls) |
28
|
|
|
{ |
29
|
|
|
if (!class_exists($cls) && !interface_exists($cls)) { |
30
|
|
|
throw new InvalidArgumentException("Class or Interface '$cls' not found."); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->objectConstrain = $cls; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function append($value) |
37
|
|
|
{ |
38
|
|
|
$this->validateObjectConstrain($value); |
39
|
|
|
|
40
|
|
|
parent::append($value); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function validateObjectConstrain($item) |
44
|
|
|
{ |
45
|
|
|
if (!isset($this->objectConstrain)) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!is_a($item, $this->objectConstrain)) { |
50
|
|
|
throw new InvalidArgumentException("Item must be instance of '{$this->objectConstrain}'"); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function offsetSet($index, $newValue) |
55
|
|
|
{ |
56
|
|
|
$this->validateObjectConstrain($newValue); |
57
|
|
|
|
58
|
|
|
parent::offsetSet($index, $newValue); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|