|
| 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 | +} |
0 commit comments