Completed
Push — api_content_name_shorthand ( 929e38...fc49b1 )
by André
20:47
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 View Code Duplication
    public function getFieldValue($fieldDefIdentifier, $languageCode = null)
74
    {
75
        if (null === $languageCode) {
76
            $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode;
77
        }
78
79
        if (isset($this->fields[$fieldDefIdentifier][$languageCode])) {
80
            return $this->fields[$fieldDefIdentifier][$languageCode];
81
        }
82
83
        return null;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getFields()
90
    {
91
        return $this->internalFields;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 View Code Duplication
    public function getFieldsByLanguage($languageCode = null)
98
    {
99
        $fields = array();
100
101
        if (null === $languageCode) {
102
            $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode;
103
        }
104
105
        foreach ($this->getFields() as $field) {
106
            if ($field->languageCode !== $languageCode) {
107
                continue;
108
            }
109
            $fields[$field->fieldDefIdentifier] = $field;
110
        }
111
112
        return $fields;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getField($fieldDefIdentifier, $languageCode = null)
119
    {
120
        if (null === $languageCode) {
121
            $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode;
122
        }
123
124
        foreach ($this->getFields() as $field) {
125
            if ($field->fieldDefIdentifier === $fieldDefIdentifier
126
                && $field->languageCode === $languageCode) {
127
                return $field;
128
            }
129
        }
130
131
        return null;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    protected function getProperties($dynamicProperties = array('id', 'contentInfo'))
138
    {
139
        return parent::getProperties($dynamicProperties);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function __get($property)
146
    {
147
        switch ($property) {
148
            case 'id':
149
                return $this->versionInfo->contentInfo->id;
150
151
            case 'contentInfo':
152
                return $this->versionInfo->contentInfo;
153
        }
154
155
        return parent::__get($property);
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function __isset($property)
162
    {
163
        if ($property === 'id') {
164
            return true;
165
        }
166
167
        if ($property === 'contentInfo') {
168
            return true;
169
        }
170
171
        return parent::__isset($property);
172
    }
173
}
174