1
|
|
|
<?php namespace Mbh\Collection\Traits\Sequenceable\LinkedList; |
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 OutOfBoundsException; |
13
|
|
|
use OutOfRangeException; |
14
|
|
|
|
15
|
|
|
trait ArrayAccess |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
19
|
|
|
* @param int $offset |
20
|
|
|
* @return boolean |
21
|
|
|
*/ |
22
|
|
|
public function offsetExists($offset) |
23
|
|
|
{ |
24
|
|
|
$index = $this->intGuard($offset); |
25
|
|
|
return $index >= 0 && $index < $this->count(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
30
|
|
|
* @param int $offset |
31
|
|
|
* @return mixed |
32
|
|
|
* @throws OutOfBoundsException |
33
|
|
|
*/ |
34
|
|
|
public function offsetGet($offset) |
35
|
|
|
{ |
36
|
|
|
$n = $this->guardedSeek($offset, __METHOD__); |
37
|
|
|
return $n->value(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
42
|
|
|
* @param int|null $offset |
43
|
|
|
* @param mixed $value |
44
|
|
|
* @return void |
45
|
|
|
* @throws OutOfBoundsException |
46
|
|
|
*/ |
47
|
|
|
public function offsetSet($offset, $value) |
48
|
|
|
{ |
49
|
|
|
if ($offset === null) { |
50
|
|
|
$this->push($value); |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
$n = $this->guardedSeek($offset, __METHOD__); |
54
|
|
|
$n->setValue($value); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
59
|
|
|
* @param int $offset |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function offsetUnset($offset) |
63
|
|
|
{ |
64
|
|
|
$index = $this->intGuard($offset); |
65
|
|
|
if ($this->offsetExists($index)) { |
66
|
|
|
$n = $this->seekTo($index); |
67
|
|
|
$this->removeNode($n); |
68
|
|
|
$this->current = $n->prev(); |
|
|
|
|
69
|
|
|
$this->offset--; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @link http://php.net/manual/en/countable.count.php |
75
|
|
|
* @return int |
76
|
|
|
*/ |
77
|
|
|
abstract public function count(): int; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param mixed $i |
81
|
|
|
* @return int |
82
|
|
|
* @throws Exception |
83
|
|
|
*/ |
84
|
|
|
abstract protected function intGuard($i); |
85
|
|
|
|
86
|
|
|
abstract protected function guardedSeek($index, $method); |
87
|
|
|
|
88
|
|
|
abstract public function push(...$values); |
89
|
|
|
|
90
|
|
|
abstract protected function removeNode(LinkedNode $n); |
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $offset |
94
|
|
|
* @return LinkedDataNode |
|
|
|
|
95
|
|
|
*/ |
96
|
|
|
abstract protected function seekTo($offset); |
97
|
|
|
} |
98
|
|
|
|