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