-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored Float and Integer field types to use external validators (#…
…425) For more details see #425 Key changes: * Refactored Float and Integer field types to use external validators * [Tests] Refactored Float, Integer, and String validator tests --------- Co-authored-by: Adam Wójs <[email protected]> Co-authored-by: Paweł Niedzielski <[email protected]>
- Loading branch information
1 parent
4bed131
commit 7c398f2
Showing
14 changed files
with
887 additions
and
1,720 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,83 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Core\FieldType; | ||
|
||
use Ibexa\Contracts\Core\FieldType\Value as SPIValue; | ||
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition; | ||
use JMS\TranslationBundle\Translation\TranslationContainerInterface; | ||
|
||
abstract class BaseNumericType extends FieldType implements TranslationContainerInterface | ||
{ | ||
/** | ||
* @return array<string, \Ibexa\Core\FieldType\Validator> | ||
*/ | ||
abstract protected function getValidators(): array; | ||
|
||
public function getValidator(string $validatorIdentifier): ?Validator | ||
{ | ||
return $this->getValidators()[$validatorIdentifier] ?? null; | ||
} | ||
|
||
/** | ||
* Validates the validatorConfiguration of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct. | ||
* | ||
* @param array<string, mixed> $validatorConfiguration | ||
* | ||
* @return \Ibexa\Contracts\Core\FieldType\ValidationError[] | ||
*/ | ||
public function validateValidatorConfiguration($validatorConfiguration): array | ||
{ | ||
$validationErrors = []; | ||
$validatorValidationErrors = []; | ||
foreach ($validatorConfiguration as $validatorIdentifier => $constraints) { | ||
$validator = $this->getValidator($validatorIdentifier); | ||
if (null === $validator) { | ||
$validationErrors[] = new ValidationError( | ||
"Validator '%validator%' is unknown", | ||
null, | ||
[ | ||
'%validator%' => $validatorIdentifier, | ||
], | ||
"[$validatorIdentifier]" | ||
); | ||
|
||
continue; | ||
} | ||
|
||
$validatorValidationErrors[] = $validator->validateConstraints($constraints); | ||
} | ||
|
||
return array_merge($validationErrors, ...$validatorValidationErrors); | ||
} | ||
|
||
/** | ||
* Validates a field based on the validators in the field definition. | ||
* | ||
* @return \Ibexa\Contracts\Core\FieldType\ValidationError[] | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\PropertyNotFoundException | ||
*/ | ||
public function validate(FieldDefinition $fieldDefinition, SPIValue $value): array | ||
{ | ||
if ($this->isEmptyValue($value)) { | ||
return []; | ||
} | ||
|
||
$errors = []; | ||
$validatorConfiguration = $fieldDefinition->getValidatorConfiguration(); | ||
foreach ($this->getValidators() as $validatorIdentifier => $validator) { | ||
$validator->initializeWithConstraints($validatorConfiguration[$validatorIdentifier] ?? []); | ||
if (!$validator->validate($value, $fieldDefinition)) { | ||
$errors[] = $validator->getMessage(); | ||
} | ||
} | ||
|
||
return array_merge(...$errors); | ||
} | ||
} |
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
Oops, something went wrong.