Completed
Push — ezp-30882-thumbnail ( 274ed9 )
by
unknown
19:23
created

Content::__get()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the eZ\Publish\Core\Repository\Values\Content\Content class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository\Values\Content;
10
11
use eZ\Publish\API\Repository\Values\Content\Content as APIContent;
12
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
13
use eZ\Publish\Core\Repository\Strategy\ContentThumbnail\ThumbnailChainStrategy;
14
use eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\ThumbnailStrategy;
15
16
/**
17
 * this class represents a content object in a specific version.
18
 *
19
 * @property-read \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo convenience getter for $versionInfo->contentInfo
20
 * @property-read \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType convenience getter for $versionInfo->contentInfo->contentType
21
 * @property-read mixed $id convenience getter for retrieving the contentId: $versionInfo->content->id
22
 * @property-read \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo calls getVersionInfo()
23
 * @property-read \eZ\Publish\API\Repository\Values\Content\Field[] $fields Access fields, calls getFields()
24
 *
25
 * @internal Meant for internal use by Repository, type hint against API object instead.
26
 */
27
class Content extends APIContent
28
{
29
    /** @var string|null */
30
    protected $thumbnail;
31
32
    /** @var mixed[][] An array of array of field values like[$fieldDefIdentifier][$languageCode] */
33
    protected $fields;
34
35
    /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo */
36
    protected $versionInfo;
37
38
    /** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType */
39
    protected $contentType;
40
41
    /** @var \eZ\Publish\API\Repository\Values\Content\Field[] An array of {@link Field} */
42
    private $internalFields = [];
43
44
    /**
45
     * The first matched field language among user provided prioritized languages.
46
     *
47
     * The first matched language among user provided prioritized languages on object retrieval, or null if none
48
     * provided (all languages) or on main fallback.
49
     *
50
     * @internal
51
     * @var string|null
52
     */
53
    protected $prioritizedFieldLanguageCode;
54
55
    public function __construct(array $data = [])
56
    {
57
        foreach ($data as $propertyName => $propertyValue) {
58
            $this->$propertyName = $propertyValue;
59
        }
60
        foreach ($this->internalFields as $field) {
61
            $this->fields[$field->fieldDefIdentifier][$field->languageCode] = $field->value;
62
        }
63
    }
64
65
    public function getThumbnail(): ?string
66
    {
67
        return $this->thumbnail;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getVersionInfo()
74
    {
75
        return $this->versionInfo;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getContentType(): ContentType
82
    {
83
        return $this->contentType;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getFieldValue($fieldDefIdentifier, $languageCode = null)
90
    {
91
        if (null === $languageCode) {
92
            $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode;
93
        }
94
95
        if (isset($this->fields[$fieldDefIdentifier][$languageCode])) {
96
            return $this->fields[$fieldDefIdentifier][$languageCode];
97
        }
98
99
        return null;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getFields()
106
    {
107
        return $this->internalFields;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getFieldsByLanguage($languageCode = null)
114
    {
115
        $fields = [];
116
117
        if (null === $languageCode) {
118
            $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode;
119
        }
120
121
        foreach ($this->getFields() as $field) {
122
            if ($field->languageCode !== $languageCode) {
123
                continue;
124
            }
125
            $fields[$field->fieldDefIdentifier] = $field;
126
        }
127
128
        return $fields;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function getField($fieldDefIdentifier, $languageCode = null)
135
    {
136
        if (null === $languageCode) {
137
            $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode;
138
        }
139
140
        foreach ($this->getFields() as $field) {
141
            if ($field->fieldDefIdentifier === $fieldDefIdentifier
142
                && $field->languageCode === $languageCode) {
143
                return $field;
144
            }
145
        }
146
147
        return null;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    protected function getProperties($dynamicProperties = ['id', 'contentInfo'])
154
    {
155
        return parent::getProperties($dynamicProperties);
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function __get($property)
162
    {
163
        switch ($property) {
164
            case 'id':
165
                return $this->versionInfo->contentInfo->id;
166
167
            case 'contentInfo':
168
                return $this->versionInfo->contentInfo;
169
170
            case 'thumbnail':
171
                return $this->getThumbnail();
172
        }
173
174
        return parent::__get($property);
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function __isset($property)
181
    {
182
        if ($property === 'id') {
183
            return true;
184
        }
185
186
        if ($property === 'contentInfo') {
187
            return true;
188
        }
189
190
        return parent::__isset($property);
191
    }
192
}
193