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