Completed
Push — lazy_content_type ( 8d23f7...0f043a )
by André
11:01
created

Content::populateFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 10
rs 9.4285
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
13
/**
14
 * this class represents a content object in a specific version.
15
 *
16
 * @property-read \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo convenience getter for $versionInfo->contentInfo
17
 * @property-read \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType convenience getter for $versionInfo->contentInfo->contentType
18
 * @property-read mixed $id convenience getter for retrieving the contentId: $versionInfo->content->id
19
 * @property-read \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo calls getVersionInfo()
20
 * @property-read \eZ\Publish\API\Repository\Values\Content\Field[] $fields Access fields, calls getFields()
21
 *
22
 * @internal Meant for internal use by Repository, type hint against API object instead.
23
 */
24
class Content extends APIContent
25
{
26
    /**
27
     * @var mixed[][] An array of array of field values like[$fieldDefIdentifier][$languageCode]
28
     */
29
    protected $fields;
30
31
    /**
32
     * @var \eZ\Publish\API\Repository\Values\Content\VersionInfo
33
     */
34
    protected $versionInfo;
35
36
    /**
37
     * @var \eZ\Publish\API\Repository\Values\Content\Field[] An array of {@link Field}
38
     */
39
    private $internalFields = array();
40
41
    /**
42
     * The first matched field language among user provided prioritized languages.
43
     *
44
     * The first matched language among user provided prioritized languages on object retrieval, or null if none
45
     * provided (all languages) or on main fallback.
46
     *
47
     * @internal
48
     * @var string|null
49
     */
50
    protected $prioritizedFieldLanguageCode;
51
52
    public function __construct(array $data = array())
53
    {
54
        foreach ($data as $propertyName => $propertyValue) {
55
            $this->$propertyName = $propertyValue;
56
        }
57
    }
58
59
    private function populateFields()
60
    {
61
        if (!empty($this->fields)) {
62
            return;
63
        }
64
65
        foreach ($this->internalFields as $field) {
66
            $this->fields[$field->fieldDefIdentifier][$field->languageCode] = $field->value;
0 ignored issues
show
Documentation introduced by
The property $value is declared protected in eZ\Publish\API\Repository\Values\Content\Field. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getVersionInfo()
74
    {
75
        return $this->versionInfo;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getFieldValue($fieldDefIdentifier, $languageCode = null)
82
    {
83
        if (null === $languageCode) {
84
            $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode;
85
        }
86
87
        $this->populateFields();
88
        if (isset($this->fields[$fieldDefIdentifier][$languageCode])) {
89
            return $this->fields[$fieldDefIdentifier][$languageCode];
90
        }
91
92
        return null;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getFields()
99
    {
100
        return $this->internalFields;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 View Code Duplication
    public function getFieldsByLanguage($languageCode = null)
107
    {
108
        $fields = array();
109
110
        if (null === $languageCode) {
111
            $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode;
112
        }
113
114
        foreach ($this->getFields() as $field) {
115
            if ($field->languageCode !== $languageCode) {
116
                continue;
117
            }
118
            $fields[$field->fieldDefIdentifier] = $field;
119
        }
120
121
        return $fields;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getField($fieldDefIdentifier, $languageCode = null)
128
    {
129
        if (null === $languageCode) {
130
            $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode;
131
        }
132
133
        foreach ($this->getFields() as $field) {
134
            if ($field->fieldDefIdentifier === $fieldDefIdentifier
135
                && $field->languageCode === $languageCode) {
136
                return $field;
137
            }
138
        }
139
140
        return null;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    protected function getProperties($dynamicProperties = array('id', 'contentInfo'))
147
    {
148
        return parent::getProperties($dynamicProperties);
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function __get($property)
155
    {
156
        switch ($property) {
157
            case 'id':
158
                return $this->versionInfo->contentInfo->id;
159
160
            case 'contentInfo':
161
                return $this->versionInfo->contentInfo;
162
163
            case 'fields':
164
                $this->populateFields();// let lookup be done in parent now that it's been loaded.
165
        }
166
167
        return parent::__get($property);
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function __isset($property)
174
    {
175
        if ($property === 'id') {
176
            return true;
177
        }
178
179
        if ($property === 'contentInfo') {
180
            return true;
181
        }
182
183
        return parent::__isset($property);
184
    }
185
}
186