Completed
Pull Request — master (#1)
by Lucas Pires
15:56
created

Collection::isValidObjectInstance()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.2
cc 4
eloc 4
nc 2
nop 1
crap 4
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 45
    public function __construct($classname = null)
30
    {
31 45
        $this->classname = $classname;
32 45
    }
33
34
    /**
35
     * Add a new item to collection.
36
     *
37
     * @param  string $name
38
     * @param  mixed  $value
39
     *
40
     * @return self
41
     */
42 45
    public function put($name, $value)
43
    {
44 45
        if (is_object($value) && ! $this->isValidObjectInstance($value)) {
45 12
            throw new InvalidCollectionItemInstanceException(sprintf('Collection item must be an instance or implementation of %s', $this->classname));
46
        }
47
48 33
        $this->items[$name] = $value;
49
50 33
        return $this;
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 45
    private function isValidObjectInstance($object)
61
    {
62 45
        if (! is_null($this->classname) && ! $object instanceof $this->classname && ! is_subclass_of($object, $this->classname)) {
63 12
            return false;
64
        }
65
66 33
        return true;
67
    }
68
69
    /**
70
     * Get all items from collection.
71
     *
72
     * @return array
73
     */
74 3
    public function all()
75
    {
76 3
        return $this->items;
77
    }
78
79
    /**
80
     * Find collection item by name.
81
     *
82
     * @param  string $name
83
     *
84
     * @return mixed|null
85
     */
86 21
    public function find($name)
87
    {
88 21
        if ($this->has($name)) {
89 18
            return $this->items[$name];
90
        }
91 3
    }
92
93
    /**
94
     * Check if the given item exists on collection.
95
     *
96
     * @param  string $name
97
     *
98
     * @return bool
99
     */
100 30
    public function has($name)
101
    {
102 30
        return array_key_exists($name, $this->items);
103
    }
104
105
    /**
106
     * Find a specific collection item or throw's an exception.
107
     *
108
     * @param  string $name
109
     *
110
     * @return mixed
111
     */
112 12
    public function findOrFail($name)
113
    {
114 12
        $result = $this->find($name);
115
116 12
        if (is_null($result)) {
117 3
            throw new CollectionItemNotFoundException(sprintf('Collection item "%s" not found', $name));
118
        }
119
120 9
        return $result;
121
    }
122
}
123