Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

FieldNameResolver.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Search\Common;
8
9 use eZ\Publish\SPI\Persistence\Content\Type\Handler as ContentTypeHandler;
10 use eZ\Publish\API\Repository\Values\Content\Query\CustomFieldInterface;
11 use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
12 use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
13 use RuntimeException;
14
19 class FieldNameResolver
20 {
26 protected $fieldRegistry;
27
33 protected $contentTypeHandler;
34
40 protected $nameGenerator;
41
49 public function __construct(
50 FieldRegistry $fieldRegistry,
51 ContentTypeHandler $contentTypeHandler,
52 FieldNameGenerator $nameGenerator
53 ) {
54 $this->fieldRegistry = $fieldRegistry;
55 $this->contentTypeHandler = $contentTypeHandler;
56 $this->nameGenerator = $nameGenerator;
57 }
58
79 protected function getSearchableFieldMap()
80 {
81 return $this->contentTypeHandler->getSearchableFieldMap();
82 }
83
103 public function getFieldNames(
104 Criterion $criterion,
105 $fieldDefinitionIdentifier,
106 $fieldTypeIdentifier = null,
107 $name = null
108 ) {
109 $fieldTypeNameMap = $this->getFieldTypes($criterion, $fieldDefinitionIdentifier, $fieldTypeIdentifier, $name);
110
111 return array_keys($fieldTypeNameMap);
112 }
113
132 public function getFieldTypes(
133 Criterion $criterion,
134 $fieldDefinitionIdentifier,
135 $fieldTypeIdentifier = null,
136 $name = null
137 ) {
138 $fieldMap = $this->getSearchableFieldMap();
139 $fieldTypeNameMap = [];
140
141 foreach ($fieldMap as $contentTypeIdentifier => $fieldIdentifierMap) {
142 // First check if field exists in the current ContentType, there is nothing to do if it doesn't
143 if (!isset($fieldIdentifierMap[$fieldDefinitionIdentifier])) {
144 continue;
145 }
146
147 // If $fieldTypeIdentifier is given it must match current field definition
148 if (
149 $fieldTypeIdentifier !== null &&
150 $fieldTypeIdentifier !== $fieldIdentifierMap[$fieldDefinitionIdentifier]['field_type_identifier']
151 ) {
152 continue;
153 }
154
155 $fieldNameWithSearchType = $this->getIndexFieldName(
156 $criterion,
157 $contentTypeIdentifier,
158 $fieldDefinitionIdentifier,
159 $fieldIdentifierMap[$fieldDefinitionIdentifier]['field_type_identifier'],
160 $name,
161 false
162 );
163
164 $fieldNames = array_keys($fieldNameWithSearchType);
165 $fieldName = reset($fieldNames);
166
167 $fieldTypeNameMap[$fieldName] = $fieldNameWithSearchType[$fieldName];
168 }
169
170 return $fieldTypeNameMap;
171 }
172
193 public function getSortFieldName(
194 SortClause $sortClause,
195 $contentTypeIdentifier,
196 $fieldDefinitionIdentifier,
197 $name = null
198 ) {
199 $fieldMap = $this->getSearchableFieldMap();
200
201 // First check if field exists in type, there is nothing to do if it doesn't
202 if (!isset($fieldMap[$contentTypeIdentifier][$fieldDefinitionIdentifier])) {
203 return null;
204 }
205
206 $fieldName = array_keys($this->getIndexFieldName(
207 $sortClause,
208 $contentTypeIdentifier,
209 $fieldDefinitionIdentifier,
210 $fieldMap[$contentTypeIdentifier][$fieldDefinitionIdentifier]['field_type_identifier'],
211 $name,
212 true
213 ));
214
215 return reset($fieldName);
216 }
217
230 public function getIndexFieldName(
231 $criterionOrSortClause,
232 $contentTypeIdentifier,
233 $fieldDefinitionIdentifier,
234 $fieldTypeIdentifier,
235 $name,
236 $isSortField
237 ) {
238 // If criterion or sort clause implements CustomFieldInterface and custom field is set for
239 // ContentType/FieldDefinition, return it
240 if (
241 $criterionOrSortClause instanceof CustomFieldInterface &&
242 $customFieldName = $criterionOrSortClause->getCustomField(
243 $contentTypeIdentifier,
244 $fieldDefinitionIdentifier
245 )
246 ) {
247 return [$customFieldName => null];
248 }
249
250 // Else, generate field name from field type's index definition
251
252 $indexFieldType = $this->fieldRegistry->getType($fieldTypeIdentifier);
253
254 // If $name is not given use default field name
255 if ($name === null) {
256 if ($isSortField) {
257 $name = $indexFieldType->getDefaultSortField();
258 } else {
259 $name = $indexFieldType->getDefaultMatchField();
260 }
261 }
262
263 $indexDefinition = $indexFieldType->getIndexDefinition();
264
265 // Should only happen by mistake, so let's throw if it does
266 if (!isset($indexDefinition[$name])) {
267 throw new RuntimeException(
268 "Could not find '{$name}' field in '{$fieldTypeIdentifier}' field type's index definition"
269 );
270 }
271
272 $field = $this->nameGenerator->getTypedName(
273 $this->nameGenerator->getName(
274 $name,
275 $fieldDefinitionIdentifier,
276 $contentTypeIdentifier
277 ),
278 $indexDefinition[$name]
279 );
280
281 return [$field => $indexDefinition[$name]];
282 }
283 }
eZ\Publish\API\Repository\Values\Content\Query\Criterion
Definition: API/Repository/Values/Content/Query/Criterion.php:15
eZ\Publish\API\Repository\Values\Content\Query\SortClause
This class is the base for SortClause classes, used to set sorting of content queries.
Definition: Content/Query/SortClause.php:16
eZ\Publish\Core\Search\Common\FieldNameGenerator
Generator for search backend field names.
Definition: FieldNameGenerator.php:15
eZ\Publish\Core\Search\Common\FieldNameResolver
Provides search backend field name resolving for criteria and sort clauses targeting Content fields.
Definition: FieldNameResolver.php:20
eZ\Publish\Core\Search\Common\FieldNameResolver\$nameGenerator
$nameGenerator
Definition: FieldNameResolver.php:40
eZ\Publish\Core\Search\Common\FieldNameResolver\getFieldNames
getFieldNames(Criterion $criterion, $fieldDefinitionIdentifier, $fieldTypeIdentifier=null, $name=null)
For the given parameters returns a set of search backend field names to search on.
Definition: FieldNameResolver.php:103
eZ\Publish\Core\Search\Common\FieldNameResolver\__construct
__construct(FieldRegistry $fieldRegistry, ContentTypeHandler $contentTypeHandler, FieldNameGenerator $nameGenerator)
Create from search field registry, content type handler and field name generator.
Definition: FieldNameResolver.php:49
eZ\Publish\Core\Search\Common\FieldNameResolver\getIndexFieldName
getIndexFieldName( $criterionOrSortClause, $contentTypeIdentifier, $fieldDefinitionIdentifier, $fieldTypeIdentifier, $name, $isSortField)
Returns index field name for the given parameters.
Definition: FieldNameResolver.php:230
eZ\Publish\Core\Search\Common\FieldNameResolver\getSortFieldName
getSortFieldName(SortClause $sortClause, $contentTypeIdentifier, $fieldDefinitionIdentifier, $name=null)
For the given parameters returns search backend field name to sort on or null if the field could not ...
Definition: FieldNameResolver.php:193
eZ\Publish\Core\Search\Common\FieldNameResolver\$fieldRegistry
$fieldRegistry
Definition: FieldNameResolver.php:26
eZ\Publish\Core\Search\Common\FieldNameResolver\$contentTypeHandler
$contentTypeHandler
Definition: FieldNameResolver.php:33
eZ\Publish\Core\Search\Common\FieldNameResolver\getFieldTypes
getFieldTypes(Criterion $criterion, $fieldDefinitionIdentifier, $fieldTypeIdentifier=null, $name=null)
For the given parameters returns a set of search backend field names/types to search on.
Definition: FieldNameResolver.php:132
eZ\Publish\Core\Search\Common\FieldNameResolver\getSearchableFieldMap
getSearchableFieldMap()
Get content type, field definition and field type mapping information.
Definition: FieldNameResolver.php:79
eZ\Publish\Core\Search\Common\FieldRegistry
Registry for field type's Indexable interface implementations available to Search Engines.
Definition: FieldRegistry.php:15
eZ\Publish\API\Repository\Values\Content\Query\CustomFieldInterface
Interface for criteria and sort clauses, which defines a custom field mapping.
Definition: CustomFieldInterface.php:18
eZ\Publish\API\Repository\Values\Content\Query\CustomFieldInterface\getCustomField
getCustomField($type, $field)
Return the custom field to query or sort on if set.
eZ\Publish\SPI\Persistence\Content\Type\Handler
Definition: SPI/Persistence/Content/Type/Handler.php:14
eZ\Publish\API\Repository\Values\Content\Query\Criterion
Definition: API/Repository/Values/Content/Query/Criterion/Ancestor.php:7
eZ\Publish\API\Repository\Values\Content\Query\SortClause
Definition: API/Repository/Values/Content/Query/SortClause/ContentId.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.