Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-7418: Added input parser for ContentName criterion #82

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/bundle/Resources/config/input_parsers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions src/lib/Server/Input/Parser/Criterion/ContentName.php
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
);
}
}
}
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(),
]
),
];
}
}
47 changes: 47 additions & 0 deletions tests/bundle/Functional/SearchView/Criterion/ContentNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\Tests\Bundle\Rest\Functional\SearchView\Criterion;

use Ibexa\Tests\Bundle\Rest\Functional\SearchView\SearchCriterionTestCase;

/**
* @covers \Ibexa\Rest\Server\Input\Parser\Criterion\ContentName
*/
final class ContentNameTest extends SearchCriterionTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->createFolder('foo', '/api/ibexa/v2/content/locations/1/2');
$this->createFolder('foobar', '/api/ibexa/v2/content/locations/1/2');
}

/**
* @return iterable<array{
* string,
* string,
* int,
* }>
*/
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,
];
}
}
Loading