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

[10.x] Add ability to set a custom class for the AsCollection and AsEncryptedCollection casts #46619

Merged
merged 11 commits into from
Apr 3, 2023
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;
osbre marked this conversation as resolved.
Show resolved Hide resolved

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;
osbre marked this conversation as resolved.
Show resolved Hide resolved

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