Completed
Pull Request — master (#18)
by Propa
09:42
created

Country::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 4
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Propaganistas\LaravelIntl;
4
5
use Illuminate\Support\Arr;
6
use Propaganistas\LaravelIntl\Contracts\Intl;
7
8 View Code Duplication
class Country extends Intl
0 ignored issues
show
Duplication introduced by
This class 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...
9
{
10
    /**
11
     * Loaded localized country data.
12
     *
13
     * @var array
14
     */
15
    protected $data;
16
17
    /**
18
     * The current locale.
19
     *
20
     * @var string $locale
21
     */
22
    protected $locale;
23
24
    /**
25
     * The current locale.
26
     *
27
     * @var string $locale
28
     */
29
    protected $fallbackLocale;
30
31
    /**
32
     * Get a localized record by key.
33
     *
34
     * @param string $key
35
     * @return string
36
     */
37 15
    public function get($key)
38
    {
39 15
        return Arr::get($this->all(), $key);
40
    }
41
42
    /**
43
     * Alias of get().
44
     *
45
     * @param string $key
46
     * @return string
47
     */
48 15
    public function name($key)
49
    {
50 15
        return $this->get($key);
51
    }
52
53
    /**
54
     * Get all localized records.
55
     *
56
     * @return array
57
     */
58 18
    public function all()
59
    {
60 18
        return $this->data[$this->getLocale()] + $this->data[$this->getFallbackLocale()];
61
    }
62
63
    /**
64
     * Get the current locale.
65
     *
66
     * @return string
67
     */
68 21
    public function getLocale()
69
    {
70 21
        return $this->locale;
71
    }
72
73
    /**
74
     * Set the current locale.
75
     *
76
     * @param $locale
77
     * @return $this
78
     */
79 21
    public function setLocale($locale)
80
    {
81 21
        $this->locale = $locale;
82
83 21
        $this->load($locale);
84
85 21
        return $this;
86
    }
87
88
    /**
89
     * Get the fallback locale.
90
     *
91
     * @return string
92
     */
93 18
    public function getFallbackLocale()
94
    {
95 18
        return $this->fallbackLocale;
96
    }
97
98
    /**
99
     * Set the fallback locale.
100
     *
101
     * @param $locale
102
     * @return $this
103
     */
104 21
    public function setFallbackLocale($locale)
105
    {
106 21
        $this->fallbackLocale = $locale;
107
108 21
        $this->load($locale);
109
110 21
        return $this;
111
    }
112
113
    /**
114
     * Load the data for the given locale.
115
     *
116
     * @param string $locale
117
     * @return void
118
     */
119 21
    protected function load($locale)
120
    {
121 21
        if (! isset($this->data[$locale])) {
122 21
            $path = base_path('vendor/umpirsky/country-list/data/'.$locale.'/country.php');
123
124 21
            $this->data[$locale] = is_file($path) ? require $path : [];
125
        }
126
    }
127
}