1515use ArrayAccess ;
1616use ArrayIterator ;
1717use Closure ;
18+ use Hyperf \Collection \Arr ;
1819use Hyperf \Contract \Arrayable ;
1920use Hyperf \Contract \Jsonable ;
2021use Hyperf \Macroable \Macroable ;
22+ use Hyperf \Support \Traits \InteractsWithData ;
2123use IteratorAggregate ;
2224use JsonSerializable ;
2325use Traversable ;
2426
27+ use function Hyperf \Collection \data_get ;
28+ use function Hyperf \Collection \data_set ;
29+
2530/**
2631 * Most of the methods in this file come from illuminate/support,
2732 * thanks Laravel Team provide such a useful class.
3439 */
3540class Fluent implements ArrayAccess, Arrayable, IteratorAggregate, Jsonable, JsonSerializable
3641{
42+ use InteractsWithData;
3743 use Macroable{
3844 __call as macroCall;
3945 }
@@ -52,9 +58,7 @@ class Fluent implements ArrayAccess, Arrayable, IteratorAggregate, Jsonable, Jso
5258 */
5359 public function __construct ($ attributes = [])
5460 {
55- foreach ($ attributes as $ key => $ value ) {
56- $ this ->attributes [$ key ] = $ value ;
57- }
61+ $ this ->fill ($ attributes );
5862 }
5963
6064 /**
@@ -123,6 +127,29 @@ public function __toString(): string
123127 return $ this ->toJson ();
124128 }
125129
130+ /**
131+ * Get all of the attributes from the fluent instance.
132+ *
133+ * @param null|array|mixed $keys
134+ * @return array
135+ */
136+ public function all ($ keys = null )
137+ {
138+ $ data = $ this ->data ();
139+
140+ if (! $ keys ) {
141+ return $ data ;
142+ }
143+
144+ $ results = [];
145+
146+ foreach (is_array ($ keys ) ? $ keys : func_get_args () as $ key ) {
147+ Arr::set ($ results , $ key , Arr::get ($ data , $ key ));
148+ }
149+
150+ return $ results ;
151+ }
152+
126153 /**
127154 * Get an attribute from the fluent instance.
128155 *
@@ -133,6 +160,47 @@ public function __toString(): string
133160 * @return TGetDefault|TValue
134161 */
135162 public function get ($ key , $ default = null )
163+ {
164+ return data_get ($ this ->attributes , $ key , $ default );
165+ }
166+
167+ /**
168+ * Set an attribute on the fluent instance using "dot" notation.
169+ *
170+ * @param TKey $key
171+ * @param TValue $value
172+ * @return $this
173+ */
174+ public function set ($ key , $ value )
175+ {
176+ data_set ($ this ->attributes , $ key , $ value );
177+
178+ return $ this ;
179+ }
180+
181+ /**
182+ * Fill the fluent instance with an array of attributes.
183+ *
184+ * @param iterable<TKey, TValue> $attributes
185+ * @return $this
186+ */
187+ public function fill ($ attributes )
188+ {
189+ foreach ($ attributes as $ key => $ value ) {
190+ $ this ->attributes [$ key ] = $ value ;
191+ }
192+
193+ return $ this ;
194+ }
195+
196+ /**
197+ * Get an attribute from the fluent instance.
198+ *
199+ * @param string $key
200+ * @param mixed $default
201+ * @return mixed
202+ */
203+ public function value ($ key , $ default = null )
136204 {
137205 if (array_key_exists ($ key , $ this ->attributes )) {
138206 return $ this ->attributes [$ key ];
@@ -141,6 +209,20 @@ public function get($key, $default = null)
141209 return value ($ default );
142210 }
143211
212+ /**
213+ * Get the value of the given key as a new Fluent instance.
214+ *
215+ * @param string $key
216+ * @param mixed $default
217+ * @return static
218+ */
219+ public function scope ($ key , $ default = null )
220+ {
221+ return new static (
222+ (array ) $ this ->get ($ key , $ default )
223+ );
224+ }
225+
144226 /**
145227 * Get the attributes from the fluent instance.
146228 *
@@ -253,4 +335,16 @@ public function getIterator(): Traversable
253335 {
254336 return new ArrayIterator ($ this ->attributes );
255337 }
338+
339+ /**
340+ * Get data from the fluent instance.
341+ *
342+ * @param string $key
343+ * @param mixed $default
344+ * @return mixed
345+ */
346+ protected function data ($ key = null , $ default = null )
347+ {
348+ return $ this ->get ($ key , $ default );
349+ }
256350}
0 commit comments