Passed
Push — master ( ede5c4...d0b3a2 )
by Max
02:45
created

BaseCollection::contains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PHPCollections\Collections;
6
7
use Countable;
8
use JsonSerializable;
9
use OutOfRangeException;
10
use PHPCollections\DataHolder;
11
12
/**
13
 * The base class for countable and
14
 * JSON serializable collections.
15
 */
16
abstract class BaseCollection implements Countable, JsonSerializable
17
{
18
    /**
19
     * The data container.
20
     *
21
     * @var \PHPCollections\DataHolder
22
     */
23
    protected $dataHolder;
24
25
    /**
26
     * Initializes the dataHolder property.
27
     *
28
     * @param array $data
29
     */
30 63
    public function __construct(array $data = [])
31
    {
32 63
        $this->dataHolder = new DataHolder($data);
33 63
    }
34
35
    /**
36
     * Reinitializes the dataHolder property.
37
     *
38
     * @return void
39
     */
40 3
    public function clear(): void
41
    {
42 3
        $this->dataHolder = new DataHolder();
43 3
    }
44
45
    /**
46
     * Checks if the collection
47
     * contains a given value.
48
     *
49
     * @param mixed $needle
50
     *
51
     * @return bool
52
     */
53 5
    public function contains($needle): bool
54
    {
55 5
        return in_array($needle, $this->toArray());
56
    }
57
58
    /**
59
     * Returns the length of the collection.
60
     *
61
     * @return int
62
     */
63 37
    public function count(): int
64
    {
65 37
        return count($this->toArray());
66
    }
67
68
    /**
69
     * Gets the difference between two collections.
70
     *
71
     * @param \PHPCollections\Collections\BaseCollection $collection
72
     *
73
     * @return \PHPCollections\Collections\BaseCollection
74
     */
75
    public abstract function diff(self $collection): self;
76
77
    /**
78
     * Checks if the given index
79
     * exists in the collection.
80
     *
81
     * @param mixed $offset
82
     *
83
     * @return bool
84
     */
85 6
    public function exists($offset): bool
86
    {
87 6
        return $this->dataHolder->offsetExists($offset);
88
    }
89
90
    /**
91
     * Gets the first element in the collection.
92
     *
93
     * @throws \OutOfRangeException
94
     *
95
     * @return mixed
96
     */
97 6
    public function first()
98
    {
99 6
        if ($this->isEmpty()) {
100 1
            throw new OutOfRangeException('You\'re trying to get data from an empty collection');
101
        }
102
103 6
        return array_values($this->toArray())[0];
104
    }
105
106
    /**
107
     * Checks if the collection is empty.
108
     *
109
     * @return bool
110
     */
111 24
    public function isEmpty(): bool
112
    {
113 24
        return $this->count() === 0;
114
    }
115
116
    /**
117
     * Defines the behavior of the collection
118
     * when json_encode is called.
119
     *
120
     * @return array
121
     */
122
    public function jsonSerialize(): array
123
    {
124
        return $this->toArray();
125
    }
126
127
    /**
128
     * Gets the last element of the collection.
129
     *
130
     * @throws \OutOfRangeException
131
     *
132
     * @return mixed
133
     */
134 7
    public function last()
135
    {
136 7
        if ($this->isEmpty()) {
137 1
            throw new OutOfRangeException('You\'re trying to get data from an empty collection');
138
        }
139
140 7
        return array_values($this->toArray())[$this->count() - 1];
141
    }
142
143
    /**
144
     * Returns a portion of the collection.
145
     *
146
     * @param int      $offset
147
     * @param int|null $lenght
148
     *
149
     * @return \PHPCollections\Collections\BaseCollection
150
     */
151
    public abstract function slice(int $offset, ?int $lenght): ?self;
152
153
    /**
154
     * Returns a plain array with
155
     * your dictionary data.
156
     *
157
     * @return array
158
     */
159 41
    public function toArray(): array
160
    {
161 41
        return $this->dataHolder->getContainer();
162
    }
163
}
164