Completed
Push — master ( 494812...c03665 )
by Juliano
13s
created

Collection::each()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Raidros\Collection;
4
5
use Raidros\Collection\Exceptions\CollectionItemNotFoundException;
6
use Raidros\Collection\Exceptions\InvalidCollectionItemInstanceException;
7
8
class Collection
9
{
10
    /**
11
     * Collection items.
12
     *
13
     * @var array
14
     */
15
    private $items = [];
16
17
    /**
18
     * Implementation namespace.
19
     *
20
     * @var string
21
     */
22
    private $classname;
23
24
    /**
25
     * Create a new collection instance.
26
     *
27
     * @param string|null $classname Class or interface of namespace for implementation
28
     */
29 57
    public function __construct($classname = null)
30
    {
31 57
        $this->classname = $classname;
32 57
    }
33
34
    /**
35
     * Add a new item to collection.
36
     *
37
     * @param string $name
38
     * @param mixed  $value
39
     *
40
     * @return self
41
     */
42 57
    public function put($name, $value)
43
    {
44 57
        if (is_object($value) && !$this->isValidObjectInstance($value)) {
45 12
            throw new InvalidCollectionItemInstanceException(sprintf('%s must implement %s', $name, $this->classname));
46
        }
47
48 45
        $this->items[$name] = $value;
49
50 45
        return $this->items[$name];
51
    }
52
53
    /**
54
     * Verify's if the given object has a valid instance.
55
     *
56
     * @param object $object
57
     *
58
     * @return bool
59
     */
60 57
    private function isValidObjectInstance($object)
61
    {
62 57
        if (!is_null($this->classname) && !$this->isInstanceOrSubclassOf($object)) {
63 12
            return false;
64
        }
65
66 45
        return true;
67
    }
68
69
    /**
70
     * Check object instance.
71
     *
72
     * @param object $object
73
     *
74
     * @return bool
75
     */
76 57
    private function isInstanceOrSubclassOf($object)
77
    {
78 57
        return $object instanceof $this->classname || is_subclass_of($object, $this->classname);
79
    }
80
81
    /**
82
     * Get all items from collection.
83
     *
84
     * @return array
85
     */
86 3
    public function all()
87
    {
88 3
        return $this->items;
89
    }
90
91
    /**
92
     * Find collection item by name.
93
     *
94
     * @param string $name
95
     *
96
     * @return mixed|null
97
     */
98 33
    public function find($name)
99
    {
100 33
        if ($this->has($name)) {
101 21
            return $this->items[$name];
102
        }
103 12
    }
104
105
    /**
106
     * Check if the given item exists on collection.
107
     *
108
     * @param string $name
109
     *
110
     * @return bool
111
     */
112 42
    public function has($name)
113
    {
114 42
        return array_key_exists($name, $this->items);
115
    }
116
117
    /**
118
     * Find a specific collection item or throw's an exception.
119
     *
120
     * @param string $name
121
     *
122
     * @return mixed
123
     */
124 12
    public function findOrFail($name)
125
    {
126 12
        $result = $this->find($name);
127
128 12
        if (is_null($result)) {
129 3
            throw new CollectionItemNotFoundException(sprintf('Collection item "%s" not found', $name));
130
        }
131
132 9
        return $result;
133
    }
134
135
    /**
136
     * Run a callback over each item.
137
     *
138
     * @param callable $callback
139
     *
140
     * @return self
141
     */
142 3
    public function each(callable $callback)
143
    {
144 3
        if (count($this->items)) {
145 3
            array_walk($this->items, $callback);
146 3
        }
147
148 3
        return $this;
149
    }
150
151
    /**
152
     * Remove an item from collection.
153
     *
154
     * @param string $name
155
     *
156
     * @return self
157
     */
158 9
    public function remove($name)
159
    {
160 9
        if ($this->has($name)) {
161 9
            unset($this->items[$name]);
162 9
        }
163
164 9
        return $this;
165
    }
166
}
167