Skip to content

Commit 1cd9bf4

Browse files
authored
[11.x] Add Macroable and fill() to Support\Fluent (#54404)
* Add ability to add macros to Fluent instance * Remove accidental imports * Add `fill` method and Macroable to Fluent * Add tests for fill and macroable * CS fix
1 parent 95adbb7 commit 1cd9bf4

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

src/Illuminate/Support/Fluent.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Contracts\Support\Arrayable;
77
use Illuminate\Contracts\Support\Jsonable;
88
use Illuminate\Support\Traits\InteractsWithData;
9+
use Illuminate\Support\Traits\Macroable;
910
use JsonSerializable;
1011

1112
/**
@@ -17,7 +18,9 @@
1718
*/
1819
class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable
1920
{
20-
use InteractsWithData;
21+
use InteractsWithData, Macroable {
22+
__call as macroCall;
23+
}
2124

2225
/**
2326
* All of the attributes set on the fluent instance.
@@ -34,9 +37,7 @@ class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable
3437
*/
3538
public function __construct($attributes = [])
3639
{
37-
foreach ($attributes as $key => $value) {
38-
$this->attributes[$key] = $value;
39-
}
40+
$this->fill($attributes);
4041
}
4142

4243
/**
@@ -67,6 +68,21 @@ public function set($key, $value)
6768
return $this;
6869
}
6970

71+
/**
72+
* Fill the fluent instance with an array of attributes.
73+
*
74+
* @param iterable<TKey, TValue> $attributes
75+
* @return $this
76+
*/
77+
public function fill($attributes)
78+
{
79+
foreach ($attributes as $key => $value) {
80+
$this->attributes[$key] = $value;
81+
}
82+
83+
return $this;
84+
}
85+
7086
/**
7187
* Get an attribute from the fluent instance.
7288
*
@@ -227,6 +243,10 @@ public function offsetUnset($offset): void
227243
*/
228244
public function __call($method, $parameters)
229245
{
246+
if (static::hasMacro($method)) {
247+
return $this->macroCall($method, $parameters);
248+
}
249+
230250
$this->attributes[$method] = count($parameters) > 0 ? reset($parameters) : true;
231251

232252
return $this;

tests/Support/SupportFluentTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,42 @@ public function testEnumsMethod()
416416
$this->assertEquals([TestBackedEnum::B], $fluent->enums('int.b', TestBackedEnum::class));
417417
$this->assertEmpty($fluent->enums('int.doesnt_exist', TestBackedEnum::class));
418418
}
419+
420+
public function testFill()
421+
{
422+
$fluent = new Fluent(['name' => 'John Doe']);
423+
424+
$fluent->fill([
425+
'email' => '[email protected]',
426+
'age' => 30,
427+
]);
428+
429+
$this->assertEquals([
430+
'name' => 'John Doe',
431+
'email' => '[email protected]',
432+
'age' => 30,
433+
], $fluent->getAttributes());
434+
}
435+
436+
public function testMacroable()
437+
{
438+
Fluent::macro('foo', function () {
439+
return $this->fill([
440+
'foo' => 'bar',
441+
'baz' => 'zal',
442+
]);
443+
});
444+
445+
$fluent = new Fluent([
446+
'bee' => 'ser',
447+
]);
448+
449+
$this->assertSame([
450+
'bee' => 'ser',
451+
'foo' => 'bar',
452+
'baz' => 'zal',
453+
], $fluent->foo()->all());
454+
}
419455
}
420456

421457
class FluentArrayIteratorStub implements IteratorAggregate

0 commit comments

Comments
 (0)