Test Failed
Push — develop ( c4a2cb...83a5b5 )
by Paul
07:40
created

Arguments::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
use GeminiLabs\SiteReviews\Helpers\Cast;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
use GeminiLabs\SiteReviews\Modules\Sanitizer;
9
10
class Arguments extends \ArrayObject
11
{
12
    /**
13
     * @param mixed $args
14 160
     */
15
    public function __construct($args = [])
16 160
    {
17
        if ($args instanceof Arguments) {
18
            $args = $args->toArray();
19 160
        } else {
20
            $args = Arr::consolidate($args);
21 160
        }
22
        parent::__construct($args, \ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS);
23
    }
24
25
    public function __toString(): string
26
    {
27
        return serialize($this->toArray());
28
    }
29
30
    /**
31
     * @param mixed $key
32
     */
33
    public function array($key, array $fallback = []): array
34 86
    {
35
        return Arr::consolidate($this->get($key, $fallback));
36 86
    }
37
38
    /**
39
     * @param mixed $key
40
     *
41
     * @return mixed
42
     */
43
    public function cast($key, string $cast, $fallback = null)
44
    {
45 172
        return Cast::to($cast, $this->get($key, $fallback));
46
    }
47 172
48 172
    public function exists(string $key): bool
49 25
    {
50 172
        return $this->offsetExists($key);
51
    }
52
53 2
    /**
54
     * @param mixed $key
55 2
     * @param mixed $fallback
56
     *
57
     * @return mixed
58
     */
59
    public function get($key, $fallback = null)
60
    {
61 25
        $value = Arr::get($this->getArrayCopy(), $key, null);
62
        if (is_null($fallback)) {
63 25
            return $value;
64 25
        }
65 25
        return Helper::ifEmpty($value, $fallback);
66
    }
67
68
    public function isEmpty(): bool
69
    {
70
        return empty($this->getArrayCopy());
71
    }
72
73
    /**
74 158
     * @return self
75
     */
76 158
    public function merge(array $data = [])
77
    {
78
        $storage = wp_parse_args($data, $this->getArrayCopy());
79
        $this->exchangeArray($storage);
80
        return $this;
81
    }
82
83 4
    /**
84
     * @param mixed $key
85 4
     *
86 4
     * @return mixed
87 4
     */
88
    #[\ReturnTypeWillChange]
89
    public function offsetGet($key)
90
    {
91
        return $this->get($key);
92
    }
93
94
    /**
95
     * @param mixed $key
96
     */
97
    #[\ReturnTypeWillChange]
98
    public function offsetUnset($key): void
99
    {
100
        $storage = $this->getArrayCopy();
101
        unset($storage[$key]);
102
        $this->exchangeArray($storage);
103
    }
104 25
105
    /**
106 25
     * @return self
107 25
     */
108 25
    public function replace(array $data = [])
109 25
    {
110
        $this->exchangeArray($data);
111
        return $this;
112
    }
113
114
    /**
115 44
     * @param mixed $key
116
     *
117 44
     * @return mixed
118 44
     */
119
    public function sanitize($key, string $sanitizer)
120
    {
121
        $sanitizers = ['key' => $sanitizer];
122
        $values = ['key' => $this->get($key)];
123
        $values = glsr(Sanitizer::class, compact('values', 'sanitizers'))->run();
124 151
        return Arr::get($values, 'key');
125
    }
126 151
127
    /**
128
     * @param mixed $value
129
     */
130
    public function set(string $path, $value): void
131
    {
132
        $storage = Arr::set($this->getArrayCopy(), $path, $value);
133
        $this->exchangeArray($storage);
134
    }
135
136
    /**
137
     * @param array $args Optional parameter that can be used to change the output
138
     */
139
    public function toArray(array $args = []): array
140
    {
141
        return Cast::toArrayDeep($this->getArrayCopy());
142
    }
143
}
144