dep: DTO
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DataTransferObject\Attributes;
|
||||
|
||||
use Attribute;
|
||||
use Spatie\DataTransferObject\Caster;
|
||||
use Spatie\DataTransferObject\Exceptions\InvalidCasterClass;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)]
|
||||
class CastWith
|
||||
{
|
||||
public array $args;
|
||||
|
||||
public function __construct(
|
||||
public string $casterClass,
|
||||
mixed ...$args
|
||||
) {
|
||||
if (! is_subclass_of($this->casterClass, Caster::class)) {
|
||||
throw new InvalidCasterClass($this->casterClass);
|
||||
}
|
||||
|
||||
$this->args = $args;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DataTransferObject\Attributes;
|
||||
|
||||
use Attribute;
|
||||
use JetBrains\PhpStorm\Immutable;
|
||||
use ReflectionNamedType;
|
||||
use ReflectionProperty;
|
||||
use ReflectionUnionType;
|
||||
use Spatie\DataTransferObject\Caster;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
|
||||
class DefaultCast
|
||||
{
|
||||
public function __construct(
|
||||
#[Immutable]
|
||||
private string $targetClass,
|
||||
#[Immutable]
|
||||
private string $casterClass,
|
||||
) {
|
||||
}
|
||||
|
||||
public function accepts(ReflectionProperty $property): bool
|
||||
{
|
||||
$type = $property->getType();
|
||||
|
||||
/** @var \ReflectionNamedType[]|null $types */
|
||||
$types = match ($type::class) {
|
||||
ReflectionNamedType::class => [$type],
|
||||
ReflectionUnionType::class => $type->getTypes(),
|
||||
default => null,
|
||||
};
|
||||
|
||||
if (! $types) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($types as $type) {
|
||||
if ($type->getName() !== $this->targetClass) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function resolveCaster(): Caster
|
||||
{
|
||||
return new $this->casterClass();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DataTransferObject\Attributes;
|
||||
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)]
|
||||
class MapFrom
|
||||
{
|
||||
public function __construct(
|
||||
public string | int $name,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DataTransferObject\Attributes;
|
||||
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)]
|
||||
class MapTo
|
||||
{
|
||||
public function __construct(
|
||||
public string $name,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DataTransferObject\Attributes;
|
||||
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS)]
|
||||
class Strict
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user