Completed
Push — master ( 627e0e...cd277e )
by Kamil
15s
created

LocaleNameConverter::convertToCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Locale\Converter;
13
14
use Symfony\Component\Intl\Intl;
15
16
/**
17
 * @author Arkadiusz Krakowiak <[email protected]>
18
 */
19
final class LocaleNameConverter implements LocaleNameConverterInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function convertToCode($name, $locale = 'en')
25
    {
26
        $localeNames = Intl::getLocaleBundle()->getLocaleNames($locale);
27
        $localeCode = array_search($name, $localeNames, true);
28
29
        if (false === $localeCode) {
30
            throw new \InvalidArgumentException(
31
                sprintf('Cannot find code for %s locale', $name)
32
            );
33
        }
34
35
        return $localeCode;
36
    }
37
}
38