Passed
Push — master ( 817cab...57b58d )
by Paul
09:30 queued 02:21
created

Arguments::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Helpers\Cast;
8
9
class Arguments extends \ArrayObject
10
{
11
    /**
12
     * @param mixed $args
13
     */
14 16
    public function __construct($args)
15
    {
16 16
        if ($args instanceof Arguments) {
17 6
            $args = $args->toArray();
18
        } else {
19 16
            $args = Arr::consolidate($args);
20
        }
21 16
        parent::__construct($args, \ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS);
22 16
    }
23
24
    /**
25
     * @param mixed $key
26
     * @param string $cast
27
     * @return mixed
28
     */
29
    public function cast($key, $cast)
30
    {
31
        return Cast::to($cast, $this->get($key));
32
    }
33
34
    /**
35
     * @param mixed $key
36
     * @param mixed $fallback
37
     * @return mixed
38
     */
39 21
    public function get($key, $fallback = null)
40
    {
41 21
        $value = Arr::get($this->toArray(), $key, $fallback);
42 21
        return isset($fallback)
43 7
            ? Helper::ifEmpty($value, $fallback)
44 21
            : $value;
45
    }
46
47
    /**
48
     * @return self
49
     */
50 13
    public function merge(array $data = [])
51
    {
52 13
        $storage = wp_parse_args($data, $this->toArray());
53 13
        $this->exchangeArray($storage);
54 13
        return $this;
55
    }
56
57
    /**
58
     * @param mixed $key
59
     * @return mixed
60
     */
61 14
    public function offsetGet($key)
62
    {
63 14
        return $this->get($key);
64
    }
65
66
    /**
67
     * @param mixed $key
68
     * @return void
69
     */
70
    public function offsetUnset($key)
71
    {
72
        $storage = $this->toArray();
73
        unset($storage[$key]);
74
        $this->exchangeArray($storage);
75
    }
76
77
    /**
78
     * @param string $path
79
     * @param mixed $value
80
     * @return void
81
     */
82 18
    public function set($path, $value)
83
    {
84 18
        $storage = Arr::set($this->toArray(), $path, $value);
85 18
        $this->exchangeArray($storage);
86 18
    }
87
88
    /**
89
     * @return array
90
     */
91 22
    public function toArray()
92
    {
93 22
        return $this->getArrayCopy();
94
    }
95
}
96