Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
43 changes: 43 additions & 0 deletions src/core/src/Database/Eloquent/Casts/AsArrayObject.php
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();
}
};
}
}
94 changes: 94 additions & 0 deletions src/core/src/Database/Eloquent/Casts/AsCollection.php
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]);
}
}
97 changes: 97 additions & 0 deletions src/core/src/Database/Eloquent/Casts/Attribute.php
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;
}
}
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 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;
}
}
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;
}