|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KGzocha\Searcher\Chain\Collection; |
|
4
|
|
|
|
|
5
|
|
|
use KGzocha\Searcher\AbstractCollection; |
|
6
|
|
|
use KGzocha\Searcher\Chain\CellInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Collection of CellInterface objects. It will throw \InvalidArgumentException if tried to use it with less |
|
10
|
|
|
* than MINIMUM_CELLS. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Krzysztof Gzocha <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class CellCollection extends AbstractCollection implements CellCollectionInterface |
|
15
|
|
|
{ |
|
16
|
|
|
const MINIMUM_CELLS = 2; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @inheritDoc |
|
20
|
|
|
*/ |
|
21
|
13 |
|
protected function isItemValid($item) |
|
22
|
|
|
{ |
|
23
|
13 |
|
return $item instanceof CellInterface; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return \ArrayIterator |
|
28
|
|
|
*/ |
|
29
|
8 |
|
public function getIterator() |
|
30
|
|
|
{ |
|
31
|
8 |
|
$this->validateNumberOfCells(); |
|
32
|
|
|
|
|
33
|
6 |
|
return parent::getIterator(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return CellInterface[] |
|
38
|
|
|
*/ |
|
39
|
4 |
|
public function getCells() |
|
40
|
|
|
{ |
|
41
|
4 |
|
$this->validateNumberOfCells(); |
|
42
|
|
|
|
|
43
|
4 |
|
return $this->getItems(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $name |
|
48
|
|
|
* |
|
49
|
|
|
* @return CellInterface|null |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function getNamedCell($name) |
|
52
|
|
|
{ |
|
53
|
1 |
|
return $this->getNamedItem($name); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param CellInterface $item |
|
58
|
|
|
* |
|
59
|
|
|
* @return $this |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function addCell(CellInterface $item) |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->addItem($item); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $name |
|
68
|
|
|
* @param CellInterface $cell |
|
69
|
|
|
* |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function addNamedCell($name, CellInterface $cell) |
|
73
|
|
|
{ |
|
74
|
2 |
|
return $this->addNamedItem($name, $cell); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @throws \InvalidArgumentException |
|
79
|
|
|
*/ |
|
80
|
12 |
|
private function validateNumberOfCells() |
|
81
|
|
|
{ |
|
82
|
12 |
|
$count = $this->count(); |
|
83
|
12 |
|
if (self::MINIMUM_CELLS <= $count) { |
|
84
|
10 |
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
throw new \InvalidArgumentException(sprintf( |
|
88
|
2 |
|
'At last %d cells are required, but there are only %d in collection %s', |
|
89
|
2 |
|
self::MINIMUM_CELLS, |
|
90
|
|
|
$count, |
|
91
|
|
|
get_class($this) |
|
92
|
|
|
)); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|