mirror of
https://github.com/Respect/Validation.git
synced 2026-03-15 06:45:44 +01:00
We've always considered renaming this directory, as it's not a common standard to name `library` the directory where the source code of a library it. Having it as `src/` is a common pattern we find in several PHP libraries these days. Acked-by: Alexandre Gomes Gaigalas <alganet@gmail.com>
38 lines
1.1 KiB
PHP
38 lines
1.1 KiB
PHP
<?php
|
|
|
|
/*
|
|
* SPDX-License-Identifier: MIT
|
|
* SPDX-FileCopyrightText: (c) Respect Project Contributors
|
|
* SPDX-FileContributor: Fabio Ribeiro <faabiosr@gmail.com>
|
|
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Validators;
|
|
|
|
use Attribute;
|
|
use Respect\Validation\Result;
|
|
use Respect\Validation\Validator;
|
|
use Respect\Validation\Validators\Core\Wrapper;
|
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
|
|
final class PropertyOptional extends Wrapper
|
|
{
|
|
public function __construct(
|
|
private readonly string $propertyName,
|
|
Validator $validator,
|
|
) {
|
|
parent::__construct($validator);
|
|
}
|
|
|
|
public function evaluate(mixed $input): Result
|
|
{
|
|
$propertyExistsResult = (new PropertyExists($this->propertyName))->evaluate($input);
|
|
if (!$propertyExistsResult->hasPassed) {
|
|
return $propertyExistsResult->withNameFrom($this->validator)->withToggledModeAndValidation();
|
|
}
|
|
|
|
return (new Property($this->propertyName, $this->validator))->evaluate($input);
|
|
}
|
|
}
|