Completed
Push — master ( 9c37c7...1d2d63 )
by Jens
08:02
created

LocalizedString::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 * @created: 26.01.15, 15:19
5
 */
6
7
namespace Commercetools\Core\Model\Common;
8
9
use Commercetools\Core\Error\Message;
10
use Commercetools\Core\Error\InvalidArgumentException;
11
12
/**
13
 * @package Commercetools\Core\Model\Type
14
 * @link https://dev.commercetools.com/http-api-types.html#localized-string
15
 * @example
16
 * ```php
17
 * LocalizedString::fromArray(['en' => 'Hello World', 'de' => 'Hallo Welt'])->add('fr', 'Bonjour le monde');
18
 * ```
19
 */
20
class LocalizedString implements \JsonSerializable, JsonDeserializeInterface
21
{
22
    use ContextTrait;
23
24
    /**
25
     * @var array
26
     */
27
    protected $values = [];
28
29
    /**
30
     * @param array $values
31
     * @param Context|callable $context
32
     */
33 229
    public function __construct(array $values, $context = null)
34
    {
35 229
        $this->setContext($context);
36 229
        foreach ($values as $locale => $value) {
37 228
            $this->add($locale, $value);
38
        }
39 229
    }
40
41
    /**
42
     * @param $locale
43
     * @return string
44
     */
45 48
    public function __get($locale)
46
    {
47 48
        $context = new Context();
48 48
        $context->setLanguages([$locale])->setGraceful($this->getContext()->isGraceful());
0 ignored issues
show
Unused Code introduced by
The call to the method Commercetools\Core\Model...\Context::setGraceful() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
49 48
        return $this->get($context);
50
    }
51
52
    /**
53
     * @param Context $context
54
     * @return string
55
     */
56 2
    public function getLocalized(Context $context = null)
57
    {
58 2
        return $this->get($context);
59
    }
60
    /**
61
     * @param Context $context
62
     * @return string
63
     */
64 85
    protected function getLanguage(Context $context)
65
    {
66 85
        $locale = null;
67 85
        foreach ($context->getLanguages() as $locale) {
68 85
            $locale = \Locale::canonicalize($locale);
69 85
            if (isset($this->values[$locale])) {
70 75
                return $locale;
71
            }
72 11
            $language = \Locale::getPrimaryLanguage($locale);
73 11
            if ($locale == $language) {
74 7
                continue;
75
            }
76 4
            if (isset($this->values[$language])) {
77 4
                return $language;
78
            }
79
        }
80 6
        return $locale;
81
    }
82
83
    /**
84
     * @param Context $context
85
     * @return string
86
     */
87 85 View Code Duplication
    public function get(Context $context = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89 85
        if (is_null($context)) {
90 20
            $context = $this->getContext();
91
        }
92 85
        $locale = $this->getLanguage($context);
93 85
        if (!isset($this->values[$locale])) {
94 6
            if (!$context->isGraceful()) {
95 1
                throw new InvalidArgumentException(Message::NO_VALUE_FOR_LOCALE);
96
            }
97 5
            return '';
98
        }
99 79
        return $this->values[$locale];
100
    }
101
102
    /**
103
     * @param string $locale
104
     * @param string $value
105
     * @return $this
106
     */
107 229
    public function add($locale, $value)
108
    {
109 229
        $locale = \Locale::canonicalize($locale);
110 229
        $this->values[$locale] = $value;
111
112 229
        return $this;
113
    }
114
115 8
    public function merge(LocalizedString $localizedString)
116
    {
117 8
        $this->values = array_merge($this->values, $localizedString->toArray());
118 8
    }
119
120 17
    public function __toString()
121
    {
122 17
        return $this->get();
123
    }
124
125
    /**
126
     * @return array
127
     */
128 179
    public function toArray()
129
    {
130 179
        $values = $this->values;
131
132 179
        $data = [];
133 179
        foreach ($values as $key => $value) {
134 179
            $data[str_replace('_', '-', $key)] = $value;
135
        }
136 179
        return $data;
137
    }
138
139
    /**
140
     * @return array
141
     */
142 169
    public function jsonSerialize()
143
    {
144 169
        return $this->toArray();
145
    }
146
147
    /**
148
     * @param Context|callable $context
149
     * @return $this
150
     */
151
    public static function of($context = null)
152
    {
153
        return new static([], $context);
154
    }
155
156
    /**
157
     * @param array $data
158
     * @param Context|callable $context
159
     * @return static
160
     */
161 108
    public static function fromArray(array $data, $context = null)
162
    {
163 108
        return new static($data, $context);
164
    }
165
166
    /**
167
     * @param string $language
168
     * @param string $text
169
     * @param Context|callable $context
170
     * @return LocalizedString
171
     */
172 147
    public static function ofLangAndText($language, $text, $context = null)
173
    {
174 147
        return new static([$language => $text], $context);
175
    }
176
}
177