Completed
Push — EZP-31287 ( b1af06...dd5eed )
by
unknown
19:50
created

FullTextMapper::getFullTextFieldData()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 2
dl 0
loc 26
rs 9.504
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\FieldType;
14
use eZ\Publish\SPI\Persistence\Content\Type\Handler as ContentTypeHandler;
15
use eZ\Publish\Core\Search\Legacy\Content\FullTextValue;
16
17
/**
18
 * FullTextMapper maps Content object fields to FullTextValue objects which are searchable and
19
 * therefore can be indexed by the legacy search engine.
20
 */
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
            list($value, $transformationRules, $splitFlag) = $this->getFullTextFieldData($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' => $transformationRules,
105
                    'splitFlag' => $splitFlag,
106
                ]
107
            );
108
        }
109
110
        return $fullTextValues;
111
    }
112
113
    private function getFullTextFieldData(Content\Field $field, Type\FieldDefinition $fieldDefinition): array
114
    {
115
        $fieldType = $this->fieldRegistry->getType($field->type);
116
        $indexFields = $fieldType->getIndexData($field, $fieldDefinition);
117
118
        // find value to be returned (stored in FullTextField)
119
        $fullTextFieldValue = '';
120
        $fullTextFieldTransformationRules = [];
121
        $fullTextFieldSplitFlag = true;
122
        foreach ($indexFields as $field) {
123
            /** @var \eZ\Publish\SPI\Search\Field $field */
124
            if ($field->type instanceof FieldType\FullTextField) {
125
                $fullTextFieldValue = $field->value;
126
                $fullTextFieldTransformationRules = $field->type->transformationRules;
127
                $fullTextFieldSplitFlag = $field->type->splitFlag;
128
                break;
129
            }
130
        }
131
132
        // some full text fields are stored as an array of strings
133
        return [
134
            !is_array($fullTextFieldValue) ? $fullTextFieldValue : implode(' ', $fullTextFieldValue),
135
            $fullTextFieldTransformationRules,
136
            $fullTextFieldSplitFlag,
137
        ];
138
    }
139
}
140