Completed
Push — ezp_30981_content_info_proxy ( 9b541b...e47d06 )
by
unknown
14:30
created

VersionInfo::getLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 4
c 0
b 0
f 0
cc 1
nop 0
rs 10
1
<?php
2
3
/**
4
 * File containing the eZ\Publish\Core\Repository\Values\Content\VersionInfo 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\ContentInfo;
12
use eZ\Publish\API\Repository\Values\Content\Language;
13
use eZ\Publish\API\Repository\Values\Content\VersionInfo as APIVersionInfo;
14
use eZ\Publish\API\Repository\Values\User\User;
15
16
/**
17
 * This class holds version information data. It also contains the corresponding {@link Content} to
18
 * which the version belongs to.
19
 *
20
 * @property-read string[] $names returns an array with language code keys and name values
21
 * @property-read \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo calls getContentInfo()
22
 * @property-read int $id the internal id of the version
23
 * @property-read int $versionNo the version number of this version (which only increments in scope of a single Content object)
24
 * @property-read \DateTime $modifiedDate the last modified date of this version
25
 * @property-read \DateTime $createdDate the creation date of this version
26
 * @property-read int $creatorId the user id of the user which created this version
27
 * @property-read int $status the status of this version. One of VersionInfo::STATUS_DRAFT, VersionInfo::STATUS_PUBLISHED, VersionInfo::STATUS_ARCHIVED
28
 * @property-read string $initialLanguageCode the language code of the version. This value is used to flag a version as a translation to specific language
29
 * @property-read string[] $languageCodes a collection of all languages which exist in this version.
30
 *
31
 * @internal Meant for internal use by Repository, type hint against API object instead.
32
 */
33
class VersionInfo extends APIVersionInfo
34
{
35
    /** @var string[] */
36
    protected $names;
37
38
    /** @var \eZ\Publish\API\Repository\Values\Content\ContentInfo */
39
    protected $contentInfo;
40
41
    /** @var \eZ\Publish\API\Repository\Values\User\User */
42
    protected $creator;
43
44
    /** @var \eZ\Publish\API\Repository\Values\Content\Language */
45
    protected $initialLanguage;
46
47
    /** @var \eZ\Publish\API\Repository\Values\Content\Language[] */
48
    protected $languages;
49
50
    /**
51
     * The first matched name language among user provided prioritized languages.
52
     *
53
     * The first matched language among user provided prioritized languages on object retrieval, or null if none
54
     * provided (all languages) or on main fallback.
55
     *
56
     * @internal
57
     * @var string|null
58
     */
59
    protected $prioritizedNameLanguageCode;
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getContentInfo(): ContentInfo
65
    {
66
        return $this->contentInfo;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getCreator(): User
73
    {
74
        return $this->creator;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getInitialLanguage(): Language
81
    {
82
        return $this->initialLanguage;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getLanguages(): iterable
89
    {
90
        return $this->languages;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getNames()
97
    {
98
        return $this->names;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getName($languageCode = null)
105
    {
106
        if ($languageCode) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $languageCode of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
107
            return isset($this->names[$languageCode]) ? $this->names[$languageCode] : null;
108
        }
109
110
        if ($this->prioritizedNameLanguageCode) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->prioritizedNameLanguageCode of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
111
            return $this->names[$this->prioritizedNameLanguageCode];
112
        } elseif (!empty($this->contentInfo->alwaysAvailable) && isset($this->names[$this->contentInfo->mainLanguageCode])) {
113
            return $this->names[$this->contentInfo->mainLanguageCode];
114
        }
115
116
        // Versioned name should always exists in initial language for a version so we use that as final fallback
117
        return $this->names[$this->initialLanguageCode];
118
    }
119
}
120