Skip to content

Commit d44cbe9

Browse files
committed
Merge branch 'main' of github.com:hypervel/components
2 parents fbc7e84 + 221a40a commit d44cbe9

File tree

9 files changed

+1518
-2
lines changed

9 files changed

+1518
-2
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Foundation\Http\Casts;
6+
7+
use ArrayObject;
8+
use Hypervel\Foundation\Http\Contracts\Castable;
9+
use Hypervel\Foundation\Http\Contracts\CastInputs;
10+
use RuntimeException;
11+
12+
class AsDataObjectArray implements Castable
13+
{
14+
/**
15+
* Get the caster class to use when casting from / to this cast target.
16+
*
17+
* @param string $class The data object class name
18+
*/
19+
public static function of(string $class): string
20+
{
21+
return static::class . ':' . $class;
22+
}
23+
24+
/**
25+
* Get the caster class to use when casting from / to this cast target.
26+
*/
27+
public static function castUsing(array $arguments = []): CastInputs
28+
{
29+
return new class($arguments) implements CastInputs {
30+
public function __construct(protected array $arguments)
31+
{
32+
}
33+
34+
public function get(string $key, mixed $value, array $inputs): mixed
35+
{
36+
if (! isset($inputs[$key]) || ! is_array($value)) {
37+
return null;
38+
}
39+
40+
$dataClass = $this->arguments[0];
41+
42+
// Check if the class has make static method (provided by DataObject)
43+
if (! method_exists($dataClass, 'make')) {
44+
throw new RuntimeException(
45+
"Class {$dataClass} must implement static make(array \$data) method"
46+
);
47+
}
48+
49+
return new ArrayObject(
50+
array_map(fn ($item) => $dataClass::make($item), $value)
51+
);
52+
}
53+
54+
public function set(string $key, mixed $value, array $inputs): array
55+
{
56+
if ($value === null) {
57+
return [$key => null];
58+
}
59+
60+
$storable = array_map(function ($item) {
61+
if (method_exists($item, 'toArray')) {
62+
return $item->toArray();
63+
}
64+
return $item;
65+
}, (array) $value);
66+
67+
return [$key => $storable];
68+
}
69+
};
70+
}
71+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Foundation\Http\Casts;
6+
7+
use Hypervel\Foundation\Http\Contracts\Castable;
8+
use Hypervel\Foundation\Http\Contracts\CastInputs;
9+
use Hypervel\Support\Collection;
10+
use RuntimeException;
11+
12+
class AsDataObjectCollection implements Castable
13+
{
14+
/**
15+
* Get the caster class to use when casting from / to this cast target.
16+
*
17+
* @param string $class The data object class name
18+
*/
19+
public static function of(string $class): string
20+
{
21+
return static::class . ':' . $class;
22+
}
23+
24+
/**
25+
* Get the caster class to use when casting from / to this cast target.
26+
*/
27+
public static function castUsing(array $arguments = []): CastInputs
28+
{
29+
return new class($arguments) implements CastInputs {
30+
public function __construct(protected array $arguments)
31+
{
32+
}
33+
34+
public function get(string $key, mixed $value, array $inputs): mixed
35+
{
36+
if (! isset($inputs[$key]) || ! is_array($value)) {
37+
return new Collection();
38+
}
39+
40+
$dataClass = $this->arguments[0];
41+
42+
// Check if the class has make static method (provided by DataObject)
43+
if (! method_exists($dataClass, 'make')) {
44+
throw new RuntimeException(
45+
"Class {$dataClass} must implement static make(array \$data) method"
46+
);
47+
}
48+
49+
return new Collection(
50+
array_map(fn ($item) => $dataClass::make($item), $value)
51+
);
52+
}
53+
54+
public function set(string $key, mixed $value, array $inputs): array
55+
{
56+
if ($value === null) {
57+
return [$key => null];
58+
}
59+
60+
if (! $value instanceof Collection) {
61+
return [$key => $value];
62+
}
63+
64+
$storable = $value->map(function ($item) {
65+
if (method_exists($item, 'toArray')) {
66+
return $item->toArray();
67+
}
68+
return $item;
69+
})->toArray();
70+
71+
return [$key => $storable];
72+
}
73+
};
74+
}
75+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Foundation\Http\Casts;
6+
7+
use ArrayObject;
8+
use BackedEnum;
9+
use Hypervel\Foundation\Http\Contracts\Castable;
10+
use Hypervel\Foundation\Http\Contracts\CastInputs;
11+
use Hypervel\Support\Collection;
12+
13+
use function Hypervel\Support\enum_value;
14+
15+
class AsEnumArrayObject implements Castable
16+
{
17+
/**
18+
* Get the caster class to use when casting from / to this cast target.
19+
*
20+
* @param string $class The enum class name
21+
*/
22+
public static function of(string $class): string
23+
{
24+
return static::class . ':' . $class;
25+
}
26+
27+
/**
28+
* Get the caster class to use when casting from / to this cast target.
29+
*/
30+
public static function castUsing(array $arguments = []): CastInputs
31+
{
32+
return new class($arguments) implements CastInputs {
33+
public function __construct(protected array $arguments)
34+
{
35+
}
36+
37+
public function get(string $key, mixed $value, array $inputs): mixed
38+
{
39+
if (! isset($inputs[$key]) || ! is_array($value)) {
40+
return null;
41+
}
42+
43+
$enumClass = $this->arguments[0];
44+
45+
return new ArrayObject(
46+
(new Collection($value))->map(function ($item) use ($enumClass) {
47+
if ($item instanceof $enumClass) {
48+
return $item;
49+
}
50+
51+
return is_subclass_of($enumClass, BackedEnum::class)
52+
? $enumClass::from($item)
53+
: constant($enumClass . '::' . $item);
54+
})->toArray()
55+
);
56+
}
57+
58+
public function set(string $key, mixed $value, array $inputs): array
59+
{
60+
if ($value === null) {
61+
return [$key => null];
62+
}
63+
64+
$storable = [];
65+
66+
foreach ($value as $enum) {
67+
$storable[] = $this->getStorableEnumValue($enum);
68+
}
69+
70+
return [$key => $storable];
71+
}
72+
73+
protected function getStorableEnumValue(mixed $enum): mixed
74+
{
75+
if (is_string($enum) || is_int($enum)) {
76+
return $enum;
77+
}
78+
79+
return enum_value($enum);
80+
}
81+
};
82+
}
83+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Foundation\Http\Casts;
6+
7+
use BackedEnum;
8+
use Hyperf\Collection\Collection;
9+
use Hypervel\Foundation\Http\Contracts\Castable;
10+
use Hypervel\Foundation\Http\Contracts\CastInputs;
11+
12+
use function Hypervel\Support\enum_value;
13+
14+
class AsEnumCollection implements Castable
15+
{
16+
/**
17+
* Get the caster class to use when casting from / to this cast target.
18+
*
19+
* @param string $class The enum class name
20+
*/
21+
public static function of(string $class): string
22+
{
23+
return static::class . ':' . $class;
24+
}
25+
26+
/**
27+
* Get the caster class to use when casting from / to this cast target.
28+
*/
29+
public static function castUsing(array $arguments = []): CastInputs
30+
{
31+
return new class($arguments) implements CastInputs {
32+
public function __construct(protected array $arguments)
33+
{
34+
}
35+
36+
public function get(string $key, mixed $value, array $inputs): mixed
37+
{
38+
if (! isset($inputs[$key]) || ! is_array($value)) {
39+
return null;
40+
}
41+
42+
$enumClass = $this->arguments[0];
43+
44+
return (new Collection($value))->map(function ($item) use ($enumClass) {
45+
if ($item instanceof $enumClass) {
46+
return $item;
47+
}
48+
49+
return is_subclass_of($enumClass, BackedEnum::class)
50+
? $enumClass::from($item)
51+
: constant($enumClass . '::' . $item);
52+
});
53+
}
54+
55+
public function set(string $key, mixed $value, array $inputs): array
56+
{
57+
if ($value === null) {
58+
return [$key => null];
59+
}
60+
61+
$storable = (new Collection($value))->map(function ($enum) {
62+
return $this->getStorableEnumValue($enum);
63+
})->toArray();
64+
65+
return [$key => $storable];
66+
}
67+
68+
protected function getStorableEnumValue(mixed $enum): mixed
69+
{
70+
if (is_string($enum) || is_int($enum)) {
71+
return $enum;
72+
}
73+
74+
return enum_value($enum);
75+
}
76+
};
77+
}
78+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Foundation\Http\Contracts;
6+
7+
interface CastInputs
8+
{
9+
/**
10+
* Transform the inputs from the underlying input value.
11+
*
12+
* @param string $key The inputs key
13+
* @param mixed $value The inputs value
14+
* @param array $inputs All inputs
15+
* @return mixed The transformed value
16+
*/
17+
public function get(string $key, mixed $value, array $inputs): mixed;
18+
19+
/**
20+
* Transform the inputs to its underlying representation for storage.
21+
*
22+
* @param string $key The inputs key
23+
* @param mixed $value The inputs value
24+
* @param array $inputs All inputs
25+
* @return array The transformed value as an array
26+
*/
27+
public function set(string $key, mixed $value, array $inputs): array;
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Foundation\Http\Contracts;
6+
7+
interface Castable
8+
{
9+
/**
10+
* Get the name of the caster class to use when casting from / to this cast target.
11+
*/
12+
public static function castUsing(array $arguments = []): CastInputs|string;
13+
}

src/foundation/src/Http/FormRequest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Hyperf\Context\Context;
99
use Hyperf\Context\ResponseContext;
1010
use Hypervel\Auth\Access\AuthorizationException;
11+
use Hypervel\Foundation\Http\Traits\HasCasts;
1112
use Hypervel\Http\Request;
1213
use Hypervel\Validation\Contracts\Factory as ValidationFactory;
1314
use Hypervel\Validation\Contracts\ValidatesWhenResolved;
@@ -19,6 +20,7 @@
1920

2021
class FormRequest extends Request implements ValidatesWhenResolved
2122
{
23+
use HasCasts;
2224
use ValidatesWhenResolvedTrait;
2325

2426
/**
@@ -36,8 +38,9 @@ class FormRequest extends Request implements ValidatesWhenResolved
3638
*/
3739
protected array $dontFlash = ['password', 'password_confirmation'];
3840

39-
public function __construct(protected ContainerInterface $container)
40-
{
41+
public function __construct(
42+
protected ContainerInterface $container
43+
) {
4144
}
4245

4346
public function scene(string $scene): static

0 commit comments

Comments
 (0)