Skip to content

Commit

Permalink
[10.x] Add ability to set a custom class for the AsCollection and `…
Browse files Browse the repository at this point in the history
…AsEncryptedCollection` casts (#46619)

* Add ability to set a custom collection class for the `AsCollection` cast

* Fix StyleCI

* Pass all arguments instead of a collection class name

* Ability to set a custom collection class in `AsEncryptedCollection` cast

* Add check for a collection class

* Revert "Add check for a collection class"

This reverts commit 99c1465.

* Switch to PHP 8 constructor property promotion syntax

* Add check for a collection class

* formatting

* Update AsCollection.php

* Update AsEncryptedCollection.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
osbre and taylorotwell authored Apr 3, 2023
1 parent f370e9b commit 5c9656e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/Illuminate/Database/Eloquent/Casts/AsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Support\Collection;
use InvalidArgumentException;

class AsCollection implements Castable
{
Expand All @@ -16,8 +17,12 @@ class AsCollection implements Castable
*/
public static function castUsing(array $arguments)
{
return new class implements CastsAttributes
return new class($arguments) implements CastsAttributes
{
public function __construct(protected array $arguments)
{
}

public function get($model, $key, $value, $attributes)
{
if (! isset($attributes[$key])) {
Expand All @@ -26,7 +31,13 @@ public function get($model, $key, $value, $attributes)

$data = Json::decode($attributes[$key]);

return is_array($data) ? new Collection($data) : null;
$collectionClass = $this->arguments[0] ?? Collection::class;

if (! is_a($collectionClass, Collection::class, true)) {
throw new InvalidArgumentException('The provided class must extend ['.Collection::class.'].');
}

return is_array($data) ? new $collectionClass($data) : null;
}

public function set($model, $key, $value, $attributes)
Expand Down
15 changes: 13 additions & 2 deletions src/Illuminate/Database/Eloquent/Casts/AsEncryptedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Crypt;
use InvalidArgumentException;

class AsEncryptedCollection implements Castable
{
Expand All @@ -17,12 +18,22 @@ class AsEncryptedCollection implements Castable
*/
public static function castUsing(array $arguments)
{
return new class implements CastsAttributes
return new class($arguments) implements CastsAttributes
{
public function __construct(protected array $arguments)
{
}

public function get($model, $key, $value, $attributes)
{
$collectionClass = $this->arguments[0] ?? Collection::class;

if (! is_a($collectionClass, Collection::class, true)) {
throw new InvalidArgumentException('The provided class must extend ['.Collection::class.'].');
}

if (isset($attributes[$key])) {
return new Collection(Json::decode(Crypt::decryptString($attributes[$key])));
return new $collectionClass(Json::decode(Crypt::decryptString($attributes[$key])));
}

return null;
Expand Down

0 comments on commit 5c9656e

Please sign in to comment.