Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/core/src/Database/Eloquent/Casts/AsArrayObject.php
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

View workflow job for this annotation

GitHub Actions / PHP 8.2 (swoole-5.1.6)

PHPDoc tag `@return` with type Illuminate\Contracts\Database\Eloquent\CastsAttributes<Illuminate\Database\Eloquent\Casts\ArrayObject<(int|string), mixed>, iterable> is not subtype of native type Hyperf\Contract\CastsAttributes.

Check failure on line 19 in src/core/src/Database/Eloquent/Casts/AsArrayObject.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 (swoole-5.1.6)

Method Hypervel\Database\Eloquent\Casts\AsArrayObject::castUsing() has invalid return type Illuminate\Database\Eloquent\Casts\ArrayObject.

Check failure on line 19 in src/core/src/Database/Eloquent/Casts/AsArrayObject.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 (swoole-5.1.6)

Method Hypervel\Database\Eloquent\Casts\AsArrayObject::castUsing() has invalid return type Illuminate\Contracts\Database\Eloquent\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();
}
};
}
}
96 changes: 96 additions & 0 deletions src/core/src/Database/Eloquent/Casts/AsCollection.php
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>
*/
public static function castUsing(array $arguments = []): CastsAttributes

Check failure on line 21 in src/core/src/Database/Eloquent/Casts/AsCollection.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 (swoole-5.1.6)

PHPDoc tag `@return` with type Illuminate\Contracts\Database\Eloquent\CastsAttributes<Illuminate\Support\Collection<(int|string), mixed>, iterable> is not subtype of native type Hyperf\Contract\CastsAttributes.

Check failure on line 21 in src/core/src/Database/Eloquent/Casts/AsCollection.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 (swoole-5.1.6)

Method Hypervel\Database\Eloquent\Casts\AsCollection::castUsing() has invalid return type Illuminate\Support\Collection.

Check failure on line 21 in src/core/src/Database/Eloquent/Casts/AsCollection.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 (swoole-5.1.6)

Method Hypervel\Database\Eloquent\Casts\AsCollection::castUsing() has invalid return type Illuminate\Contracts\Database\Eloquent\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]);
}
}
106 changes: 106 additions & 0 deletions src/core/src/Database/Eloquent/Casts/Attribute.php
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
*/
public $get;

/**
* The attribute mutator.
*
* @var 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.
*
* @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;
}
}
58 changes: 58 additions & 0 deletions src/core/src/Database/Eloquent/Casts/Json.php
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.
*
* @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;
}
}
4 changes: 3 additions & 1 deletion src/foundation/src/Http/Contracts/Castable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CastInputs interface is referenced in the return type but not imported. Add use Hypervel\Foundation\Http\Contracts\CastInputs; at the top of the file to properly resolve this type.

Copilot uses AI. Check for mistakes.
}
Loading