1 | <?php |
||
21 | class FullTextMapper |
||
22 | { |
||
23 | /** |
||
24 | * Field registry. |
||
25 | * |
||
26 | * @var \eZ\Publish\Core\Search\Common\FieldRegistry |
||
27 | */ |
||
28 | protected $fieldRegistry; |
||
29 | |||
30 | /** |
||
31 | * Content type handler. |
||
32 | * |
||
33 | * @var \eZ\Publish\SPI\Persistence\Content\Type\Handler |
||
34 | */ |
||
35 | protected $contentTypeHandler; |
||
36 | |||
37 | /** |
||
38 | * @param \eZ\Publish\Core\Search\Common\FieldRegistry $fieldRegistry |
||
39 | * @param \eZ\Publish\SPI\Persistence\Content\Type\Handler $contentTypeHandler |
||
40 | */ |
||
41 | public function __construct( |
||
42 | FieldRegistry $fieldRegistry, |
||
43 | ContentTypeHandler $contentTypeHandler |
||
44 | ) { |
||
45 | $this->fieldRegistry = $fieldRegistry; |
||
46 | $this->contentTypeHandler = $contentTypeHandler; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Map given Content to a FullTextValue. |
||
51 | * |
||
52 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
53 | * |
||
54 | * @return \eZ\Publish\Core\Search\Legacy\Content\FullTextData |
||
55 | */ |
||
56 | public function mapContent(Content $content) |
||
57 | { |
||
58 | return new FullTextData( |
||
59 | [ |
||
60 | 'id' => $content->versionInfo->contentInfo->id, |
||
61 | 'contentTypeId' => $content->versionInfo->contentInfo->contentTypeId, |
||
62 | 'sectionId' => $content->versionInfo->contentInfo->sectionId, |
||
63 | 'published' => $content->versionInfo->contentInfo->publicationDate, |
||
64 | 'values' => $this->getFullTextValues($content), |
||
65 | ] |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Returns an array of FullTextValue object containing searchable values of content object |
||
71 | * fields for the given $content. |
||
72 | * |
||
73 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
74 | * |
||
75 | * @return \eZ\Publish\Core\Search\Legacy\Content\FullTextValue[] |
||
76 | */ |
||
77 | protected function getFullTextValues(Content $content) |
||
78 | { |
||
79 | $fullTextValues = []; |
||
80 | foreach ($content->fields as $field) { |
||
81 | $fieldDefinition = $this->contentTypeHandler->getFieldDefinition( |
||
82 | $field->fieldDefinitionId, Content\Type::STATUS_DEFINED |
||
83 | ); |
||
84 | if (!$fieldDefinition->isSearchable) { |
||
85 | continue; |
||
86 | } |
||
87 | |||
88 | $value = $this->getFullTextFieldValue($field, $fieldDefinition); |
||
89 | if (empty($value)) { |
||
90 | continue; |
||
91 | } |
||
92 | |||
93 | $contentInfo = $content->versionInfo->contentInfo; |
||
94 | $fullTextValues[] = new FullTextValue( |
||
95 | [ |
||
96 | 'id' => $field->id, |
||
97 | 'fieldDefinitionId' => $field->fieldDefinitionId, |
||
98 | 'fieldDefinitionIdentifier' => $fieldDefinition->identifier, |
||
99 | 'languageCode' => $field->languageCode, |
||
100 | 'value' => !is_array($value) ? $value : implode(' ', $value), |
||
101 | 'isMainAndAlwaysAvailable' => ( |
||
102 | $field->languageCode === $contentInfo->mainLanguageCode && $contentInfo->alwaysAvailable |
||
103 | ), |
||
104 | 'transformationRules' => $this->getTransformationRules($fieldDefinition), |
||
105 | 'splitFlag' => $this->getSplitFlag($fieldDefinition), |
||
106 | ] |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | return $fullTextValues; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Get FullTextField value. |
||
115 | * |
||
116 | * @param Content\Field $field |
||
117 | * @param Type\FieldDefinition $fieldDefinition |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | private function getFullTextFieldValue(Content\Field $field, Type\FieldDefinition $fieldDefinition) |
||
139 | |||
140 | /** |
||
141 | * Get transformation rules based on a given field type. |
||
142 | */ |
||
143 | private function getTransformationRules(Type\FieldDefinition $fieldDefinition): array |
||
154 | |||
155 | /** |
||
156 | * Get transformation rules based on a given field type. |
||
157 | */ |
||
158 | private function getSplitFlag(Type\FieldDefinition $fieldDefinition): bool |
||
166 | } |
||
167 |