-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added input parser for ContentName criterion
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?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\Rest\Server\Input\Parser\Criterion; | ||
|
||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ContentName as ContentNameCriterion; | ||
use Ibexa\Contracts\Rest\Input\ParsingDispatcher; | ||
use Ibexa\Rest\Input\BaseParser; | ||
use Ibexa\Rest\Server\Exceptions\ValidationFailedException; | ||
use Ibexa\Rest\Server\Validation\Builder\Input\Parser\Criterion\ContentNameValidatorBuilder; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
final class ContentName extends BaseParser | ||
{ | ||
public const CONTENT_NAME_CRITERION = 'ContentNameCriterion'; | ||
|
||
private ValidatorInterface $validator; | ||
|
||
public function __construct(ValidatorInterface $validator) | ||
{ | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $data | ||
*/ | ||
public function parse(array $data, ParsingDispatcher $parsingDispatcher): ContentNameCriterion | ||
{ | ||
$this->validateInputArray($data); | ||
|
||
$criterionData = $data[self::CONTENT_NAME_CRITERION]; | ||
|
||
return new ContentNameCriterion($criterionData); | ||
} | ||
|
||
/** | ||
* @param array<mixed> $data | ||
*/ | ||
private function validateInputArray(array $data): void | ||
{ | ||
$validatorBuilder = new ContentNameValidatorBuilder($this->validator); | ||
$validatorBuilder->validateInputArray($data); | ||
$violations = $validatorBuilder->build()->getViolations(); | ||
|
||
if ($violations->count() > 0) { | ||
throw new ValidationFailedException( | ||
self::CONTENT_NAME_CRITERION, | ||
$violations | ||
); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/lib/Server/Validation/Builder/Input/Parser/Criterion/ContentNameValidatorBuilder.php
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 | ||
|
||
/** | ||
* @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\Rest\Server\Validation\Builder\Input\Parser\Criterion; | ||
|
||
use Ibexa\Rest\Server\Input\Parser\Criterion\ContentName; | ||
use Ibexa\Rest\Server\Validation\Builder\Input\Parser\BaseInputParserCollectionValidatorBuilder; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
final class ContentNameValidatorBuilder extends BaseInputParserCollectionValidatorBuilder | ||
{ | ||
protected function getCollectionConstraints(): array | ||
{ | ||
return [ | ||
ContentName::CONTENT_NAME_CRITERION => new Assert\Required( | ||
[ | ||
new Assert\Type('string'), | ||
new Assert\NotBlank(), | ||
] | ||
), | ||
]; | ||
} | ||
} |