ArrayAccess::offsetSet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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