1
|
|
|
<?php namespace Mbh\Collection\Traits\Sequenceable\Arrayed; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MBHFramework |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/MBHFramework/mbh-framework |
7
|
|
|
* @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos |
8
|
|
|
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use Traversable; |
12
|
|
|
use OutOfRangeException; |
13
|
|
|
|
14
|
|
|
trait ArrayAccess |
15
|
|
|
{ |
16
|
|
|
protected $sfa = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ArrayAccess |
20
|
|
|
*/ |
21
|
|
|
public function offsetExists($offset): bool |
22
|
|
|
{ |
23
|
|
|
return is_integer($offset) |
24
|
|
|
&& $this->validIndex($offset) |
25
|
|
|
&& $this->sfa->offsetExists($offset); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function offsetGet($offset) |
29
|
|
|
{ |
30
|
|
|
return $this->sfa->offsetGet($offset); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function offsetSet($offset, $value) |
34
|
|
|
{ |
35
|
|
|
if ($offset === null) { |
36
|
|
|
$this->push($value); |
37
|
|
|
} elseif (is_integer($offset)) { |
38
|
|
|
$this->set($offset, $value); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function offsetUnset($offset) |
43
|
|
|
{ |
44
|
|
|
return is_integer($offset) |
45
|
|
|
&& $this->remove($offset); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
abstract protected function checkCapacity(); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Adds zero or more values to the end of the sequence. |
52
|
|
|
* |
53
|
|
|
* @param mixed ...$values |
54
|
|
|
*/ |
55
|
|
|
abstract public function push(...$values); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Removes and returns the value at a given index in the sequence. |
59
|
|
|
* |
60
|
|
|
* @param int $index this index to remove. |
61
|
|
|
* |
62
|
|
|
* @return mixed the removed value. |
63
|
|
|
* |
64
|
|
|
* @throws OutOfRangeException if the index is not in the range [0, size-1] |
65
|
|
|
*/ |
66
|
|
|
abstract public function remove(int $index); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Replaces the value at a given index in the sequence with a new value. |
70
|
|
|
* |
71
|
|
|
* @param int $index |
72
|
|
|
* @param mixed $value |
73
|
|
|
* |
74
|
|
|
* @throws OutOfRangeException if the index is not in the range [0, size-1] |
75
|
|
|
*/ |
76
|
|
|
abstract public function set(int $index, $value); |
77
|
|
|
|
78
|
|
|
abstract protected function validIndex(int $index); |
79
|
|
|
} |
80
|
|
|
|