TranslateLoader::getPath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (c) 2017 Salah Alkhwlani <[email protected]>
5
 *
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace Yemenifree\Validation;
11
12
class TranslateLoader
13
{
14
    protected $path;
15
    protected $defaultPath;
16
    protected $supportLang = ['ar'];
17
18
    /**
19
     * TranslateLoader constructor.
20
     */
21
    public function __construct()
22
    {
23
        $this->defaultPath = __DIR__ . '/lang';
24
        $this->path = __DIR__ . '/lang';
25
    }
26
27
    /**
28
     * load lang.
29
     *
30
     * @param $locale
31
     *
32
     * @return array
33
     */
34
    public function load($locale): array
35
    {
36
        // if file not exists
37
        if (!\file_exists($this->getFilePath($locale))) {
38
            // if local support build in get translate from package.
39
            if ($this->isSupportLocal($locale)) {
40
                return include $this->getFilePath($locale, true);
41
            }
42
43
            return [];
44
        }
45
46
        return include $this->getFilePath($locale);
47
    }
48
49
    /**
50
     * @param $locale
51
     * @param bool $default
52
     *
53
     * @return string
54
     */
55
    protected function getFilePath($locale, $default = false): string
56
    {
57
        return $this->getPath($default) . '/' . $locale . '.php';
58
    }
59
60
    /**
61
     * get path lang.
62
     *
63
     * @param bool $default
64
     *
65
     * @return string
66
     */
67
    public function getPath($default = false): string
68
    {
69
        return $default ? $this->getDefaultPath() : $this->path;
70
    }
71
72
    /**
73
     * @param string $path
74
     *
75
     * @return TranslateLoader
76
     */
77
    public function setPath($path): self
78
    {
79
        $this->path = $path;
80
81
        return $this;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getDefaultPath(): string
88
    {
89
        return $this->defaultPath;
90
    }
91
92
    /**
93
     * Check if local support build in with package.
94
     *
95
     * @param $locale
96
     *
97
     * @return bool
98
     */
99
    private function isSupportLocal($locale): bool
100
    {
101
        return \in_array($locale, $this->supportLang, true);
102
    }
103
}
104