1 | <?php |
||
10 | class TypedCollection extends Collection |
||
11 | { |
||
12 | /** |
||
13 | * The class name required for any element of the collection |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $itemType; |
||
18 | |||
19 | /** |
||
20 | * @param array $items the items list |
||
21 | * @param string $itemType the class name for the collection elements |
||
22 | */ |
||
23 | 6 | public function __construct(array $items, $itemType) |
|
30 | |||
31 | /** |
||
32 | * Builder for TypedCollection objects |
||
33 | * |
||
34 | * @param iterable $iterable |
||
35 | * @return TypedCollection |
||
36 | */ |
||
37 | 2 | public static function from($iterable) |
|
58 | |||
59 | |||
60 | /** |
||
61 | * @param $itemType |
||
62 | * @return $this |
||
63 | */ |
||
64 | public function setItemType($itemType) |
||
69 | |||
70 | /** |
||
71 | * @param object $item |
||
72 | */ |
||
73 | 2 | public function add($item) |
|
79 | |||
80 | /** |
||
81 | * @param array $items |
||
82 | * @param string $itemType |
||
83 | * |
||
84 | * @throws \InvalidArgumentException |
||
85 | */ |
||
86 | 6 | protected static function validateItems(array $items, $itemType) |
|
96 | |||
97 | /** |
||
98 | * @param string $type An object class name |
||
99 | * |
||
100 | * @return \Closure A function to validate the data type |
||
101 | * @throws \InvalidArgumentException |
||
102 | */ |
||
103 | protected static function validateItem($type) |
||
114 | } |
||
115 |