Skip to content

Commit

Permalink
[11.x] Fluent::scope/collect methods (#49180)
Browse files Browse the repository at this point in the history
* Add toFluent method

* Add scope and toCollection methods

* Add tests

* Remove toFluent on collections

* Remove unused import

* Remove unused import

* Cast attributes as array in scope instead

* formatting

* Use `value` for `offsetGet` and `__get` methods

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
stevebauman and taylorotwell authored Nov 29, 2023
1 parent e447ce2 commit 5fedaf4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/Illuminate/Support/Fluent.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct($attributes = [])
}

/**
* Get an attribute from the fluent instance.
* Get an attribute from the fluent instance using "dot" notation.
*
* @template TGetDefault
*
Expand All @@ -46,6 +46,18 @@ public function __construct($attributes = [])
* @return TValue|TGetDefault
*/
public function get($key, $default = null)
{
return data_get($this->attributes, $key, $default);
}

/**
* Get an attribute from the fluent instance.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function value($key, $default = null)
{
if (array_key_exists($key, $this->attributes)) {
return $this->attributes[$key];
Expand All @@ -54,6 +66,20 @@ public function get($key, $default = null)
return value($default);
}

/**
* Get the value of the given key as a new Fluent instance.
*
* @param string $key
* @param mixed $default
* @return static
*/
public function scope($key, $default = null)
{
return new static(
(array) $this->get($key, $default)
);
}

/**
* Get the attributes from the fluent instance.
*
Expand All @@ -74,6 +100,16 @@ public function toArray()
return $this->attributes;
}

/**
* Convert the fluent instance to a Collection.
*
* @return \Illuminate\Support\Collection
*/
public function collect()
{
return new Collection($this->attributes);
}

/**
* Convert the object into something JSON serializable.
*
Expand Down Expand Up @@ -114,7 +150,7 @@ public function offsetExists($offset): bool
*/
public function offsetGet($offset): mixed
{
return $this->get($offset);
return $this->value($offset);
}

/**
Expand Down Expand Up @@ -162,7 +198,7 @@ public function __call($method, $parameters)
*/
public function __get($key)
{
return $this->get($key);
return $this->value($key);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/Support/SupportFluentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ public function testToJsonEncodesTheToArrayResult()

$this->assertJsonStringEqualsJsonString(json_encode(['foo']), $results);
}

public function testScope()
{
$fluent = new Fluent(['user' => ['name' => 'taylor']]);
$this->assertEquals(['taylor'], $fluent->scope('user.name')->toArray());
$this->assertEquals(['dayle'], $fluent->scope('user.age', 'dayle')->toArray());

$fluent = new Fluent(['products' => ['forge', 'vapour', 'spark']]);
$this->assertEquals(['forge', 'vapour', 'spark'], $fluent->scope('products')->toArray());
$this->assertEquals(['foo', 'bar'], $fluent->scope('missing', ['foo', 'bar'])->toArray());

$fluent = new Fluent(['authors' => ['taylor' => ['products' => ['forge', 'vapour', 'spark']]]]);
$this->assertEquals(['forge', 'vapour', 'spark'], $fluent->scope('authors.taylor.products')->toArray());
}

public function testToCollection()
{
$fluent = new Fluent(['forge', 'vapour', 'spark']);
$this->assertEquals(['forge', 'vapour', 'spark'], $fluent->collect()->all());
}
}

class FluentArrayIteratorStub implements IteratorAggregate
Expand Down

0 comments on commit 5fedaf4

Please sign in to comment.