Completed
Push — master ( a54aff...c6c52b )
by ARCANEDEV
04:02 queued 04:01
created

LocaleCollection::toNative()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Arcanedev\Localization\Entities;
2
3
use Illuminate\Support\Collection;
4
5
/**
6
 * Class     LocaleCollection
7
 *
8
 * @package  Arcanedev\Localization\Entities
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class LocaleCollection extends Collection
12
{
13
    /* -----------------------------------------------------------------
14
     |  Properties
15
     | -----------------------------------------------------------------
16
     */
17
18
    /**
19
     * Supported locales.
20
     *
21
     * @var array
22
     */
23
    protected $supported = [];
24
25
    /* -----------------------------------------------------------------
26
     |  Getters & Setters
27
     | -----------------------------------------------------------------
28
     */
29
30
    /**
31
     * Set supported locales keys.
32
     *
33
     * @param  array  $supported
34
     *
35
     * @return self
36
     */
37 8
    public function setSupportedKeys(array $supported)
38
    {
39 8
        $this->supported = $supported;
40
41 8
        return $this;
42
    }
43
44
    /* -----------------------------------------------------------------
45
     |  Main Methods
46
     | -----------------------------------------------------------------
47
     */
48
49
    /**
50
     * Get the first locale from the collection.
51
     *
52
     * @param  callable|null  $callback
53
     * @param  mixed          $default
54
     *
55
     * @return \Arcanedev\Localization\Entities\Locale|mixed
56
     */
57 4
    public function first(callable $callback = null, $default = null)
58
    {
59 4
        return parent::first($callback, $default);
60
    }
61
62
    /**
63
     * Get supported locales collection.
64
     *
65
     * @return \Arcanedev\Localization\Entities\SupportedLocaleCollection
66
     */
67 6
    public function getSupported()
68
    {
69 6
        return new SupportedLocaleCollection(
70 6
            $this->filter(function(Locale $locale) {
71 6
                return in_array($locale->key(), $this->supported);
72 6
            })
73
        );
74
    }
75
76
    /**
77
     * Transform the collection with only locale's native name.
78
     *
79
     * @return \Illuminate\Support\Collection
80
     */
81
    public function toNative()
82
    {
83 2
        return $this->map(function (Locale $locale) {
84 2
            return $locale->native();
85 2
        })->toBase();
86
    }
87
88
    /**
89
     * Load from config.
90
     *
91
     * @return self
92
     */
93 4
    public function loadFromConfig()
94
    {
95 4
        $this->loadFromArray(
96 4
            config('localization.locales', [])
97
        );
98 4
        $this->setSupportedKeys(
99 4
            config('localization.supported-locales', [])
100
        );
101
102 4
        return $this;
103
    }
104
105
    /**
106
     * Load locales from array.
107
     *
108
     * @param  array  $locales
109
     *
110
     * @return self
111
     */
112 254
    public function loadFromArray(array $locales)
113
    {
114 254
        $this->items = [];
115
116 254
        foreach ($locales as $key => $locale) {
117 254
            $this->put($key, Locale::make($key, $locale));
118
        }
119
120 254
        return $this;
121
    }
122
}
123