diff --git a/src/Illuminate/Support/Fluent.php b/src/Illuminate/Support/Fluent.php index 109ce17336a7..a4f482f31743 100755 --- a/src/Illuminate/Support/Fluent.php +++ b/src/Illuminate/Support/Fluent.php @@ -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 * @@ -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]; @@ -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. * @@ -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. * @@ -114,7 +150,7 @@ public function offsetExists($offset): bool */ public function offsetGet($offset): mixed { - return $this->get($offset); + return $this->value($offset); } /** @@ -162,7 +198,7 @@ public function __call($method, $parameters) */ public function __get($key) { - return $this->get($key); + return $this->value($key); } /** diff --git a/tests/Support/SupportFluentTest.php b/tests/Support/SupportFluentTest.php index 515b4817da10..659472df335b 100755 --- a/tests/Support/SupportFluentTest.php +++ b/tests/Support/SupportFluentTest.php @@ -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