Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array access for higher-order collection operations #16313

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions src/Illuminate/Support/HigherOrderCollectionProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Illuminate\Support;

class HigherOrderCollectionProxy
use ArrayAccess;

class HigherOrderCollectionProxy implements ArrayAccess
{
/**
* The collection being operated on.
Expand Down Expand Up @@ -40,7 +42,7 @@ public function __construct(Collection $collection, $method)
public function __get($key)
{
return $this->collection->{$this->method}(function ($value) use ($key) {
return is_array($value) ? $value[$key] : $value->{$key};
return $value->{$key};
});
}

Expand All @@ -57,4 +59,57 @@ public function __call($method, $parameters)
return $value->{$method}(...$parameters);
});
}

/**
* Get an item at a given offset.
*
* @param mixed $key
* @return mixed
*/
public function offsetGet($key)
{
return $this->collection->{$this->method}(function ($value) use ($key) {
return $value[$key];
});
}

/**
* Determine if an item exists at an offset.
*
* @param mixed $key
* @return bool
*/
public function offsetExists($key)
{
throw new Exception(
__CLASS__.' does not support array access other than retrieving array elements.'
);
}

/**
* Set the item at a given offset.
*
* @param mixed $key
* @param mixed $value
* @return void
*/
public function offsetSet($key, $value)
{
throw new Exception(
__CLASS__.' does not support array access other than retrieving array elements.'
);
}

/**
* Unset the item at a given offset.
*
* @param string $key
* @return void
*/
public function offsetUnset($key)
{
throw new Exception(
__CLASS__.' does not support array access other than retrieving array elements.'
);
}
}
6 changes: 1 addition & 5 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1662,11 +1662,7 @@ public function testHigherOrderCollectionMapFromArrays()

$collection = collect([$person1, $person2]);

$this->assertEquals(['Taylor', 'Yaz'], $collection->map->name->toArray());

$collection = collect([new TestSupportCollectionHigherOrderItem, new TestSupportCollectionHigherOrderItem]);

$this->assertEquals(['TAYLOR', 'TAYLOR'], $collection->each->uppercase()->map->name->toArray());
$this->assertEquals(['Taylor', 'Yaz'], $collection->map['name']->toArray());
}
}

Expand Down