Completed
Push — master ( 0d8e21...641b84 )
by Ben
05:24 queued 02:23
created

BranchCollection::flatten()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 2
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);
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->branches of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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