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
|
|
|
* Create an fixed array |
20
|
|
|
* |
21
|
|
|
* @param Traversable $array data |
22
|
|
|
*/ |
23
|
|
|
protected function __construct(Traversable $array) |
24
|
|
|
{ |
25
|
|
|
$this->sfa = $array; |
26
|
|
|
$this->checkCapacity(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* ArrayAccess |
31
|
|
|
*/ |
32
|
|
|
public function offsetExists($offset): bool |
33
|
|
|
{ |
34
|
|
|
return is_integer($offset) |
35
|
|
|
&& $this->validIndex($offset) |
36
|
|
|
&& $this->sfa->offsetExists($offset); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function offsetGet($offset) |
40
|
|
|
{ |
41
|
|
|
return $this->sfa->offsetGet($offset); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function offsetSet($offset, $value) |
45
|
|
|
{ |
46
|
|
|
if ($offset === null) { |
47
|
|
|
$this->push($value); |
48
|
|
|
} elseif (is_integer($offset)) { |
49
|
|
|
$this->set($offset, $value); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function offsetUnset($offset) |
54
|
|
|
{ |
55
|
|
|
return is_integer($offset) |
56
|
|
|
&& $this->remove($offset); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
abstract protected function checkCapacity(); |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Adds zero or more values to the end of the sequence. |
63
|
|
|
* |
64
|
|
|
* @param mixed ...$values |
65
|
|
|
*/ |
66
|
|
|
abstract public function push(...$values); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Removes and returns the value at a given index in the sequence. |
70
|
|
|
* |
71
|
|
|
* @param int $index this index to remove. |
72
|
|
|
* |
73
|
|
|
* @return mixed the removed value. |
74
|
|
|
* |
75
|
|
|
* @throws OutOfRangeException if the index is not in the range [0, size-1] |
76
|
|
|
*/ |
77
|
|
|
abstract public function remove(int $index); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Replaces the value at a given index in the sequence with a new value. |
81
|
|
|
* |
82
|
|
|
* @param int $index |
83
|
|
|
* @param mixed $value |
84
|
|
|
* |
85
|
|
|
* @throws OutOfRangeException if the index is not in the range [0, size-1] |
86
|
|
|
*/ |
87
|
|
|
abstract public function set(int $index, $value); |
88
|
|
|
|
89
|
|
|
abstract protected function validIndex(int $index); |
90
|
|
|
} |
91
|
|
|
|