Completed
Push — EZP-31287 ( dd5eed...0e16cc )
by
unknown
21:24
created

FullTextMapper::extractFullTextField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Search\Legacy\Content\Mapper;
8
9
use eZ\Publish\Core\Search\Common\FieldRegistry;
10
use eZ\Publish\Core\Search\Legacy\Content\FullTextData;
11
use eZ\Publish\SPI\Persistence\Content;
12
use eZ\Publish\SPI\Persistence\Content\Type;
13
use eZ\Publish\SPI\Search\Field;
14
use eZ\Publish\SPI\Search\FieldType;
15
use eZ\Publish\SPI\Persistence\Content\Type\Handler as ContentTypeHandler;
16
use eZ\Publish\Core\Search\Legacy\Content\FullTextValue;
17
18
/**
19
 * FullTextMapper maps Content object fields to FullTextValue objects which are searchable and
20
 * therefore can be indexed by the legacy search engine.
21
 */
22
class FullTextMapper
23
{
24
    /**
25
     * Field registry.
26
     *
27
     * @var \eZ\Publish\Core\Search\Common\FieldRegistry
28
     */
29
    protected $fieldRegistry;
30
31
    /**
32
     * Content type handler.
33
     *
34
     * @var \eZ\Publish\SPI\Persistence\Content\Type\Handler
35
     */
36
    protected $contentTypeHandler;
37
38
    /**
39
     * @param \eZ\Publish\Core\Search\Common\FieldRegistry $fieldRegistry
40
     * @param \eZ\Publish\SPI\Persistence\Content\Type\Handler $contentTypeHandler
41
     */
42
    public function __construct(
43
        FieldRegistry $fieldRegistry,
44
        ContentTypeHandler $contentTypeHandler
45
    ) {
46
        $this->fieldRegistry = $fieldRegistry;
47
        $this->contentTypeHandler = $contentTypeHandler;
48
    }
49
50
    /**
51
     * Map given Content to a FullTextValue.
52
     *
53
     * @param \eZ\Publish\SPI\Persistence\Content $content
54
     *
55
     * @return \eZ\Publish\Core\Search\Legacy\Content\FullTextData
56
     */
57
    public function mapContent(Content $content)
58
    {
59
        return new FullTextData(
60
            [
61
                'id' => $content->versionInfo->contentInfo->id,
62
                'contentTypeId' => $content->versionInfo->contentInfo->contentTypeId,
63
                'sectionId' => $content->versionInfo->contentInfo->sectionId,
64
                'published' => $content->versionInfo->contentInfo->publicationDate,
65
                'values' => $this->getFullTextValues($content),
66
            ]
67
        );
68
    }
69
70
    /**
71
     * Returns an array of FullTextValue object containing searchable values of content object
72
     * fields for the given $content.
73
     *
74
     * @param \eZ\Publish\SPI\Persistence\Content $content
75
     *
76
     * @return \eZ\Publish\Core\Search\Legacy\Content\FullTextValue[]
77
     *
78
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
79
     */
80
    protected function getFullTextValues(Content $content): array
81
    {
82
        $fullTextValues = [];
83
        foreach ($content->fields as $field) {
84
            $fieldDefinition = $this->contentTypeHandler->getFieldDefinition(
85
                $field->fieldDefinitionId, Content\Type::STATUS_DEFINED
86
            );
87
            if (!$fieldDefinition->isSearchable) {
88
                continue;
89
            }
90
91
            $fullTextField = $this->extractFullTextField($field, $fieldDefinition);
92
            if (null === $fullTextField || empty($fullTextField->value)) {
93
                continue;
94
            }
95
            $fullTextValue = !is_array($fullTextField->value)
96
                ? $fullTextField->value
97
                : implode(' ', $fullTextField->value);
98
99
            $contentInfo = $content->versionInfo->contentInfo;
100
            $fullTextValues[] = new FullTextValue(
101
                [
102
                    'id' => $field->id,
103
                    'fieldDefinitionId' => $field->fieldDefinitionId,
104
                    'fieldDefinitionIdentifier' => $fieldDefinition->identifier,
105
                    'languageCode' => $field->languageCode,
106
                    'value' => $fullTextValue,
107
                    'isMainAndAlwaysAvailable' => (
108
                        $field->languageCode === $contentInfo->mainLanguageCode && $contentInfo->alwaysAvailable
109
                    ),
110
                    'transformationRules' => $fullTextField->type->transformationRules,
111
                    'splitFlag' => $fullTextField->type->splitFlag,
112
                ]
113
            );
114
        }
115
116
        return $fullTextValues;
117
    }
118
119
    private function extractFullTextField(
120
        Content\Field $field,
121
        Type\FieldDefinition $fieldDefinition
122
    ): ?Field {
123
        $fieldType = $this->fieldRegistry->getType($field->type);
124
        $fullTextFields = array_filter(
125
            $fieldType->getIndexData($field, $fieldDefinition),
126
            static function ($indexField) {
127
                return $indexField->type instanceof FieldType\FullTextField;
128
            }
129
        );
130
131
        return !empty($fullTextFields) ? array_values($fullTextFields)[0] : null;
132
    }
133
}
134