-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
Open
adhikjoshi
wants to merge
9
commits into
hypervel:main
Choose a base branch
from
adhikjoshi:add-Attribute
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4ae30ac
Create Attribute.php
adhikjoshi 86a8d33
added AsCollection and Json Casts
adhikjoshi ed43f56
Create AsArrayObject.php
adhikjoshi 7bc0537
Update src/core/src/Database/Eloquent/Casts/Attribute.php
adhikjoshi 6f4e705
Update src/core/src/Database/Eloquent/Casts/Attribute.php
adhikjoshi d163102
Update src/core/src/Database/Eloquent/Casts/Json.php
adhikjoshi 625590d
Update src/core/src/Database/Eloquent/Casts/AsCollection.php
adhikjoshi 73847f6
Update src/core/src/Database/Eloquent/Casts/AsArrayObject.php
adhikjoshi cce0afe
fixed CI/CD
adhikjoshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Hypervel\Database\Eloquent\Casts; | ||
|
|
||
| use ArrayObject; | ||
| use Hyperf\Contract\CastsAttributes; | ||
| use Hypervel\Foundation\Http\Contracts\Castable; | ||
|
|
||
| class AsArrayObject implements Castable | ||
| { | ||
| /** | ||
| * Get the caster class to use when casting from / to this cast target. | ||
| * | ||
| * @return \Hyperf\Contract\CastsAttributes<ArrayObject<array-key, mixed>, iterable> | ||
| */ | ||
| public static function castUsing(array $arguments = []): CastsAttributes | ||
| { | ||
| 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(); | ||
| } | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Hypervel\Database\Eloquent\Casts; | ||
|
|
||
| use Hyperf\Contract\CastsAttributes; | ||
| use Hypervel\Foundation\Http\Contracts\Castable; | ||
| use Hypervel\Support\Collection; | ||
| use Hypervel\Support\Str; | ||
| use InvalidArgumentException; | ||
|
|
||
| class AsCollection implements Castable | ||
| { | ||
| /** | ||
| * Get the caster class to use when casting from / to this cast target. | ||
| * | ||
| * @return \Hyperf\Contract\CastsAttributes<\Hypervel\Support\Collection<array-key, mixed>, iterable> | ||
| */ | ||
| public static function castUsing(array $arguments = []): CastsAttributes | ||
| { | ||
| 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]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Hypervel\Database\Eloquent\Casts; | ||
|
|
||
| class Attribute | ||
| { | ||
| /** | ||
| * The attribute accessor. | ||
| * | ||
| * @var null|callable | ||
| */ | ||
| public $get; | ||
|
|
||
| /** | ||
| * The attribute mutator. | ||
| * | ||
| * @var null|callable | ||
| */ | ||
| 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. | ||
| */ | ||
| public function __construct(?callable $get = null, ?callable $set = null) | ||
| { | ||
| $this->get = $get; | ||
| $this->set = $set; | ||
| } | ||
|
|
||
| /** | ||
| * Create a new attribute accessor / mutator. | ||
| */ | ||
| public static function make(?callable $get = null, ?callable $set = null): static | ||
| { | ||
| return new static($get, $set); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new attribute accessor. | ||
| * | ||
| * @return static | ||
| */ | ||
| public static function get(callable $get) | ||
| { | ||
| return new static($get); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new attribute mutator. | ||
| * | ||
| * @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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 null|callable | ||
| */ | ||
| protected static $encoder; | ||
|
|
||
| /** | ||
| * The custom JSON decoder. | ||
| * | ||
| * @var null|callable | ||
| */ | ||
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.