Passed
Push — master ( 197c27...f2cc15 )
by Paul
22:24 queued 07:48
created

Arguments   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 64.1%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 13
eloc 29
c 2
b 0
f 2
dl 0
loc 116
ccs 25
cts 39
cp 0.641
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A cast() 0 3 1
A get() 0 6 2
A toArray() 0 3 1
A sanitize() 0 6 1
A merge() 0 5 1
A offsetUnset() 0 6 1
A __toString() 0 3 1
A offsetGet() 0 4 1
A isEmpty() 0 3 1
A set() 0 4 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\Modules\Sanitizer;
8
9
class Arguments extends \ArrayObject
10
{
11
    /**
12
     * @param mixed $args
13
     */
14 24
    public function __construct($args)
15
    {
16 24
        if ($args instanceof Arguments) {
17 7
            $args = $args->toArray();
18
        } else {
19 24
            $args = Arr::consolidate($args);
20
        }
21 24
        parent::__construct($args, \ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS);
22 24
    }
23
24
    /**
25
     * @return string
26
     */
27 6
    public function __toString()
28
    {
29 6
        return serialize($this->toArray());
30
    }
31
32
    /**
33
     * @param mixed $key
34
     * @param string $cast
35
     * @return mixed
36
     */
37
    public function cast($key, $cast)
38
    {
39
        return Cast::to($cast, $this->get($key));
40
    }
41
42
    /**
43
     * @param mixed $key
44
     * @param mixed $fallback
45
     * @return mixed
46
     */
47 24
    public function get($key, $fallback = null)
48
    {
49 24
        $value = Arr::get($this->toArray(), $key, $fallback);
50 24
        return isset($fallback)
51 15
            ? Helper::ifEmpty($value, $fallback)
52 24
            : $value;
53
    }
54
55
    /**
56
     * @return bool
57
     */
58
    public function isEmpty()
59
    {
60
        return empty($this->toArray());
61
    }
62
63
    /**
64
     * @return self
65
     */
66 13
    public function merge(array $data = [])
67
    {
68 13
        $storage = wp_parse_args($data, $this->toArray());
69 13
        $this->exchangeArray($storage);
70 13
        return $this;
71
    }
72
73
    /**
74
     * @param mixed $key
75
     * @return mixed
76
     */
77
    #[\ReturnTypeWillChange]
78 15
    public function offsetGet($key)
79
    {
80 15
        return $this->get($key);
81
    }
82
83
    /**
84
     * @param mixed $key
85
     * @return void
86
     */
87
    #[\ReturnTypeWillChange]
88
    public function offsetUnset($key)
89
    {
90
        $storage = $this->toArray();
91
        unset($storage[$key]);
92
        $this->exchangeArray($storage);
93
    }
94
95
    /**
96
     * @param mixed $key
97
     * @param string $sanitizer
98
     * @return mixed
99
     */
100
    public function sanitize($key, $sanitizer)
101
    {
102
        $sanitizers = ['key' => $sanitizer];
103
        $values = ['key' => $this->get($key)];
104
        $values = glsr(Sanitizer::class, compact('values', 'sanitizers'))->run();
105
        return Arr::get($values, 'key');
106
    }
107
108
    /**
109
     * @param string $path
110
     * @param mixed $value
111
     * @return void
112
     */
113 23
    public function set($path, $value)
114
    {
115 23
        $storage = Arr::set($this->toArray(), $path, $value);
116 23
        $this->exchangeArray($storage);
117 23
    }
118
119
    /**
120
     * @return array
121
     */
122 25
    public function toArray()
123
    {
124 25
        return $this->getArrayCopy();
125
    }
126
}
127