1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Tleckie\Translate\Loader; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Tleckie\Translate\Resolver\File; |
9
|
|
|
use Tleckie\Translate\Resolver\Locale; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ArrayLoader |
13
|
|
|
* |
14
|
|
|
* @package Tleckie\Translate\Loader |
15
|
|
|
* @category ArrayLoader |
16
|
|
|
* @author Teodoro Leckie Westberg <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class ArrayLoader implements LoaderInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected string $path; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected string $fileExtension; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var File |
32
|
|
|
*/ |
33
|
|
|
protected File $fileResolver; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Locale |
37
|
|
|
*/ |
38
|
|
|
protected Locale $localeResolver; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* ArrayLoader constructor. |
42
|
|
|
* |
43
|
|
|
* @param string $path |
44
|
|
|
* @param string $fileExtension |
45
|
|
|
* @param File|null $fileResolver |
46
|
|
|
* @param Locale|null $localeResolver |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
string $path, |
50
|
|
|
string $fileExtension, |
51
|
|
|
File $fileResolver = null, |
52
|
|
|
Locale $localeResolver = null |
53
|
|
|
) { |
54
|
|
|
$this->path = $path; |
55
|
|
|
$this->fileExtension = $fileExtension; |
56
|
|
|
$this->fileResolver = $fileResolver ?? new File(); |
57
|
|
|
$this->localeResolver = $localeResolver ?? new Locale(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
|
|
public function load(string $locale): array |
64
|
|
|
{ |
65
|
|
|
$this->localeResolver->setLocale($locale); |
66
|
|
|
|
67
|
|
|
foreach ($this->localeResolver->toArray() as $part) { |
68
|
|
|
if ($this->fileResolver->isFile($this->path, $part, $this->fileExtension)) { |
69
|
|
|
return include $this->fileResolver->getFullPath( |
70
|
|
|
$this->path, |
71
|
|
|
$part, |
72
|
|
|
$this->fileExtension |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
throw new InvalidArgumentException( |
78
|
|
|
sprintf('Locale [%s] not found in [%s]', $locale, $this->path) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|