Sequenceable   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 8
dl 0
loc 169
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A copy() 0 3 1
A unserialize() 0 4 1
A contains() 0 9 3
1
<?php namespace Mbh\Collection\Traits;
2
3
use Mbh\Collection\Interfaces\Sequenceable as SequenceableInterface;
4
use Traversable;
5
use ArrayAccess;
6
use Iterator;
7
use SplFixedArray;
8
use SplHeap;
9
use UnderflowException;
10
use OutOfRangeException;
11
12
/**
13
 * MBHFramework
14
 *
15
 * @link      https://github.com/MBHFramework/mbh-framework
16
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
17
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
18
 */
19
20
trait Sequenceable
21
{
22
    /**
23
     * Build from an array
24
     *
25
     * @return SequenceableInterface
26
     */
27
    abstract public static function fromArray(array $array);
28
29
    /**
30
     * Factory for building FixedArrays from any traversable
31
     *
32
     * @return SequenceableInterface
33
     */
34
    abstract public static function fromItems(Traversable $array);
35
36
    /**
37
     * Determines whether the sequence contains all of zero or more values.
38
     *
39
     * @param mixed ...$values
40
     *
41
     * @return bool true if at least one value was provided and the sequence
42
     *              contains all given values, false otherwise.
43
     */
44
    public function contains(...$values): bool
45
    {
46
        foreach ($values as &$value) {
47
            if ($this->search($value) !== null) {
48
                return false;
49
            }
50
        }
51
52
        return true;
53
    }
54
55
    /**
56
     * Returns a shallow copy of the collection.
57
     *
58
     * @return Collection a copy of the collection.
59
     */
60
    public function copy()
61
    {
62
        return static::fromArray($this->toArray());
63
    }
64
65
    /**
66
     * Returns the first value in the sequence.
67
     *
68
     * @return mixed
69
     *
70
     * @throws UnderflowException if the sequence is empty.
71
     */
72
    abstract public function first();
73
74
    /**
75
     * Returns the value at a given index (position) in the sequence.
76
     *
77
     * @param int $index
78
     *
79
     * @return mixed
80
     *
81
     * @throws OutOfRangeException if the index is not in the range [0, size-1]
82
     */
83
    abstract public function get(int $index);
84
85
    /**
86
     * Inserts zero or more values at a given index.
87
     *
88
     * Each value after the index will be moved one position to the right.
89
     * Values may be inserted at an index equal to the size of the sequence.
90
     *
91
     * @param int   $index
92
     * @param mixed ...$values
93
     *
94
     * @throws OutOfRangeException if the index is not in the range [0, n]
95
     */
96
    abstract public function insert(int $index, ...$values);
97
98
    /**
99
     * Returns the last value in the sequence.
100
     *
101
     * @return mixed
102
     *
103
     * @throws UnderflowException if the sequence is empty.
104
     */
105
    abstract public function last();
106
107
    /**
108
     * Removes the last value in the sequence, and returns it.
109
     *
110
     * @return mixed what was the last value in the sequence.
111
     *
112
     * @throws UnderflowException if the sequence is empty.
113
     */
114
    abstract public function pop();
115
116
    /**
117
     * Adds zero or more values to the end of the sequence.
118
     *
119
     * @param mixed ...$values
120
     */
121
    abstract public function push(...$values);
122
123
    /**
124
     * Removes and returns the value at a given index in the sequence.
125
     *
126
     * @param int $index this index to remove.
127
     *
128
     * @return mixed the removed value.
129
     *
130
     * @throws OutOfRangeException if the index is not in the range [0, size-1]
131
     */
132
    abstract public function remove(int $index);
133
134
    /**
135
     * Find a single element key
136
     *
137
     * @param mixed $value The value to search
138
     * @return mixed The key for the element we found
139
     */
140
    abstract public function search($value);
141
142
    /**
143
     * Replaces the value at a given index in the sequence with a new value.
144
     *
145
     * @param int   $index
146
     * @param mixed $value
147
     *
148
     * @throws OutOfRangeException if the index is not in the range [0, size-1]
149
     */
150
    abstract public function set(int $index, $value);
151
152
    abstract protected function setValues(Traversable $traversable);
153
154
    /**
155
     * Removes and returns the first value in the sequence.
156
     *
157
     * @return mixed what was the first value in the sequence.
158
     *
159
     * @throws UnderflowException if the sequence was empty.
160
     */
161
    abstract public function shift();
162
163
    /**
164
     * Returns an array representation of the collection.
165
     *
166
     * The format of the returned array is implementation-dependent.
167
     * Some implementations may throw an exception if an array representation
168
     * could not be created.
169
     *
170
     * @return array
171
     */
172
    abstract public function toArray(): array;
173
174
    /**
175
     * @inheritDoc
176
     */
177
    public function unserialize($values)
178
    {
179
        $values = unserialize($values);
180
        $this->setValues(SplFixedArray::fromArray($values));
181
    }
182
183
    /**
184
     * Adds zero or more values to the front of the sequence.
185
     *
186
     * @param mixed ...$values
187
     */
188
    abstract public function unshift(...$values);
189
}
190