diff --git a/src/bundle/Resources/config/input_parsers.yml b/src/bundle/Resources/config/input_parsers.yml index cbd1bc93..4965346a 100644 --- a/src/bundle/Resources/config/input_parsers.yml +++ b/src/bundle/Resources/config/input_parsers.yml @@ -494,6 +494,13 @@ services: tags: - { name: ibexa.rest.input.parser, mediaType: application/vnd.ibexa.api.internal.criterion.Sibling } + Ibexa\Rest\Server\Input\Parser\Criterion\ContentName: + parent: Ibexa\Rest\Server\Common\Parser + arguments: + $validator: '@validator' + tags: + - { name: ibexa.rest.input.parser, mediaType: application/vnd.ibexa.api.internal.criterion.ContentName } + ibexa.rest.input.parser.internal.sortclause.content_id: parent: Ibexa\Rest\Server\Common\Parser class: Ibexa\Rest\Server\Input\Parser\SortClause\DataKeyValueObjectClass diff --git a/src/lib/Server/Input/Parser/Criterion/ContentName.php b/src/lib/Server/Input/Parser/Criterion/ContentName.php new file mode 100644 index 00000000..69fd6eff --- /dev/null +++ b/src/lib/Server/Input/Parser/Criterion/ContentName.php @@ -0,0 +1,57 @@ +validator = $validator; + } + + /** + * @param array $data + */ + public function parse(array $data, ParsingDispatcher $parsingDispatcher): ContentNameCriterion + { + $this->validateInputArray($data); + + $criterionData = $data[self::CONTENT_NAME_CRITERION]; + + return new ContentNameCriterion($criterionData); + } + + /** + * @param array $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 + ); + } + } +} diff --git a/src/lib/Server/Validation/Builder/Input/Parser/Criterion/ContentNameValidatorBuilder.php b/src/lib/Server/Validation/Builder/Input/Parser/Criterion/ContentNameValidatorBuilder.php new file mode 100644 index 00000000..a743b627 --- /dev/null +++ b/src/lib/Server/Validation/Builder/Input/Parser/Criterion/ContentNameValidatorBuilder.php @@ -0,0 +1,28 @@ + new Assert\Required( + [ + new Assert\Type('string'), + new Assert\NotBlank(), + ] + ), + ]; + } +} diff --git a/tests/bundle/Functional/SearchView/Criterion/ContentNameTest.php b/tests/bundle/Functional/SearchView/Criterion/ContentNameTest.php new file mode 100644 index 00000000..18b2491d --- /dev/null +++ b/tests/bundle/Functional/SearchView/Criterion/ContentNameTest.php @@ -0,0 +1,47 @@ +createFolder('foo', '/api/ibexa/v2/content/locations/1/2'); + $this->createFolder('foobar', '/api/ibexa/v2/content/locations/1/2'); + } + + /** + * @return iterable + */ + public function getCriteriaPayloads(): iterable + { + yield 'Return content items that contain "foo" in name' => [ + 'json', + $this->buildJsonCriterionQuery('"ContentNameCriterion": "foo*"'), + 2, + ]; + + yield 'No content items found with article in name' => [ + 'json', + $this->buildJsonCriterionQuery('"ContentNameCriterion": "*article*"'), + 0, + ]; + } +}