1 | <?php |
||
23 | class FieldNameResolver |
||
24 | { |
||
25 | /** |
||
26 | * Field registry. |
||
27 | * |
||
28 | * @var \eZ\Publish\Core\Search\Common\FieldRegistry |
||
29 | */ |
||
30 | protected $fieldRegistry; |
||
31 | |||
32 | /** |
||
33 | * Content type handler. |
||
34 | * |
||
35 | * @var \eZ\Publish\SPI\Persistence\Content\Type\Handler |
||
36 | */ |
||
37 | protected $contentTypeHandler; |
||
38 | |||
39 | /** |
||
40 | * Field name generator. |
||
41 | * |
||
42 | * @var \eZ\Publish\Core\Search\Common\FieldNameGenerator |
||
43 | */ |
||
44 | protected $nameGenerator; |
||
45 | |||
46 | /** |
||
47 | * Create from search field registry, content type handler and field name generator. |
||
48 | * |
||
49 | * @param \eZ\Publish\Core\Search\Common\FieldRegistry $fieldRegistry |
||
50 | * @param \eZ\Publish\SPI\Persistence\Content\Type\Handler $contentTypeHandler |
||
51 | * @param \eZ\Publish\Core\Search\Common\FieldNameGenerator $nameGenerator |
||
52 | */ |
||
53 | public function __construct( |
||
54 | FieldRegistry $fieldRegistry, |
||
55 | ContentTypeHandler $contentTypeHandler, |
||
56 | FieldNameGenerator $nameGenerator |
||
57 | ) { |
||
58 | $this->fieldRegistry = $fieldRegistry; |
||
59 | $this->contentTypeHandler = $contentTypeHandler; |
||
60 | $this->nameGenerator = $nameGenerator; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Get content type, field definition and field type mapping information. |
||
65 | * |
||
66 | * Returns an array in the form: |
||
67 | * |
||
68 | * <code> |
||
69 | * array( |
||
70 | * "<ContentType identifier>" => array( |
||
71 | * "<FieldDefinition identifier>" => array( |
||
72 | * "field_definition_id" => "<FieldDefinition id>", |
||
73 | * "field_type_identifier" => "<FieldType identifier>", |
||
74 | * ), |
||
75 | * ... |
||
76 | * ), |
||
77 | * ... |
||
78 | * ) |
||
79 | * </code> |
||
80 | * |
||
81 | * @return array[] |
||
82 | */ |
||
83 | protected function getSearchableFieldMap() |
||
84 | { |
||
85 | return $this->contentTypeHandler->getSearchableFieldMap(); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * For the given parameters returns a set of search backend field names to search on. |
||
90 | * |
||
91 | * The method will check for custom fields if given $criterion implements |
||
92 | * CustomFieldInterface. With optional parameters $fieldTypeIdentifier and |
||
93 | * $name specific field type and field from its Indexable implementation |
||
94 | * can be targeted. |
||
95 | * |
||
96 | * @deprecated since 6.2, use getFieldTypes instead |
||
97 | * |
||
98 | * @see \eZ\Publish\API\Repository\Values\Content\Query\CustomFieldInterface |
||
99 | * @see \eZ\Publish\SPI\FieldType\Indexable |
||
100 | * |
||
101 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
||
102 | * @param string $fieldDefinitionIdentifier |
||
103 | * @param null|string $fieldTypeIdentifier |
||
104 | * @param null|string $name |
||
105 | * |
||
106 | * @return string[] |
||
107 | */ |
||
108 | public function getFieldNames( |
||
109 | Criterion $criterion, |
||
110 | $fieldDefinitionIdentifier, |
||
111 | $fieldTypeIdentifier = null, |
||
112 | $name = null |
||
113 | ) { |
||
114 | $fieldTypeNameMap = $this->getFieldTypes($criterion, $fieldDefinitionIdentifier, $fieldTypeIdentifier, $name); |
||
115 | |||
116 | return array_keys($fieldTypeNameMap); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * For the given parameters returns a set of search backend field names/types to search on. |
||
121 | * |
||
122 | * The method will check for custom fields if given $criterion implements |
||
123 | * CustomFieldInterface. With optional parameters $fieldTypeIdentifier and |
||
124 | * $name specific field type and field from its Indexable implementation |
||
125 | * can be targeted. |
||
126 | * |
||
127 | * @see \eZ\Publish\API\Repository\Values\Content\Query\CustomFieldInterface |
||
128 | * @see \eZ\Publish\SPI\FieldType\Indexable |
||
129 | * |
||
130 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
||
131 | * @param string $fieldDefinitionIdentifier |
||
132 | * @param null|string $fieldTypeIdentifier |
||
133 | * @param null|string $name |
||
134 | * |
||
135 | * @return array<string, \eZ\Publish\SPI\Search\FieldType> |
||
|
|||
136 | */ |
||
137 | public function getFieldTypes( |
||
138 | Criterion $criterion, |
||
139 | $fieldDefinitionIdentifier, |
||
140 | $fieldTypeIdentifier = null, |
||
141 | $name = null |
||
142 | ) { |
||
143 | $fieldMap = $this->getSearchableFieldMap(); |
||
144 | $fieldTypeNameMap = []; |
||
145 | |||
146 | foreach ($fieldMap as $contentTypeIdentifier => $fieldIdentifierMap) { |
||
147 | // First check if field exists in the current ContentType, there is nothing to do if it doesn't |
||
148 | if (!isset($fieldIdentifierMap[$fieldDefinitionIdentifier])) { |
||
149 | continue; |
||
150 | } |
||
151 | |||
152 | // If $fieldTypeIdentifier is given it must match current field definition |
||
153 | if ( |
||
154 | $fieldTypeIdentifier !== null && |
||
155 | $fieldTypeIdentifier !== $fieldIdentifierMap[$fieldDefinitionIdentifier]['field_type_identifier'] |
||
156 | ) { |
||
157 | continue; |
||
158 | } |
||
159 | |||
160 | $fieldNameWithSearchType = $this->getIndexFieldName( |
||
161 | $criterion, |
||
162 | $contentTypeIdentifier, |
||
163 | $fieldDefinitionIdentifier, |
||
164 | $fieldIdentifierMap[$fieldDefinitionIdentifier]['field_type_identifier'], |
||
165 | $name, |
||
166 | false |
||
167 | ); |
||
168 | |||
169 | $fieldNames = array_keys($fieldNameWithSearchType); |
||
170 | $fieldName = reset($fieldNames); |
||
171 | |||
172 | $fieldTypeNameMap[$fieldName] = $fieldNameWithSearchType[$fieldName]; |
||
173 | } |
||
174 | |||
175 | return $fieldTypeNameMap; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * For the given parameters returns search backend field name to sort on or |
||
180 | * null if the field could not be found. |
||
181 | * |
||
182 | * The method will check for custom fields if given $sortClause implements |
||
183 | * CustomFieldInterface. With optional parameter $name specific field from |
||
184 | * field type's Indexable implementation can be targeted. |
||
185 | * |
||
186 | * Will return null if no sortable field is found. |
||
187 | * |
||
188 | * @see \eZ\Publish\API\Repository\Values\Content\Query\CustomFieldInterface |
||
189 | * @see \eZ\Publish\SPI\FieldType\Indexable |
||
190 | * |
||
191 | * @param \eZ\Publish\API\Repository\Values\Content\Query\SortClause $sortClause |
||
192 | * @param string $contentTypeIdentifier |
||
193 | * @param string $fieldDefinitionIdentifier |
||
194 | * @param null|string $name |
||
195 | * |
||
196 | * @return null|string |
||
197 | */ |
||
198 | public function getSortFieldName( |
||
222 | |||
223 | /** |
||
224 | * Returns index field name for the given parameters. |
||
225 | * |
||
226 | * @param object $criterionOrSortClause |
||
227 | * @param string $contentTypeIdentifier |
||
228 | * @param string $fieldDefinitionIdentifier |
||
229 | * @param string $fieldTypeIdentifier |
||
230 | * @param string $name |
||
231 | * @param bool $isSortField |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | public function getIndexFieldName( |
||
288 | } |
||
289 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.