Completed
Push — master ( 734525...97c065 )
by Owen
02:29
created

Collection::getWhere()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.2
cc 4
eloc 4
nc 4
nop 1
1
<?php
2
3
namespace Mundanity\Collection;
4
5
6
/**
7
 * Implements a basic collection from an array.
8
 *
9
 */
10
class Collection implements CollectionInterface
11
{
12
    /**
13
     * The data in the collection.
14
     *
15
     * @var array
16
     *
17
     */
18
    protected $data = [];
19
20
21
    /**
22
     * Constructor
23
     *
24
     * @param array $data
25
     *   An array of data to populate the collection with.
26
     *
27
     */
28
    public function __construct(array $data = [])
29
    {
30
        $this->data = $data;
31
    }
32
33
34
    /**
35
     * {@inheritdoc}
36
     *
37
     */
38
    public function has($item)
39
    {
40
        return in_array($item, $this->data);
41
    }
42
43
44
    /**
45
     * {@inheritdoc}
46
     *
47
     */
48
    public function getWhere(callable $callable)
49
    {
50
        foreach($this->data as $item) {
51
            if ($callable($item) === true) {
52
                return is_object($item) ? clone $item : $item;
53
            }
54
        }
55
    }
56
57
58
    /**
59
     * {@inheritdoc}
60
     *
61
     */
62
    public function isEmpty()
63
    {
64
        return empty($this->data);
65
    }
66
67
68
    /**
69
     * Returns the number of items within the collection.
70
     *
71
     * @return int
72
     *
73
     */
74
    public function count()
75
    {
76
        return count($this->data);
77
    }
78
79
80
    /**
81
     * Returns an iterator.
82
     *
83
     * @return \Traversable
84
     *
85
     */
86
    public function getIterator()
87
    {
88
        return new \ArrayIterator($this->data);
89
    }
90
91
92
    /**
93
     * {@inheritdoc}
94
     *
95
     */
96
    public function toArray()
97
    {
98
        return $this->data;
99
    }
100
101
102
    /**
103
     * {@inheritdoc}
104
     *
105
     */
106
    public function filter(callable $callable)
107
    {
108
        $data = array_filter($this->data, $callable, ARRAY_FILTER_USE_BOTH);
109
        return new static($data);
110
    }
111
112
113
    /**
114
     * {@inheritdoc}
115
     *
116
     */
117
    public function map(callable $callable)
118
    {
119
        return array_map($callable, $this->data);
120
    }
121
}
122