Completed
Push — api_content_name_shorthand ( e0a75f )
by André
17:01
created

Content::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
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
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 View Code Duplication
    public function __construct(array $data = array())
53
    {
54
        foreach ($data as $propertyName => $propertyValue) {
55
            $this->$propertyName = $propertyValue;
56
        }
57
        foreach ($this->internalFields as $field) {
58
            $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...
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getVersionInfo()
66
    {
67
        return $this->versionInfo;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getName($languageCode = null)
74
    {
75
        return $this->versionInfo->getName($languageCode);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 View Code Duplication
    public function getFieldValue($fieldDefIdentifier, $languageCode = null)
82
    {
83
        if (null === $languageCode) {
84
            $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode;
85
        }
86
87
        if (isset($this->fields[$fieldDefIdentifier][$languageCode])) {
88
            return $this->fields[$fieldDefIdentifier][$languageCode];
89
        }
90
91
        return null;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getFields()
98
    {
99
        return $this->internalFields;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 View Code Duplication
    public function getFieldsByLanguage($languageCode = null)
106
    {
107
        $fields = array();
108
109
        if (null === $languageCode) {
110
            $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode;
111
        }
112
113
        foreach ($this->getFields() as $field) {
114
            if ($field->languageCode !== $languageCode) {
115
                continue;
116
            }
117
            $fields[$field->fieldDefIdentifier] = $field;
118
        }
119
120
        return $fields;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getField($fieldDefIdentifier, $languageCode = null)
127
    {
128
        if (null === $languageCode) {
129
            $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode;
130
        }
131
132
        foreach ($this->getFields() as $field) {
133
            if ($field->fieldDefIdentifier === $fieldDefIdentifier
134
                && $field->languageCode === $languageCode) {
135
                return $field;
136
            }
137
        }
138
139
        return null;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    protected function getProperties($dynamicProperties = array('id', 'contentInfo'))
146
    {
147
        return parent::getProperties($dynamicProperties);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function __get($property)
154
    {
155
        switch ($property) {
156
            case 'id':
157
                return $this->versionInfo->contentInfo->id;
158
159
            case 'contentInfo':
160
                return $this->versionInfo->contentInfo;
161
        }
162
163
        return parent::__get($property);
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function __isset($property)
170
    {
171
        if ($property === 'id') {
172
            return true;
173
        }
174
175
        if ($property === 'contentInfo') {
176
            return true;
177
        }
178
179
        return parent::__isset($property);
180
    }
181
}
182