Completed
Pull Request — master (#81)
by
unknown
03:04
created

Collection::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Zewa;
5
6
use Zewa\Interfaces\CollectionInterface;
7
8
/**
9
 * Class Collection
10
 * @package App\Models
11
 */
12
class Collection implements CollectionInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    public $collection = [];
18
19
    /**
20
     * If $data is passed, populate collection
21
     *
22
     * @param $data array
23
     * @access public
24
     */
25
    public function __construct(array $data)
26
    {
27
        $this->collection = $data;
28
    }
29
30
    public function count() : int
31
    {
32
        return count($this->collection);
33
    }
34
35
    public function isEmpty() : bool
36
    {
37
        if (!empty($this->collection)) {
38
            return false;
39
        }
40
41
        return true;
42
    }
43
44
    public function getArray() : array
45
    {
46
        return $this->collection;
47
    }
48
49
    public function jsonSerialize() : array //@TODO: this should return text i think
50
    {
51
        return $this->collection;
52
    }
53
54
    public function getIterator()
55
    {
56
        return new \ArrayIterator($this->collection);
57
    }
58
59
    public function offsetExists($offset)
60
    {
61
        return array_key_exists($offset, $this->collection);
62
    }
63
64
    public function offsetGet($offset)
65
    {
66
        return $this->collection[$offset] ?? null;
67
    }
68
69
    public function offsetSet($offset, $value)
70
    {
71
        $this->collection[$offset] = $value;
72
    }
73
74
    public function offsetUnset($offset)
75
    {
76
        if ($this->offsetExists($offset)) {
77
            unset($this->collection[$offset]);
78
        }
79
    }
80
81
    public function clear()
82
    {
83
        $this->collection = [];
84
    }
85
86
    public function mutate($mutation)
87
    {
88
        $result = [];
89
        foreach ($this->collection as $item) {
90
            $result[] = new $mutation($item);
91
        }
92
        $this->clear();
93
        $this->collection = $result;
94
    }
95
96 View Code Duplication
    public function map(callable $func)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $result = [];
99
100
        foreach ($this->collection as $key => $item) {
101
            $result[$key] = $func($item);
102
        }
103
104
        $this->clear();
105
        $this->collection = $result;
106
    }
107
108 View Code Duplication
    public function filter(callable $func)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110
        $result = [];
111
112
        foreach ($this->collection as $key => $item) {
113
            if ($func($key, $item)) {
114
                $result[$key] = $item;
115
            }
116
        }
117
118
        $this->clear();
119
        $this->collection = $result;
120
    }
121
122
    public function each(callable $func)
123
    {
124
        $result = [];
125
126
        foreach ($this->collection as $key => $item) {
127
            $result[$key] = $func($key, $item);
128
        }
129
    }
130
131 View Code Duplication
    public function not(callable $func)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        $result = [];
134
135
        foreach ($this->collection as $key => $item) {
136
            if (! $func($key, $item)) {
137
                $result[$key] = $item;
138
            }
139
        }
140
141
        $this->clear();
142
        $this->collection = $result;
143
    }
144
145
    public function reduce($initial, callable $func)
146
    {
147
        $accumulator = $initial;
148
149
        foreach ($this->collection as $item) {
150
            $accumulator = $func($accumulator, $item);
151
        }
152
153
        return $accumulator;
154
    }
155
}
156