generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
510 additions
and
25 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
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
Empty file.
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Validator\Attribute; | ||
|
||
use Attribute; | ||
use Yiisoft\Hydrator\ParameterAttributeInterface; | ||
use Yiisoft\Validator\RuleInterface; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)] | ||
final class Validate implements ParameterAttributeInterface | ||
{ | ||
/** | ||
* @var RuleInterface[] $rules | ||
* @psalm-var list<RuleInterface> $rules | ||
*/ | ||
private array $rules; | ||
|
||
public function __construct(RuleInterface ...$rules) | ||
{ | ||
$this->rules = array_values($rules); | ||
} | ||
|
||
/** | ||
* @return RuleInterface[] | ||
* @psalm-return list<RuleInterface> | ||
*/ | ||
public function getRules(): array | ||
{ | ||
return $this->rules; | ||
} | ||
|
||
public function getResolver(): string | ||
{ | ||
return ValidateResolver::class; | ||
} | ||
} |
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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Validator\Attribute; | ||
|
||
use Yiisoft\Hydrator\Context; | ||
use Yiisoft\Hydrator\NotResolvedException; | ||
use Yiisoft\Hydrator\ParameterAttributeInterface; | ||
use Yiisoft\Hydrator\ParameterAttributeResolverInterface; | ||
use Yiisoft\Hydrator\UnexpectedAttributeException; | ||
use Yiisoft\Validator\Result; | ||
use Yiisoft\Validator\ValidatorInterface; | ||
|
||
final class ValidateResolver implements ParameterAttributeResolverInterface | ||
{ | ||
private ?Result $result = null; | ||
|
||
public function __construct( | ||
private ValidatorInterface $validator, | ||
) { | ||
} | ||
|
||
public function setResult(?Result $result): void | ||
{ | ||
$this->result = $result; | ||
} | ||
|
||
public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): mixed | ||
{ | ||
if (!$attribute instanceof Validate) { | ||
throw new UnexpectedAttributeException(Validate::class, $attribute); | ||
} | ||
|
||
if ($this->result !== null) { | ||
$parameterName = $context->getParameter()->getName(); | ||
$result = $this->validator->validate( | ||
$context->isResolved() ? [$parameterName => $context->getResolvedValue()] : [], | ||
Check warning on line 38 in src/Attribute/ValidateResolver.php GitHub Actions / mutation / PHP 8.2-ubuntu-latest
Check warning on line 38 in src/Attribute/ValidateResolver.php GitHub Actions / mutation / PHP 8.2-ubuntu-latest
|
||
[$parameterName => $attribute->getRules()], | ||
); | ||
|
||
foreach ($result->getErrors() as $error) { | ||
$this->result->addError( | ||
$error->getMessage(), | ||
$error->getParameters(), | ||
$error->getValuePath(), | ||
); | ||
} | ||
} | ||
|
||
throw new NotResolvedException(); | ||
} | ||
} |
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 Yiisoft\Hydrator\Validator; | ||
|
||
use Yiisoft\Validator\PostValidationHookInterface; | ||
use Yiisoft\Validator\Result; | ||
|
||
interface ValidatedInputInterface extends PostValidationHookInterface | ||
{ | ||
public function getValidationResult(): ?Result; | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Validator; | ||
|
||
use Yiisoft\Hydrator\Attribute\SkipHydration; | ||
use Yiisoft\Validator\Result; | ||
|
||
trait ValidatedInputTrait | ||
{ | ||
#[SkipHydration] | ||
private ?Result $validationResult = null; | ||
|
||
public function processValidationResult(Result $result): void | ||
{ | ||
$this->validationResult = $result; | ||
} | ||
|
||
public function getValidationResult(): ?Result | ||
{ | ||
return $this->validationResult; | ||
} | ||
} |
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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Validator; | ||
|
||
use Yiisoft\Hydrator\HydratorInterface; | ||
use Yiisoft\Hydrator\Validator\Attribute\ValidateResolver; | ||
use Yiisoft\Validator\Result; | ||
use Yiisoft\Validator\ValidatorInterface; | ||
|
||
final class ValidatingHydrator implements HydratorInterface | ||
{ | ||
public function __construct( | ||
private HydratorInterface $hydrator, | ||
private ValidatorInterface $validator, | ||
private ValidateResolver $validateResolver, | ||
) { | ||
} | ||
|
||
public function hydrate( | ||
object $object, | ||
array $data = [], | ||
array $map = [], | ||
bool $strict = false | ||
): void { | ||
$result = $this->beforeAction(); | ||
$this->hydrator->hydrate($object, $data, $map, $strict); | ||
$this->afterAction($object, $result); | ||
} | ||
|
||
public function create( | ||
string $class, | ||
array $data = [], | ||
array $map = [], | ||
bool $strict = false | ||
): object { | ||
$result = $this->beforeAction(); | ||
$object = $this->hydrator->create($class, $data, $map, $strict); | ||
$this->afterAction($object, $result); | ||
return $object; | ||
} | ||
|
||
private function beforeAction(): Result | ||
{ | ||
$result = new Result(); | ||
$this->validateResolver->setResult($result); | ||
return $result; | ||
} | ||
|
||
private function afterAction(object $object, Result $result): void | ||
{ | ||
if (!$object instanceof ValidatedInputInterface) { | ||
return; | ||
} | ||
|
||
$result->isValid() | ||
? $this->validator->validate($object) | ||
: $object->processValidationResult($result); | ||
} | ||
} |
Empty file.
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Validator\Tests\Attribute; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Hydrator\UnexpectedAttributeException; | ||
use Yiisoft\Hydrator\Validator\Attribute\Validate; | ||
use Yiisoft\Hydrator\Validator\Tests\Support\Attribute\IncorrectValidateResolver; | ||
use Yiisoft\Hydrator\Validator\Tests\Support\TestHelper; | ||
|
||
final class ValidateResolverTest extends TestCase | ||
{ | ||
public function testInvalidAttribute(): void | ||
{ | ||
$object = new class () { | ||
#[IncorrectValidateResolver] | ||
public int $a; | ||
}; | ||
|
||
$hydrator = TestHelper::createValidatingHydrator(); | ||
|
||
$this->expectException(UnexpectedAttributeException::class); | ||
$this->expectExceptionMessage('Expected "' . Validate::class . '", but'); | ||
$hydrator->hydrate($object, ['a' => 7]); | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Validator\Tests\Attribute; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Hydrator\Validator\Attribute\Validate; | ||
use Yiisoft\Hydrator\Validator\Attribute\ValidateResolver; | ||
use Yiisoft\Validator\Rule\Length; | ||
use Yiisoft\Validator\Rule\Required; | ||
|
||
final class ValidateTest extends TestCase | ||
{ | ||
public function testBase(): void | ||
{ | ||
$sourceRules = [new Required(), new Length(min: 3)]; | ||
|
||
$attribute = new Validate(...$sourceRules); | ||
|
||
$rules = $attribute->getRules(); | ||
$resolver = $attribute->getResolver(); | ||
|
||
$this->assertSame($sourceRules, $rules); | ||
$this->assertSame(ValidateResolver::class, $resolver); | ||
} | ||
|
||
public function testNamedArguments(): void | ||
{ | ||
$rule1 = new Required(); | ||
$rule2 = new Length(min: 3); | ||
|
||
$attribute = new Validate(a: $rule1, b: $rule2); | ||
|
||
$this->assertSame([$rule1, $rule2], $attribute->getRules()); | ||
} | ||
} |
Oops, something went wrong.