Completed
Push — ezp-30882-thumbnail ( d9f236...3bd997 )
by
unknown
30:29 queued 14:17
created

Content::getThumbnail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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