Skip to content

Commit 5499eef

Browse files
authored
IBX-6620: Added Image criterion visitors (#55)
1 parent 73c3dcc commit 5499eef

File tree

15 files changed

+447
-37
lines changed

15 files changed

+447
-37
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"require-dev": {
2929
"symfony/proxy-manager-bridge": "^5.4",
30+
"symfony/phpunit-bridge": "^5.4",
3031
"ibexa/doctrine-schema": "~4.6.0@dev",
3132
"phpunit/phpunit": "^8.2",
3233
"matthiasnoback/symfony-dependency-injection-test": "^4.1",

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,8 +2249,3 @@ parameters:
22492249
message: "#^Parameter \\#1 \\$contentObjects of method Ibexa\\\\Solr\\\\Handler\\:\\:bulkIndexContent\\(\\) expects array\\<Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\>, iterable\\<Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\> given\\.$#"
22502250
count: 1
22512251
path: tests/lib/SetupFactory/LegacySetupFactory.php
2252-
2253-
-
2254-
message: "#^Parameter \\#1 \\$paths of class Symfony\\\\Component\\\\Config\\\\FileLocator constructor expects array\\<string\\>\\|string, string\\|false given\\.$#"
2255-
count: 2
2256-
path: tests/lib/SetupFactory/LegacySetupFactory.php
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\Contracts\Solr\Test;
10+
11+
use Ibexa\Bundle\Solr\IbexaSolrBundle;
12+
use Ibexa\Contracts\Core\Search\Handler;
13+
use Ibexa\Contracts\Core\Test\IbexaTestKernel as BaseIbexaTestKernel;
14+
use Ibexa\Solr\Handler as SolrHandler;
15+
use Ibexa\Solr\Test\SolrTestContainerBuilder;
16+
use Symfony\Component\Config\Loader\LoaderInterface;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
19+
/**
20+
* @internal
21+
*
22+
* Exposed in contracts to be able to run tests from ibexa/core.
23+
*/
24+
final class IbexaSolrTestKernel extends BaseIbexaTestKernel
25+
{
26+
public function registerBundles(): iterable
27+
{
28+
yield from parent::registerBundles();
29+
30+
yield new IbexaSolrBundle();
31+
}
32+
33+
protected static function getExposedServicesById(): iterable
34+
{
35+
yield from parent::getExposedServicesById();
36+
37+
yield SolrHandler::class => Handler::class;
38+
}
39+
40+
public function registerContainerConfiguration(LoaderInterface $loader): void
41+
{
42+
parent::registerContainerConfiguration($loader);
43+
44+
$loader->load(static function (ContainerBuilder $container): void {
45+
(new SolrTestContainerBuilder())->loadSolrSettings($container);
46+
});
47+
}
48+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\Solr\Query\Image\CriterionVisitor;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
12+
use Ibexa\Contracts\Solr\Query\CriterionVisitor;
13+
14+
abstract class AbstractImageRangeVisitor extends AbstractImageVisitor
15+
{
16+
public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null): string
17+
{
18+
/** @var array{0: int, 1?: int|null} $criterionValue */
19+
$criterionValue = $criterion->value;
20+
$queries = [];
21+
22+
foreach ($this->getSearchFieldNames($criterion) as $fieldName) {
23+
$queries[] = $fieldName . ':' . $this->getRange(
24+
$criterion->operator,
25+
$criterionValue[0],
26+
$criterionValue[1] ?? null
27+
);
28+
}
29+
30+
return '(' . implode(' OR ', $queries) . ')';
31+
}
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Solr\Query\Image\CriterionVisitor;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
12+
use Ibexa\Contracts\Solr\Query\CriterionVisitor;
13+
14+
abstract class AbstractImageTermsVisitor extends AbstractImageVisitor
15+
{
16+
public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null): string
17+
{
18+
$queries = [];
19+
/** @var array<string>|string $criterionValue */
20+
$criterionValue = $criterion->value;
21+
22+
foreach ($this->getSearchFieldNames($criterion) as $fieldName) {
23+
if (is_array($criterionValue)) {
24+
foreach ($criterionValue as $value) {
25+
$queries[] = $fieldName . ':' . $value;
26+
}
27+
}
28+
29+
if (is_string($criterionValue)) {
30+
$queries[] = $fieldName . ':' . $criterionValue;
31+
}
32+
}
33+
34+
return '(' . implode(' OR ', $queries) . ')';
35+
}
36+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\Solr\Query\Image\CriterionVisitor;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
12+
use Ibexa\Contracts\Solr\Query\CriterionVisitor;
13+
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
14+
use Ibexa\Core\FieldType\Image\Type;
15+
use Ibexa\Core\Search\Common\FieldNameResolver;
16+
17+
abstract class AbstractImageVisitor extends CriterionVisitor
18+
{
19+
private FieldNameResolver $fieldNameResolver;
20+
21+
private Type $imageFieldType;
22+
23+
public function __construct(
24+
FieldNameResolver $fieldNameResolver,
25+
Type $imageFieldType
26+
) {
27+
$this->fieldNameResolver = $fieldNameResolver;
28+
$this->imageFieldType = $imageFieldType;
29+
}
30+
31+
abstract protected function getSearchFieldName(): string;
32+
33+
/**
34+
* @return array<string>
35+
*
36+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
37+
*/
38+
protected function getSearchFieldNames(Criterion $criterion): array
39+
{
40+
$searchFieldNames = array_keys(
41+
$this->fieldNameResolver->getFieldTypes(
42+
$criterion,
43+
$criterion->target,
44+
$this->imageFieldType->getFieldTypeIdentifier(),
45+
$this->getSearchFieldName()
46+
)
47+
);
48+
49+
if (empty($searchFieldNames)) {
50+
throw new InvalidArgumentException(
51+
'$criterion->target',
52+
"No searchable Fields found for the provided Criterion target '{$criterion->target}'."
53+
);
54+
}
55+
56+
return $searchFieldNames;
57+
}
58+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\Solr\Query\Image\CriterionVisitor;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
12+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
13+
14+
final class FileSize extends AbstractImageRangeVisitor
15+
{
16+
private const SEARCH_FIELD_FILE_SIZE = 'file_size';
17+
18+
public function canVisit(Criterion $criterion): bool
19+
{
20+
return $criterion instanceof Criterion\Image\FileSize
21+
&& (
22+
$criterion->operator === Operator::BETWEEN
23+
|| $criterion->operator === Operator::GTE
24+
);
25+
}
26+
27+
protected function getSearchFieldName(): string
28+
{
29+
return self::SEARCH_FIELD_FILE_SIZE;
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\Solr\Query\Image\CriterionVisitor;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
12+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
13+
14+
final class Height extends AbstractImageRangeVisitor
15+
{
16+
private const SEARCH_FIELD_HEIGHT = 'height';
17+
18+
public function canVisit(Criterion $criterion): bool
19+
{
20+
return $criterion instanceof Criterion\Image\Height
21+
&& (
22+
$criterion->operator === Operator::BETWEEN
23+
|| $criterion->operator === Operator::GTE
24+
);
25+
}
26+
27+
protected function getSearchFieldName(): string
28+
{
29+
return self::SEARCH_FIELD_HEIGHT;
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\Solr\Query\Image\CriterionVisitor;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
12+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
13+
14+
final class MimeType extends AbstractImageTermsVisitor
15+
{
16+
private const SEARCH_FIELD_MIME_TYPE = 'mime_type';
17+
18+
public function canVisit(Criterion $criterion): bool
19+
{
20+
return $criterion instanceof Criterion\Image\MimeType
21+
&& (
22+
$criterion->operator === Operator::EQ
23+
|| $criterion->operator === Operator::IN
24+
);
25+
}
26+
27+
protected function getSearchFieldName(): string
28+
{
29+
return self::SEARCH_FIELD_MIME_TYPE;
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\Solr\Query\Image\CriterionVisitor;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
12+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
13+
14+
final class Orientation extends AbstractImageTermsVisitor
15+
{
16+
private const SEARCH_FIELD_ORIENTATION = 'orientation';
17+
18+
public function canVisit(Criterion $criterion): bool
19+
{
20+
return $criterion instanceof Criterion\Image\Orientation
21+
&& (
22+
$criterion->operator === Operator::EQ
23+
|| $criterion->operator === Operator::IN
24+
);
25+
}
26+
27+
protected function getSearchFieldName(): string
28+
{
29+
return self::SEARCH_FIELD_ORIENTATION;
30+
}
31+
}

0 commit comments

Comments
 (0)