-
-
Notifications
You must be signed in to change notification settings - Fork 13
Draft: migrate more Eloquent Casts #266
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
4ae30ac
86a8d33
ed43f56
7bc0537
6f4e705
d163102
625590d
73847f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Hypervel\Database\Eloquent\Casts; | ||
|
|
||
| use ArrayObject; | ||
| use Hypervel\Foundation\Http\Contracts\Castable; | ||
| use Hyperf\Contract\CastsAttributes; | ||
|
|
||
| class AsArrayObject implements Castable | ||
| { | ||
| /** | ||
| * Get the caster class to use when casting from / to this cast target. | ||
| * | ||
| * @param array $arguments | ||
| * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Database\Eloquent\Casts\ArrayObject<array-key, mixed>, iterable> | ||
| */ | ||
| public static function castUsing(array $arguments = []): CastsAttributes | ||
|
Check failure on line 19 in src/core/src/Database/Eloquent/Casts/AsArrayObject.php
|
||
| { | ||
| return new class implements CastsAttributes | ||
| { | ||
| public function get($model, $key, $value, $attributes) | ||
| { | ||
| if (! isset($attributes[$key])) { | ||
| return; | ||
| } | ||
|
|
||
| $data = Json::decode($attributes[$key]); | ||
|
|
||
| return is_array($data) ? new ArrayObject($data, ArrayObject::ARRAY_AS_PROPS) : null; | ||
| } | ||
|
|
||
| public function set($model, $key, $value, $attributes) | ||
| { | ||
| return [$key => Json::encode($value)]; | ||
| } | ||
|
|
||
| public function serialize($model, string $key, $value, array $attributes) | ||
| { | ||
| return $value->getArrayCopy(); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Hypervel\Database\Eloquent\Casts; | ||
|
|
||
| use Hyperf\Contract\CastsAttributes; | ||
| use Hypervel\Support\Collection; | ||
| use Hypervel\Support\Str; | ||
| use InvalidArgumentException; | ||
| use Hypervel\Foundation\Http\Contracts\Castable; | ||
|
|
||
| class AsCollection implements Castable | ||
| { | ||
| /** | ||
| * Get the caster class to use when casting from / to this cast target. | ||
| * | ||
| * @param array $arguments | ||
| * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Support\Collection<array-key, mixed>, iterable> | ||
adhikjoshi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| public static function castUsing(array $arguments = []): CastsAttributes | ||
|
Check failure on line 21 in src/core/src/Database/Eloquent/Casts/AsCollection.php
|
||
| { | ||
| return new class($arguments) implements CastsAttributes | ||
| { | ||
| public function __construct(protected array $arguments) | ||
| { | ||
| $this->arguments = array_pad(array_values($this->arguments), 2, ''); | ||
| } | ||
|
|
||
| public function get($model, $key, $value, $attributes) | ||
| { | ||
| if (! isset($attributes[$key])) { | ||
| return; | ||
| } | ||
|
|
||
| $data = Json::decode($attributes[$key]); | ||
|
|
||
| $collectionClass = empty($this->arguments[0]) ? Collection::class : $this->arguments[0]; | ||
|
|
||
| if (! is_a($collectionClass, Collection::class, true)) { | ||
| throw new InvalidArgumentException('The provided class must extend [' . Collection::class . '].'); | ||
| } | ||
|
|
||
| if (! is_array($data)) { | ||
| return null; | ||
| } | ||
|
|
||
| $instance = new $collectionClass($data); | ||
|
|
||
| if (! isset($this->arguments[1]) || ! $this->arguments[1]) { | ||
| return $instance; | ||
| } | ||
|
|
||
| if (is_string($this->arguments[1])) { | ||
| $this->arguments[1] = Str::parseCallback($this->arguments[1]); | ||
| } | ||
|
|
||
| return is_callable($this->arguments[1]) | ||
| ? $instance->map($this->arguments[1]) | ||
| : $instance->mapInto($this->arguments[1][0]); | ||
| } | ||
|
|
||
| public function set($model, $key, $value, $attributes) | ||
| { | ||
| return [$key => Json::encode($value)]; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Specify the type of object each item in the collection should be mapped to. | ||
| * | ||
| * @param array{class-string, string}|class-string $map | ||
| * @return string | ||
| */ | ||
| public static function of($map) | ||
| { | ||
| return static::using('', $map); | ||
| } | ||
|
|
||
| /** | ||
| * Specify the collection type for the cast. | ||
| * | ||
| * @param class-string $class | ||
| * @param array{class-string, string}|class-string $map | ||
| * @return string | ||
| */ | ||
| public static function using($class, $map = null) | ||
| { | ||
| if (is_array($map) && is_callable($map)) { | ||
| $map = $map[0] . '@' . $map[1]; | ||
| } | ||
|
|
||
| return static::class . ':' . implode(',', [$class, $map]); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Hypervel\Database\Eloquent\Casts; | ||
|
|
||
| class Attribute | ||
| { | ||
| /** | ||
| * The attribute accessor. | ||
| * | ||
| * @var callable | ||
adhikjoshi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| public $get; | ||
|
|
||
| /** | ||
| * The attribute mutator. | ||
| * | ||
| * @var callable | ||
adhikjoshi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| public $set; | ||
|
|
||
| /** | ||
| * Indicates if caching is enabled for this attribute. | ||
| * | ||
| * @var bool | ||
| */ | ||
| public $withCaching = false; | ||
|
|
||
| /** | ||
| * Indicates if caching of objects is enabled for this attribute. | ||
| * | ||
| * @var bool | ||
| */ | ||
| public $withObjectCaching = true; | ||
|
|
||
| /** | ||
| * Create a new attribute accessor / mutator. | ||
| * | ||
| * @param callable|null $get | ||
| * @param callable|null $set | ||
| */ | ||
| public function __construct(?callable $get = null, ?callable $set = null) | ||
| { | ||
| $this->get = $get; | ||
| $this->set = $set; | ||
| } | ||
|
|
||
| /** | ||
| * Create a new attribute accessor / mutator. | ||
| * | ||
| * @param callable|null $get | ||
| * @param callable|null $set | ||
| * @return static | ||
| */ | ||
| public static function make(?callable $get = null, ?callable $set = null): static | ||
| { | ||
| return new static($get, $set); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new attribute accessor. | ||
| * | ||
| * @param callable $get | ||
| * @return static | ||
| */ | ||
| public static function get(callable $get) | ||
| { | ||
| return new static($get); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new attribute mutator. | ||
| * | ||
| * @param callable $set | ||
| * @return static | ||
| */ | ||
| public static function set(callable $set) | ||
| { | ||
| return new static(null, $set); | ||
| } | ||
|
|
||
| /** | ||
| * Disable object caching for the attribute. | ||
| * | ||
| * @return static | ||
| */ | ||
| public function withoutObjectCaching() | ||
| { | ||
| $this->withObjectCaching = false; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Enable caching for the attribute. | ||
| * | ||
| * @return static | ||
| */ | ||
| public function shouldCache() | ||
| { | ||
| $this->withCaching = true; | ||
|
|
||
| return $this; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Hypervel\Database\Eloquent\Casts; | ||
|
|
||
| class Json | ||
| { | ||
| /** | ||
| * The custom JSON encoder. | ||
| * | ||
| * @var callable|null | ||
| */ | ||
| protected static $encoder; | ||
|
|
||
| /** | ||
| * The custom JSON decode. | ||
adhikjoshi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @var callable|null | ||
| */ | ||
| protected static $decoder; | ||
|
|
||
| /** | ||
| * Encode the given value. | ||
| */ | ||
| public static function encode(mixed $value, int $flags = 0): mixed | ||
| { | ||
| return isset(static::$encoder) | ||
| ? (static::$encoder)($value, $flags) | ||
| : json_encode($value, $flags); | ||
| } | ||
|
|
||
| /** | ||
| * Decode the given value. | ||
| */ | ||
| public static function decode(mixed $value, ?bool $associative = true): mixed | ||
| { | ||
| return isset(static::$decoder) | ||
| ? (static::$decoder)($value, $associative) | ||
| : json_decode($value, $associative); | ||
| } | ||
|
|
||
| /** | ||
| * Encode all values using the given callable. | ||
| */ | ||
| public static function encodeUsing(?callable $encoder): void | ||
| { | ||
| static::$encoder = $encoder; | ||
| } | ||
|
|
||
| /** | ||
| * Decode all values using the given callable. | ||
| */ | ||
| public static function decodeUsing(?callable $decoder): void | ||
| { | ||
| static::$decoder = $decoder; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,10 +4,12 @@ | |
|
|
||
| namespace Hypervel\Foundation\Http\Contracts; | ||
|
|
||
| use Hyperf\Contract\CastsAttributes; | ||
|
|
||
| interface Castable | ||
| { | ||
| /** | ||
| * Get the name of the caster class to use when casting from / to this cast target. | ||
| */ | ||
| public static function castUsing(array $arguments = []): CastInputs|string; | ||
| public static function castUsing(array $arguments = []): CastInputs|CastsAttributes|string; | ||
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.