Completed
Push — 6.7 ( eacd1f...2a3684 )
by
unknown
25:16
created

generateLanguageMaskFromLanguageIds()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Language MaskGenerator 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\Persistence\Legacy\Content\Language;
10
11
use eZ\Publish\SPI\Persistence\Content\Language\Handler as LanguageHandler;
12
13
/**
14
 * Language MaskGenerator.
15
 */
16
class MaskGenerator
17
{
18
    /**
19
     * Language lookup.
20
     *
21
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler
22
     */
23
    protected $languageHandler;
24
25
    /**
26
     * Creates a new Language MaskGenerator.
27
     *
28
     * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler
29
     */
30
    public function __construct(LanguageHandler $languageHandler)
31
    {
32
        $this->languageHandler = $languageHandler;
0 ignored issues
show
Documentation Bug introduced by
$languageHandler is of type object<eZ\Publish\SPI\Pe...ntent\Language\Handler>, but the property $languageHandler was declared to be of type object<eZ\Publish\Core\P...ntent\Language\Handler>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
33
    }
34
35
    /**
36
     * Generates a language mask from the keys of $languages.
37
     *
38
     * @param array $languages
39
     *
40
     * @return int
41
     */
42
    public function generateLanguageMask(array $languages)
43
    {
44
        $mask = 0;
45
        if (isset($languages['always-available'])) {
46
            $mask |= $languages['always-available'] ? 1 : 0;
47
            unset($languages['always-available']);
48
        }
49
50
        foreach ($languages as $language => $value) {
51
            $mask |= $this->languageHandler->loadByLanguageCode($language)->id;
52
        }
53
54
        return $mask;
55
    }
56
57
    /**
58
     * Generates a language mask from pre-loaded Language Ids.
59
     *
60
     * @param array $languageIds
61
     * @param bool $alwaysAvailable
62
     *
63
     * @return int
64
     */
65
    public function generateLanguageMaskFromLanguageIds(array $languageIds, $alwaysAvailable)
66
    {
67
        // make sure alwaysAvailable part of bit mask always results in 1 or 0
68
        $languageMask = $alwaysAvailable ? 1 : 0;
69
70
        foreach ($languageIds as $languageId) {
71
            $languageMask |= $languageId;
72
        }
73
74
        return $languageMask;
75
    }
76
77
    /**
78
     * Generates a language indicator from $languageCode and $alwaysAvailable.
79
     *
80
     * @param string $languageCode
81
     * @param bool $alwaysAvailable
82
     *
83
     * @return int
84
     */
85
    public function generateLanguageIndicator($languageCode, $alwaysAvailable)
86
    {
87
        return $this->languageHandler->loadByLanguageCode($languageCode)->id | ($alwaysAvailable ? 1 : 0);
88
    }
89
90
    /**
91
     * Checks if $language is always available in $languages;.
92
     *
93
     * @param string $language
94
     * @param array $languages
95
     *
96
     * @return bool
97
     */
98
    public function isLanguageAlwaysAvailable($language, array $languages)
99
    {
100
        return isset($languages['always-available'])
101
           && ($languages['always-available'] == $language)
102
        ;
103
    }
104
105
    /**
106
     * Checks if $languageMask contains the alwaysAvailable bit field.
107
     *
108
     * @param int $languageMask
109
     *
110
     * @return bool
111
     */
112
    public function isAlwaysAvailable($languageMask)
113
    {
114
        return (bool)($languageMask & 1);
115
    }
116
117
    /**
118
     * Removes the alwaysAvailable flag from $languageId and returns cleaned up $languageId.
119
     *
120
     * @param int $languageId
121
     *
122
     * @return int
123
     */
124
    public function removeAlwaysAvailableFlag($languageId)
125
    {
126
        return $languageId & ~1;
127
    }
128
129
    /**
130
     * Extracts every language Ids contained in $languageMask.
131
     *
132
     * @param int $languageMask
133
     *
134
     * @return array Array of language Id
135
     */
136 View Code Duplication
    public function extractLanguageIdsFromMask($languageMask)
137
    {
138
        $exp = 2;
139
        $result = array();
140
141
        // Decomposition of $languageMask into its binary components.
142
        while ($exp <= $languageMask) {
143
            if ($languageMask & $exp) {
144
                $result[] = $exp;
145
            }
146
147
            $exp *= 2;
148
        }
149
150
        return $result;
151
    }
152
153
    /**
154
     * Extracts Language codes contained in given $languageMask.
155
     *
156
     * @param int $languageMask
157
     *
158
     * @return array
159
     */
160
    public function extractLanguageCodesFromMask($languageMask)
161
    {
162
        $languageCodes = array();
163
164
        foreach ($this->extractLanguageIdsFromMask($languageMask) as $languageId) {
165
            $languageCodes[] = $this->languageHandler->load($languageId)->languageCode;
166
        }
167
168
        return $languageCodes;
169
    }
170
171
    /**
172
     * Checks if given $languageMask consists of multiple languages.
173
     *
174
     * @param int $languageMask
175
     *
176
     * @return bool
177
     */
178
    public function isLanguageMaskComposite($languageMask)
179
    {
180
        // Ignore first bit
181
        $languageMask = $this->removeAlwaysAvailableFlag($languageMask);
182
183
        // Special case
184
        if ($languageMask === 0) {
185
            return false;
186
        }
187
188
        // Return false if power of 2
189
        return (bool)($languageMask & ($languageMask - 1));
190
    }
191
}
192