ArrayAccessor::hasValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace gamringer\JSONPointer\Access;
4
5
use gamringer\JSONPointer\VoidValue;
6
7
class ArrayAccessor implements Accesses
8
{
9
    public function &getValue(&$target, $token)
10
    {
11
        $pointedValue = new VoidValue($target, $token);
12
        
13
        if ($this->hasValue($target, $token)) {
14
            $pointedValue = &$target[$token];
15
        }
16
        
17
        return $pointedValue;
18
    }
19
    
20
    public function &setValue(&$target, $token, &$value)
21
    {
22
        $target[$token] = &$value;
23
        
24
        return $this->getValue($target, $token);
25
    }
26
27
    public function unsetValue(&$target, $token)
28
    {
29
        if (!$this->isIndexedArray($target)) {
30
            unset($target[$token]);
31
            return;
32
        }
33
34
        array_splice($target, $token, 1);
35
    }
36
    
37
    public function hasValue(&$target, $token)
38
    {
39
        return array_key_exists($token, $target);
40
    }
41
42
    public function isIndexedArray(Array $value)
43
    {
44
        $count = sizeof($value);
45
        $value[] = null;
46
47
        $result = array_key_exists($count, $value);
48
49
        array_pop($value);
50
51
        return  $result;
52
    }
53
54
    public function covers(&$target)
55
    {
56
        return is_array($target);
57
    }
58
}
59