1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Vine\Branch; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use ArrayIterator; |
7
|
|
|
use Countable; |
8
|
|
|
use IteratorAggregate; |
9
|
|
|
use Thinktomorrow\Vine\VineException; |
10
|
|
|
|
11
|
|
|
class BranchCollection implements ArrayAccess, Countable, IteratorAggregate |
12
|
|
|
{ |
13
|
|
|
private $branches; |
14
|
|
|
|
15
|
36 |
|
public function __construct(array $branches = []) |
16
|
|
|
{ |
17
|
36 |
|
self::validateBranches($branches); |
18
|
|
|
|
19
|
36 |
|
$this->branches = $branches; |
20
|
36 |
|
} |
21
|
|
|
|
22
|
12 |
|
public function hasBranches() |
23
|
|
|
{ |
24
|
12 |
|
return ($this->branches && count($this->branches) > 0); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
18 |
|
public function push(Branch $branch) |
28
|
|
|
{ |
29
|
18 |
|
$this->branches[] = $branch; |
30
|
|
|
|
31
|
18 |
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
public function __get($key) |
35
|
|
|
{ |
36
|
3 |
|
if(isset($this->$key)) return $this->$key; |
37
|
|
|
|
38
|
|
|
throw VineException::unknownBranchCollectionAttribute($key); |
39
|
|
|
} |
40
|
|
|
|
41
|
36 |
|
public static function validateBranches($branches) |
42
|
|
|
{ |
43
|
36 |
|
foreach($branches as $branch) |
44
|
|
|
{ |
45
|
9 |
|
if(!$branch instanceof Branch) |
46
|
6 |
|
{ |
47
|
3 |
|
throw VineException::requiredAsBranch($branch); |
48
|
|
|
} |
49
|
24 |
|
} |
50
|
36 |
|
} |
51
|
|
|
|
52
|
3 |
|
public function offsetSet($offset, $value) { |
53
|
3 |
|
if (is_null($offset)) { |
54
|
3 |
|
$this->branches[] = $value; |
55
|
2 |
|
} else { |
56
|
|
|
$this->branches[$offset] = $value; |
57
|
|
|
} |
58
|
3 |
|
} |
59
|
|
|
|
60
|
3 |
|
public function offsetExists($offset) { |
61
|
3 |
|
return isset($this->branches[$offset]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function offsetUnset($offset) { |
65
|
|
|
unset($this->branches[$offset]); |
66
|
|
|
} |
67
|
|
|
|
68
|
21 |
|
public function offsetGet($offset) { |
69
|
21 |
|
return isset($this->branches[$offset]) ? $this->branches[$offset] : null; |
70
|
|
|
} |
71
|
|
|
|
72
|
21 |
|
public function count() |
73
|
|
|
{ |
74
|
21 |
|
return count($this->branches); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Iterator for the branches |
79
|
|
|
* |
80
|
|
|
* @return \ArrayIterator |
81
|
|
|
*/ |
82
|
24 |
|
public function getIterator() |
83
|
|
|
{ |
84
|
24 |
|
return new ArrayIterator($this->branches); |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
public function toArray() |
88
|
|
|
{ |
89
|
3 |
|
return BuildArray::fromCollection($this); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function flatten() |
93
|
|
|
{ |
94
|
|
|
// return a flattened array of all branch items |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.