-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #961 from msmakouz/feature/filters-improvement
- Loading branch information
Showing
22 changed files
with
501 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Filters\Exception; | ||
|
||
class SetterException extends FilterException | ||
{ | ||
public function __construct(\Throwable $previous = null) | ||
{ | ||
parent::__construct(message: 'Unable to set value. The given data was invalid.', previous: $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Filters\Model\Mapper; | ||
|
||
use Spiral\Filters\Model\FilterInterface; | ||
|
||
interface CasterInterface | ||
{ | ||
public function supports(\ReflectionNamedType $type): bool; | ||
|
||
public function setValue(FilterInterface $filter, \ReflectionProperty $property, mixed $value): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Filters\Model\Mapper; | ||
|
||
final class CasterRegistry implements CasterRegistryInterface | ||
{ | ||
/** @var array<class-string, CasterInterface> */ | ||
private array $casters = []; | ||
|
||
public function __construct(array $casters = []) | ||
{ | ||
foreach ($casters as $caster) { | ||
$this->register($caster); | ||
} | ||
} | ||
|
||
public function register(CasterInterface $caster): void | ||
{ | ||
$this->casters[$caster::class] = $caster; | ||
} | ||
|
||
/** | ||
* @return array<CasterInterface> | ||
*/ | ||
public function getCasters(): array | ||
{ | ||
return \array_values($this->casters); | ||
} | ||
|
||
public function getDefault(): CasterInterface | ||
{ | ||
return new DefaultCaster(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Filters\Model\Mapper; | ||
|
||
interface CasterRegistryInterface | ||
{ | ||
public function register(CasterInterface $caster): void; | ||
|
||
/** | ||
* @return array<CasterInterface> | ||
*/ | ||
public function getCasters(): array; | ||
|
||
public function getDefault(): CasterInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Filters\Model\Mapper; | ||
|
||
use Spiral\Filters\Model\FilterInterface; | ||
|
||
final class DefaultCaster implements CasterInterface | ||
{ | ||
public function supports(\ReflectionNamedType $type): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function setValue(FilterInterface $filter, \ReflectionProperty $property, mixed $value): void | ||
{ | ||
$property->setValue($filter, $value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Filters\Model\Mapper; | ||
|
||
use Spiral\Filters\Model\FilterInterface; | ||
|
||
final class EnumCaster implements CasterInterface | ||
{ | ||
public function supports(\ReflectionNamedType $type): bool | ||
{ | ||
return \enum_exists($type->getName()); | ||
} | ||
|
||
public function setValue(FilterInterface $filter, \ReflectionProperty $property, mixed $value): void | ||
{ | ||
/** | ||
* @var \ReflectionNamedType $type | ||
*/ | ||
$type = $property->getType(); | ||
|
||
/** | ||
* @var class-string<\BackedEnum> $enum | ||
*/ | ||
$enum = $type->getName(); | ||
|
||
$property->setValue($filter, $enum::from($value)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Filters\Model\Mapper; | ||
|
||
use Spiral\Filters\Model\FilterInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class Mapper | ||
{ | ||
public function __construct( | ||
private readonly CasterRegistryInterface $registry | ||
) { | ||
} | ||
|
||
/** | ||
* Set input data to the filter property. | ||
*/ | ||
public function setValue(FilterInterface $filter, \ReflectionProperty $property, mixed $value): void | ||
{ | ||
$type = $property->getType(); | ||
if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) { | ||
foreach ($this->registry->getCasters() as $setter) { | ||
if ($setter->supports($type)) { | ||
$setter->setValue($filter, $property, $value); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
$this->registry->getDefault()->setValue($filter, $property, $value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Filters\Model\Mapper; | ||
|
||
use Ramsey\Uuid\UuidInterface; | ||
use Spiral\Filters\Model\FilterInterface; | ||
|
||
final class UuidCaster implements CasterInterface | ||
{ | ||
private ?bool $interfaceExists = null; | ||
|
||
public function supports(\ReflectionNamedType $type): bool | ||
{ | ||
if ($this->interfaceExists === null) { | ||
$this->interfaceExists = \interface_exists(UuidInterface::class); | ||
} | ||
|
||
return $this->interfaceExists && $this->implements($type->getName(), UuidInterface::class); | ||
} | ||
|
||
public function setValue(FilterInterface $filter, \ReflectionProperty $property, mixed $value): void | ||
{ | ||
$property->setValue($filter, \Ramsey\Uuid\Uuid::fromString($value)); | ||
} | ||
|
||
private function implements(string $haystack, string $interface): bool | ||
{ | ||
if ($haystack === $interface) { | ||
return true; | ||
} | ||
|
||
foreach ((array)\class_implements($haystack) as $implements) { | ||
if ($implements === $interface) { | ||
return true; | ||
} | ||
|
||
if (self::implements($implements, $interface)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.