1414
1515use ArrayAccess ;
1616use Closure ;
17+ use Hyperf \Collection \Arr ;
1718use Hyperf \Contract \Arrayable ;
1819use Hyperf \Contract \Jsonable ;
20+ use Hyperf \Macroable \Macroable ;
1921use JsonSerializable ;
2022
23+ use function Hyperf \Collection \data_get ;
24+ use function Hyperf \Collection \data_set ;
25+
2126/**
2227 * Most of the methods in this file come from illuminate/support,
2328 * thanks Laravel Team provide such a useful class.
3035 */
3136class Fluent implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
3237{
38+ use Traits \InteractsWithData;
39+ use Macroable{
40+ __call as macroCall;
41+ }
42+
3343 /**
3444 * All the attributes set on the fluent instance.
3545 *
@@ -44,9 +54,7 @@ class Fluent implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
4454 */
4555 public function __construct ($ attributes = [])
4656 {
47- foreach ($ attributes as $ key => $ value ) {
48- $ this ->attributes [$ key ] = $ value ;
49- }
57+ $ this ->fill ($ attributes );
5058 }
5159
5260 /**
@@ -58,6 +66,10 @@ public function __construct($attributes = [])
5866 */
5967 public function __call ($ method , $ parameters )
6068 {
69+ if (static ::hasMacro ($ method )) {
70+ return $ this ->macroCall ($ method , $ parameters );
71+ }
72+
6173 $ this ->attributes [$ method ] = count ($ parameters ) > 0 ? $ parameters [0 ] : true ;
6274
6375 return $ this ;
@@ -121,6 +133,20 @@ public function __toString(): string
121133 * @return TGetDefault|TValue
122134 */
123135 public function get ($ key , $ default = null )
136+ {
137+ return data_get ($ this ->attributes , $ key , $ default );
138+ }
139+
140+ /**
141+ * Get an attribute from the fluent instance.
142+ *
143+ * @template TGetDefault
144+ *
145+ * @param TKey $key
146+ * @param (Closure(): TGetDefault)|TGetDefault $default
147+ * @return TGetDefault|TValue
148+ */
149+ public function value ($ key , $ default = null )
124150 {
125151 if (array_key_exists ($ key , $ this ->attributes )) {
126152 return $ this ->attributes [$ key ];
@@ -129,6 +155,68 @@ public function get($key, $default = null)
129155 return value ($ default );
130156 }
131157
158+ /**
159+ * Set an attribute on the fluent instance using "dot" notation.
160+ *
161+ * @param TKey $key
162+ * @param TValue $value
163+ * @return $this
164+ */
165+ public function set ($ key , $ value )
166+ {
167+ data_set ($ this ->attributes , $ key , $ value );
168+
169+ return $ this ;
170+ }
171+
172+ /**
173+ * Fill the fluent instance with an array of attributes.
174+ *
175+ * @param iterable<TKey, TValue> $attributes
176+ * @return $this
177+ */
178+ public function fill ($ attributes )
179+ {
180+ foreach ($ attributes as $ key => $ value ) {
181+ $ this ->attributes [$ key ] = $ value ;
182+ }
183+
184+ return $ this ;
185+ }
186+
187+ /**
188+ * Get the value of the given key as a new Fluent instance.
189+ *
190+ * @param string $key
191+ * @param mixed $default
192+ * @return static
193+ */
194+ public function scope ($ key , $ default = null )
195+ {
196+ return new static (
197+ (array ) $ this ->get ($ key , $ default )
198+ );
199+ }
200+
201+ /**
202+ * Get all of the attributes from the fluent instance.
203+ *
204+ * @param null|array|mixed $keys
205+ * @return array
206+ */
207+ public function all ($ keys = null )
208+ {
209+ $ data = $ this ->data ();
210+ if (! $ keys ) {
211+ return $ data ;
212+ }
213+ $ results = [];
214+ foreach (is_array ($ keys ) ? $ keys : func_get_args () as $ key ) {
215+ Arr::set ($ results , $ key , Arr::get ($ data , $ key ));
216+ }
217+ return $ results ;
218+ }
219+
132220 /**
133221 * Get the attributes from the fluent instance.
134222 *
@@ -211,4 +299,16 @@ public function offsetUnset(mixed $offset): void
211299 {
212300 unset($ this ->attributes [$ offset ]);
213301 }
302+
303+ /**
304+ * Get data from the fluent instance.
305+ *
306+ * @param string $key
307+ * @param mixed $default
308+ * @return mixed
309+ */
310+ protected function data ($ key = null , $ default = null )
311+ {
312+ return $ this ->get ($ key , $ default );
313+ }
214314}
0 commit comments