From 4ae30ac3b33e315f3e8176a5203905984edd280b Mon Sep 17 00:00:00 2001 From: Adhik Joshi Date: Tue, 4 Nov 2025 14:33:06 +0530 Subject: [PATCH 1/9] Create Attribute.php --- .../src/Database/Eloquent/Casts/Attribute.php | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/core/src/Database/Eloquent/Casts/Attribute.php diff --git a/src/core/src/Database/Eloquent/Casts/Attribute.php b/src/core/src/Database/Eloquent/Casts/Attribute.php new file mode 100644 index 00000000..430f1c54 --- /dev/null +++ b/src/core/src/Database/Eloquent/Casts/Attribute.php @@ -0,0 +1,106 @@ +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; + } +} From 86a8d3375c24b808c88d4ece77e03a39ff4ad9de Mon Sep 17 00:00:00 2001 From: Adhik Joshi Date: Tue, 4 Nov 2025 14:58:42 +0530 Subject: [PATCH 2/9] added AsCollection and Json Casts --- .../Database/Eloquent/Casts/AsCollection.php | 96 +++++++++++++++++++ src/core/src/Database/Eloquent/Casts/Json.php | 58 +++++++++++ .../src/Http/Contracts/Castable.php | 4 +- 3 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 src/core/src/Database/Eloquent/Casts/AsCollection.php create mode 100644 src/core/src/Database/Eloquent/Casts/Json.php diff --git a/src/core/src/Database/Eloquent/Casts/AsCollection.php b/src/core/src/Database/Eloquent/Casts/AsCollection.php new file mode 100644 index 00000000..070531f6 --- /dev/null +++ b/src/core/src/Database/Eloquent/Casts/AsCollection.php @@ -0,0 +1,96 @@ +, 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]); + } +} diff --git a/src/core/src/Database/Eloquent/Casts/Json.php b/src/core/src/Database/Eloquent/Casts/Json.php new file mode 100644 index 00000000..c7f56a95 --- /dev/null +++ b/src/core/src/Database/Eloquent/Casts/Json.php @@ -0,0 +1,58 @@ + Date: Tue, 4 Nov 2025 15:03:56 +0530 Subject: [PATCH 3/9] Create AsArrayObject.php --- .../Database/Eloquent/Casts/AsArrayObject.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/core/src/Database/Eloquent/Casts/AsArrayObject.php diff --git a/src/core/src/Database/Eloquent/Casts/AsArrayObject.php b/src/core/src/Database/Eloquent/Casts/AsArrayObject.php new file mode 100644 index 00000000..c74afb31 --- /dev/null +++ b/src/core/src/Database/Eloquent/Casts/AsArrayObject.php @@ -0,0 +1,45 @@ +, 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(); + } + }; + } +} From 7bc0537da88cbf0d78301a523edacec38e4fd9ae Mon Sep 17 00:00:00 2001 From: Adhik Joshi Date: Sun, 9 Nov 2025 17:56:55 +0530 Subject: [PATCH 4/9] Update src/core/src/Database/Eloquent/Casts/Attribute.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/core/src/Database/Eloquent/Casts/Attribute.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/src/Database/Eloquent/Casts/Attribute.php b/src/core/src/Database/Eloquent/Casts/Attribute.php index 430f1c54..2e085440 100644 --- a/src/core/src/Database/Eloquent/Casts/Attribute.php +++ b/src/core/src/Database/Eloquent/Casts/Attribute.php @@ -9,7 +9,7 @@ class Attribute /** * The attribute accessor. * - * @var callable + * @var callable|null */ public $get; From 6f4e70595ea56c5dbcfbd6573bf7e3705e9c1aad Mon Sep 17 00:00:00 2001 From: Adhik Joshi Date: Sun, 9 Nov 2025 17:57:32 +0530 Subject: [PATCH 5/9] Update src/core/src/Database/Eloquent/Casts/Attribute.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/core/src/Database/Eloquent/Casts/Attribute.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/src/Database/Eloquent/Casts/Attribute.php b/src/core/src/Database/Eloquent/Casts/Attribute.php index 2e085440..c410419e 100644 --- a/src/core/src/Database/Eloquent/Casts/Attribute.php +++ b/src/core/src/Database/Eloquent/Casts/Attribute.php @@ -16,7 +16,7 @@ class Attribute /** * The attribute mutator. * - * @var callable + * @var callable|null */ public $set; From d1631028bae8632c90228a3d279d55f95438b2cb Mon Sep 17 00:00:00 2001 From: Adhik Joshi Date: Sun, 9 Nov 2025 17:58:18 +0530 Subject: [PATCH 6/9] Update src/core/src/Database/Eloquent/Casts/Json.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/core/src/Database/Eloquent/Casts/Json.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/src/Database/Eloquent/Casts/Json.php b/src/core/src/Database/Eloquent/Casts/Json.php index c7f56a95..06706aaf 100644 --- a/src/core/src/Database/Eloquent/Casts/Json.php +++ b/src/core/src/Database/Eloquent/Casts/Json.php @@ -14,7 +14,7 @@ class Json protected static $encoder; /** - * The custom JSON decode. + * The custom JSON decoder. * * @var callable|null */ From 625590d95110b49db98fd9db6bc80a4ea77791b6 Mon Sep 17 00:00:00 2001 From: Adhik Joshi Date: Sun, 9 Nov 2025 18:02:19 +0530 Subject: [PATCH 7/9] Update src/core/src/Database/Eloquent/Casts/AsCollection.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/core/src/Database/Eloquent/Casts/AsCollection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/src/Database/Eloquent/Casts/AsCollection.php b/src/core/src/Database/Eloquent/Casts/AsCollection.php index 070531f6..14b9de0c 100644 --- a/src/core/src/Database/Eloquent/Casts/AsCollection.php +++ b/src/core/src/Database/Eloquent/Casts/AsCollection.php @@ -16,7 +16,7 @@ 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, iterable> + * @return \Hyperf\Contract\CastsAttributes<\Hypervel\Support\Collection, iterable> */ public static function castUsing(array $arguments = []): CastsAttributes { From 73847f6717e58bfa750a1655bea34f5687ab56df Mon Sep 17 00:00:00 2001 From: Adhik Joshi Date: Sun, 9 Nov 2025 18:02:29 +0530 Subject: [PATCH 8/9] Update src/core/src/Database/Eloquent/Casts/AsArrayObject.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/core/src/Database/Eloquent/Casts/AsArrayObject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/src/Database/Eloquent/Casts/AsArrayObject.php b/src/core/src/Database/Eloquent/Casts/AsArrayObject.php index c74afb31..c1f11e23 100644 --- a/src/core/src/Database/Eloquent/Casts/AsArrayObject.php +++ b/src/core/src/Database/Eloquent/Casts/AsArrayObject.php @@ -14,7 +14,7 @@ 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, iterable> + * @return \Hyperf\Contract\CastsAttributes<\ArrayObject, iterable> */ public static function castUsing(array $arguments = []): CastsAttributes { From cce0afe2591f1d65e0ee768fa79000795e3a7d39 Mon Sep 17 00:00:00 2001 From: Adhik Joshi Date: Mon, 24 Nov 2025 11:28:18 +0530 Subject: [PATCH 9/9] fixed CI/CD --- .../src/Database/Eloquent/Casts/AsArrayObject.php | 8 +++----- .../src/Database/Eloquent/Casts/AsCollection.php | 12 +++++------- src/core/src/Database/Eloquent/Casts/Attribute.php | 13 ++----------- src/core/src/Database/Eloquent/Casts/Json.php | 4 ++-- 4 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/core/src/Database/Eloquent/Casts/AsArrayObject.php b/src/core/src/Database/Eloquent/Casts/AsArrayObject.php index c1f11e23..8c4d1329 100644 --- a/src/core/src/Database/Eloquent/Casts/AsArrayObject.php +++ b/src/core/src/Database/Eloquent/Casts/AsArrayObject.php @@ -5,21 +5,19 @@ namespace Hypervel\Database\Eloquent\Casts; use ArrayObject; -use Hypervel\Foundation\Http\Contracts\Castable; 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. * - * @param array $arguments - * @return \Hyperf\Contract\CastsAttributes<\ArrayObject, iterable> + * @return \Hyperf\Contract\CastsAttributes, iterable> */ public static function castUsing(array $arguments = []): CastsAttributes { - return new class implements CastsAttributes - { + return new class implements CastsAttributes { public function get($model, $key, $value, $attributes) { if (! isset($attributes[$key])) { diff --git a/src/core/src/Database/Eloquent/Casts/AsCollection.php b/src/core/src/Database/Eloquent/Casts/AsCollection.php index 14b9de0c..41ca1a4c 100644 --- a/src/core/src/Database/Eloquent/Casts/AsCollection.php +++ b/src/core/src/Database/Eloquent/Casts/AsCollection.php @@ -5,23 +5,21 @@ 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; -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 \Hyperf\Contract\CastsAttributes<\Hypervel\Support\Collection, iterable> */ public static function castUsing(array $arguments = []): CastsAttributes { - return new class($arguments) implements CastsAttributes - { + return new class($arguments) implements CastsAttributes { public function __construct(protected array $arguments) { $this->arguments = array_pad(array_values($this->arguments), 2, ''); @@ -70,7 +68,7 @@ public function set($model, $key, $value, $attributes) /** * Specify the type of object each item in the collection should be mapped to. * - * @param array{class-string, string}|class-string $map + * @param array{class-string, string}|class-string $map * @return string */ public static function of($map) @@ -81,8 +79,8 @@ public static function of($map) /** * Specify the collection type for the cast. * - * @param class-string $class - * @param array{class-string, string}|class-string $map + * @param class-string $class + * @param array{class-string, string}|class-string $map * @return string */ public static function using($class, $map = null) diff --git a/src/core/src/Database/Eloquent/Casts/Attribute.php b/src/core/src/Database/Eloquent/Casts/Attribute.php index c410419e..9e620b1a 100644 --- a/src/core/src/Database/Eloquent/Casts/Attribute.php +++ b/src/core/src/Database/Eloquent/Casts/Attribute.php @@ -9,14 +9,14 @@ class Attribute /** * The attribute accessor. * - * @var callable|null + * @var null|callable */ public $get; /** * The attribute mutator. * - * @var callable|null + * @var null|callable */ public $set; @@ -36,9 +36,6 @@ class Attribute /** * Create a new attribute accessor / mutator. - * - * @param callable|null $get - * @param callable|null $set */ public function __construct(?callable $get = null, ?callable $set = null) { @@ -48,10 +45,6 @@ public function __construct(?callable $get = null, ?callable $set = null) /** * 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 { @@ -61,7 +54,6 @@ public static function make(?callable $get = null, ?callable $set = null): stati /** * Create a new attribute accessor. * - * @param callable $get * @return static */ public static function get(callable $get) @@ -72,7 +64,6 @@ public static function get(callable $get) /** * Create a new attribute mutator. * - * @param callable $set * @return static */ public static function set(callable $set) diff --git a/src/core/src/Database/Eloquent/Casts/Json.php b/src/core/src/Database/Eloquent/Casts/Json.php index 06706aaf..cf8129f9 100644 --- a/src/core/src/Database/Eloquent/Casts/Json.php +++ b/src/core/src/Database/Eloquent/Casts/Json.php @@ -9,14 +9,14 @@ class Json /** * The custom JSON encoder. * - * @var callable|null + * @var null|callable */ protected static $encoder; /** * The custom JSON decoder. * - * @var callable|null + * @var null|callable */ protected static $decoder;