Passed
Push — master ( 78936a...47c5f2 )
by Smoren
02:13
created

NestedHelper::isset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Smoren\NestedAccessor\Helpers;
4
5
use Smoren\NestedAccessor\Components\NestedAccessor;
6
use Smoren\NestedAccessor\Exceptions\NestedAccessorException;
7
8
/**
9
 * Helper for getting and setting to source array or object with nested keys
10
 * @author Smoren <[email protected]>
11
 */
12
class NestedHelper
13
{
14
    /**
15
     * @var NestedAccessor|null nested accessor instance
16
     */
17
    protected static ?NestedAccessor $accessor = null;
18
19
    /**
20
     * Gets value from nested source by given path
21
     * @param array<scalar, mixed>|object $source source
22
     * @param string|array<string> $path path e.g. 'path.to.value' or ['path', 'to', 'value']
23
     * @param bool $strict if true: throw exception when path is not found in source
24
     * @return mixed value got by path
25
     * @throws NestedAccessorException
26
     */
27
    public static function get($source, $path, bool $strict = true)
28
    {
29
        return static::prepareAccessor($source)->get($path, $strict);
30
    }
31
32
    /**
33
     * Sets value to nested source by given path
34
     * @param array<scalar, mixed>|object $source source
35
     * @param string|array<string> $path path e.g. 'path.to.value' or ['path', 'to', 'value']
36
     * @param mixed $value value to set
37
     * @param bool $strict if true: throw exception when path is not found in source
38
     * @return void
39
     * @throws NestedAccessorException
40
     */
41
    public static function set(&$source, $path, $value, bool $strict = true): void
42
    {
43
        static::prepareAccessor($source)->set($path, $value, $strict);
44
    }
45
46
47
    /**
48
     * Appends value to nested source by given path
49
     * @param array<scalar, mixed>|object $source source
50
     * @param string|array<string> $path path e.g. 'path.to.value' or ['path', 'to', 'value']
51
     * @param mixed $value value to set
52
     * @param bool $strict if true: throw exception when path is not found in source
53
     * @return void
54
     * @throws NestedAccessorException
55
     */
56
    public static function append(&$source, $path, $value, bool $strict = true): void
57
    {
58
        static::prepareAccessor($source)->append($path, $value, $strict);
59
    }
60
61
    /**
62
     * Returns true if path exists, false otherwise
63
     * @param array<scalar, mixed>|object $source source
64
     * @param string|string[] $path nested path
65
     * @return bool
66
     * @throws NestedAccessorException
67
     */
68
    public static function exist(&$source, $path): bool
69
    {
70
        return static::prepareAccessor($source)->exist($path);
71
    }
72
73
    /**
74
     * Returns true if path exists and not null, false otherwise
75
     * @param array<scalar, mixed>|object $source source
76
     * @param string|string[] $path nested path
77
     * @return bool
78
     * @throws NestedAccessorException
79
     */
80
    public static function isset(&$source, $path): bool
81
    {
82
        return static::prepareAccessor($source)->isset($path);
83
    }
84
85
    /**
86
     * Method for preparing accessor to use with source
87
     * @param array<scalar, mixed>|object $source source data
88
     * @return NestedAccessor
89
     * @throws NestedAccessorException
90
     */
91
    protected static function prepareAccessor(&$source): NestedAccessor
92
    {
93
        if(static::$accessor === null) {
94
            static::$accessor = new NestedAccessor($source);
95
        } else {
96
            static::$accessor->setSource($source);
97
        }
98
99
        return static::$accessor;
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::accessor could return the type null which is incompatible with the type-hinted return Smoren\NestedAccessor\Components\NestedAccessor. Consider adding an additional type-check to rule them out.
Loading history...
100
    }
101
}
102