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
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
public function __toString() |
28
|
|
|
{ |
29
|
|
|
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
|
21 |
|
public function get($key, $fallback = null) |
48
|
|
|
{ |
49
|
21 |
|
$value = Arr::get($this->toArray(), $key, $fallback); |
50
|
21 |
|
return isset($fallback) |
51
|
7 |
|
? Helper::ifEmpty($value, $fallback) |
52
|
21 |
|
: $value; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return self |
57
|
|
|
*/ |
58
|
13 |
|
public function merge(array $data = []) |
59
|
|
|
{ |
60
|
13 |
|
$storage = wp_parse_args($data, $this->toArray()); |
61
|
13 |
|
$this->exchangeArray($storage); |
62
|
13 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param mixed $key |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
14 |
|
public function offsetGet($key) |
70
|
|
|
{ |
71
|
14 |
|
return $this->get($key); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param mixed $key |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function offsetUnset($key) |
79
|
|
|
{ |
80
|
|
|
$storage = $this->toArray(); |
81
|
|
|
unset($storage[$key]); |
82
|
|
|
$this->exchangeArray($storage); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $path |
87
|
|
|
* @param mixed $value |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
18 |
|
public function set($path, $value) |
91
|
|
|
{ |
92
|
18 |
|
$storage = Arr::set($this->toArray(), $path, $value); |
93
|
18 |
|
$this->exchangeArray($storage); |
94
|
18 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
22 |
|
public function toArray() |
100
|
|
|
{ |
101
|
22 |
|
return $this->getArrayCopy(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|