1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ArinaSystems\JsonResponse; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
class Attribute |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \ArinaSystems\JsonResponse\Option |
12
|
|
|
*/ |
13
|
|
|
protected $options; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $attributes = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Create a new instance. |
22
|
|
|
* |
23
|
|
|
* @param \ArinaSystems\JsonResponse\Option $options |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Option $options) |
26
|
|
|
{ |
27
|
|
|
$this->parse($options); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get an attribute from an array using "dot" notation. |
32
|
|
|
* |
33
|
|
|
* @param string $key |
34
|
|
|
* @param mixed $default |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
public function get(string $key, $default = null) |
38
|
|
|
{ |
39
|
|
|
return Arr::get($this->attributes, $key.'.value', $default); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set a attribute to a given value using "dot" notation. |
44
|
|
|
* |
45
|
|
|
* @param string|array $keys |
46
|
|
|
* @param null|mixed $value |
47
|
|
|
* @return self |
48
|
|
|
*/ |
49
|
|
|
public function set($keys, $value = null) |
50
|
|
|
{ |
51
|
|
|
if (is_array($keys)) { |
52
|
|
|
foreach ($keys as $key => $value) { |
53
|
|
|
$this->set($key, $value); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (! is_string($keys)) { |
|
|
|
|
60
|
|
|
throw new InvalidArgumentException('$key must be a string or array.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$value = $this->build($keys, $value); |
64
|
|
|
|
65
|
|
|
Arr::set($this->attributes, $keys.'.value', $value); |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Retrieve all response attributes. |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function all(string $value = 'value') |
76
|
|
|
{ |
77
|
|
|
$attributes = $this->attributes; |
78
|
|
|
|
79
|
|
|
foreach ($attributes as $attribute => $options) { |
80
|
|
|
$attributes[$attribute] = Arr::get($options, $value); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $attributes; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Build the value of attribute. |
88
|
|
|
* |
89
|
|
|
* @param string $attribute |
90
|
|
|
* @param mixed $value |
91
|
|
|
* @param mixed|null $builder |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
public function build(string $attribute, $value, $builder = null) |
95
|
|
|
{ |
96
|
|
|
if (is_null($builder)) { |
97
|
|
|
$builder = $this->builderOf($attribute); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (is_string($builder) && class_exists($builder)) { |
101
|
|
|
$builder = new $builder($this->options, $this); |
102
|
|
|
|
103
|
|
|
return $builder->build($value); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $value; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the attribute's value builder. |
111
|
|
|
* |
112
|
|
|
* @param string $attribute |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
|
|
protected function builderOf(string $attribute) |
116
|
|
|
{ |
117
|
|
|
return Arr::get($this->options->builders(), $attribute); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Parsing the attributes from the given options. |
122
|
|
|
* |
123
|
|
|
* @param \ArinaSystems\JsonResponse\Option $options |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
protected function parse(Option $options): void |
127
|
|
|
{ |
128
|
|
|
$this->options = $options; |
129
|
|
|
$this->attributes = (array) $options->get('attributes'); |
130
|
|
|
$this->set($this->all()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Dynamically set attribute on the response attributes object. |
135
|
|
|
* |
136
|
|
|
* @param string $key |
137
|
|
|
* @param mixed $value |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
public function __set(string $key, $value) |
141
|
|
|
{ |
142
|
|
|
$this->set($key, $value); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Dynamically retrieve attribute on the response attributes object. |
147
|
|
|
* |
148
|
|
|
* @param string $key |
149
|
|
|
* @return mixed |
150
|
|
|
*/ |
151
|
|
|
public function __get($key) |
152
|
|
|
{ |
153
|
|
|
return $this->get($key); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get an instance of attribute object. |
158
|
|
|
* |
159
|
|
|
* @return self |
160
|
|
|
*/ |
161
|
|
|
public function instance() |
162
|
|
|
{ |
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|