* SPDX-FileContributor: Fabio Ribeiro * SPDX-FileContributor: Henrique Moody */ declare(strict_types=1); namespace Respect\Validation\Validators; use Attribute; use Respect\Dev\CodeGen\Attributes\Mixin; use Respect\Validation\Result; use Respect\Validation\Validator; #[Mixin(exclude: ['all', 'key', 'property'])] #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] final readonly class PropertyOptional implements Validator { public function __construct( private string $propertyName, private Validator $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); } }