Completed
Push — master ( cc51b8...3f6140 )
by Nate
02:16
created

Bag::filter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
namespace Tebru\Collection;
8
9
use ArrayIterator;
10
11
/**
12
 * Class Bag
13
 *
14
 * A generic [@see CollectionInterface] backed by an array.
15
 *
16
 * @author Nate Brunette <[email protected]>
17
 */
18
class Bag extends AbstractCollection
19
{
20
    /**
21
     * The collection elements
22
     *
23
     * @var array
24
     */
25
    protected $elements = [];
26
27
    /**
28
     * Constructor
29
     *
30
     * @param array $elements
31
     */
32 4
    public function __construct(array $elements = [])
33
    {
34 4
        $this->elements = array_values($elements);
35 4
    }
36
37
    /**
38
     * Ensure the element exists in the collection
39
     *
40
     * Returns true if the collection can contain duplicates,
41
     * and false if it cannot.
42
     *
43
     * By default this method will use strict comparison checking, passing false
44
     * in will use a double equals (==) instead.
45
     *
46
     * @param mixed $element
47
     * @param bool $strict
48
     * @return bool
49
     */
50 12
    public function add($element, bool $strict = true): bool
51
    {
52 12
        $this->elements[] = $element;
53
54 12
        return true;
55
    }
56
57
    /**
58
     * Removes all elements from a collection
59
     *
60
     * @return void
61
     */
62 1
    public function clear()
63
    {
64 1
        $this->elements = [];
65 1
    }
66
67
    /**
68
     * Returns true if the collection contains element
69
     *
70
     * By default this method will use strict comparison checking, passing false
71
     * in will use a double equals (==) instead.
72
     *
73
     * @param mixed $element
74
     * @param bool $strict
75
     * @return bool
76
     */
77 3
    public function contains($element, bool $strict = true): bool
78
    {
79 3
        return in_array($element, $this->elements, $strict);
80
    }
81
82
    /**
83
     * Removes object if it exists
84
     *
85
     * By default this method will use strict comparison checking, passing false
86
     * in will use a double equals (==) instead.
87
     *
88
     * Returns true if the element was removed
89
     *
90
     * @param mixed $element
91
     * @param bool $strict
92
     * @return bool
93
     */
94 3
    public function remove($element, bool $strict = true): bool
95
    {
96 3
        $index = array_search($element, $this->elements, $strict);
97
98 3
        if (false === $index) {
99 1
            return false;
100
        }
101
102 2
        array_splice($this->elements, $index, 1);
103
104 2
        return true;
105
    }
106
107
    /**
108
     * Returns an array of all elements in the collection
109
     *
110
     * @return array
111
     */
112 5
    public function toArray(): array
113
    {
114 5
        return $this->elements;
115
    }
116
117
    /**
118
     * Filter the collection using closure
119
     *
120
     * The closure will get passed each element.  Returning true from the
121
     * closure will include that element in the new collection.
122
     *
123
     * @param callable $filter
124
     * @return CollectionInterface
125
     */
126 1
    public function filter(callable $filter): CollectionInterface
127
    {
128 1
        return new Bag(array_filter($this->elements, $filter));
129
    }
130
131
    /**
132
     * Returns the size of the collection
133
     *
134
     * @return int
135
     */
136 9
    public function count(): int
137
    {
138 9
        return count($this->elements);
139
    }
140
141
    /**
142
     * Retrieve an external iterator
143
     *
144
     * @return ArrayIterator
145
     */
146 1
    public function getIterator(): ArrayIterator
147
    {
148 1
        return new ArrayIterator($this->elements);
149
    }
150
}
151