Skip to content

Commit 2d69c56

Browse files
authored
IBX-7418: Added input parser for ContentName criterion (#82)
For more details see https://issues.ibexa.co/browse/IBX-7418 and #82 Key changes: * Implemented input parser for ContentName criterion (ibexa/core#312) * [Tests] Added functional tests coverage
1 parent 93710f1 commit 2d69c56

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

src/bundle/Resources/config/input_parsers.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,13 @@ services:
494494
tags:
495495
- { name: ibexa.rest.input.parser, mediaType: application/vnd.ibexa.api.internal.criterion.Sibling }
496496

497+
Ibexa\Rest\Server\Input\Parser\Criterion\ContentName:
498+
parent: Ibexa\Rest\Server\Common\Parser
499+
arguments:
500+
$validator: '@validator'
501+
tags:
502+
- { name: ibexa.rest.input.parser, mediaType: application/vnd.ibexa.api.internal.criterion.ContentName }
503+
497504
ibexa.rest.input.parser.internal.sortclause.content_id:
498505
parent: Ibexa\Rest\Server\Common\Parser
499506
class: Ibexa\Rest\Server\Input\Parser\SortClause\DataKeyValueObjectClass
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Rest\Server\Input\Parser\Criterion;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ContentName as ContentNameCriterion;
12+
use Ibexa\Contracts\Rest\Input\ParsingDispatcher;
13+
use Ibexa\Rest\Input\BaseParser;
14+
use Ibexa\Rest\Server\Exceptions\ValidationFailedException;
15+
use Ibexa\Rest\Server\Validation\Builder\Input\Parser\Criterion\ContentNameValidatorBuilder;
16+
use Symfony\Component\Validator\Validator\ValidatorInterface;
17+
18+
final class ContentName extends BaseParser
19+
{
20+
public const CONTENT_NAME_CRITERION = 'ContentNameCriterion';
21+
22+
private ValidatorInterface $validator;
23+
24+
public function __construct(ValidatorInterface $validator)
25+
{
26+
$this->validator = $validator;
27+
}
28+
29+
/**
30+
* @param array<mixed> $data
31+
*/
32+
public function parse(array $data, ParsingDispatcher $parsingDispatcher): ContentNameCriterion
33+
{
34+
$this->validateInputArray($data);
35+
36+
$criterionData = $data[self::CONTENT_NAME_CRITERION];
37+
38+
return new ContentNameCriterion($criterionData);
39+
}
40+
41+
/**
42+
* @param array<mixed> $data
43+
*/
44+
private function validateInputArray(array $data): void
45+
{
46+
$validatorBuilder = new ContentNameValidatorBuilder($this->validator);
47+
$validatorBuilder->validateInputArray($data);
48+
$violations = $validatorBuilder->build()->getViolations();
49+
50+
if ($violations->count() > 0) {
51+
throw new ValidationFailedException(
52+
self::CONTENT_NAME_CRITERION,
53+
$violations
54+
);
55+
}
56+
}
57+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Rest\Server\Validation\Builder\Input\Parser\Criterion;
10+
11+
use Ibexa\Rest\Server\Input\Parser\Criterion\ContentName;
12+
use Ibexa\Rest\Server\Validation\Builder\Input\Parser\BaseInputParserCollectionValidatorBuilder;
13+
use Symfony\Component\Validator\Constraints as Assert;
14+
15+
final class ContentNameValidatorBuilder extends BaseInputParserCollectionValidatorBuilder
16+
{
17+
protected function getCollectionConstraints(): array
18+
{
19+
return [
20+
ContentName::CONTENT_NAME_CRITERION => new Assert\Required(
21+
[
22+
new Assert\Type('string'),
23+
new Assert\NotBlank(),
24+
]
25+
),
26+
];
27+
}
28+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Bundle\Rest\Functional\SearchView\Criterion;
10+
11+
use Ibexa\Tests\Bundle\Rest\Functional\SearchView\SearchCriterionTestCase;
12+
13+
/**
14+
* @covers \Ibexa\Rest\Server\Input\Parser\Criterion\ContentName
15+
*/
16+
final class ContentNameTest extends SearchCriterionTestCase
17+
{
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->createFolder('foo', '/api/ibexa/v2/content/locations/1/2');
23+
$this->createFolder('foobar', '/api/ibexa/v2/content/locations/1/2');
24+
}
25+
26+
/**
27+
* @return iterable<array{
28+
* string,
29+
* string,
30+
* int,
31+
* }>
32+
*/
33+
public function getCriteriaPayloads(): iterable
34+
{
35+
yield 'Return content items that contain "foo" in name' => [
36+
'json',
37+
$this->buildJsonCriterionQuery('"ContentNameCriterion": "foo*"'),
38+
2,
39+
];
40+
41+
yield 'No content items found with article in name' => [
42+
'json',
43+
$this->buildJsonCriterionQuery('"ContentNameCriterion": "*article*"'),
44+
0,
45+
];
46+
}
47+
}

0 commit comments

Comments
 (0)