|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Zvermafia\Larastate\Abstracts; |
|
6
|
|
|
|
|
7
|
|
|
use UnexpectedValueException; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use BadMethodCallException; |
|
10
|
|
|
use ReflectionClass; |
|
11
|
|
|
use Zvermafia\Larastate\Traits\LocaleKeysTrait; |
|
12
|
|
|
use Zvermafia\Larastate\Traits\LocaleValuesTrait; |
|
13
|
|
|
use Zvermafia\Larastate\Traits\LocalePathsTrait; |
|
14
|
|
|
use Zvermafia\Larastate\Traits\HelpersTrait; |
|
15
|
|
|
|
|
16
|
|
|
abstract class StateAbstract |
|
17
|
|
|
{ |
|
18
|
|
|
use LocaleValuesTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A localizations file path for the entity. |
|
22
|
|
|
* |
|
23
|
|
|
* Optional. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $locale_path; |
|
28
|
|
|
|
|
29
|
|
|
/** @var \ReflectionClass */ |
|
30
|
|
|
protected $reflection; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Create a new instance. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->reflection = new ReflectionClass(static::class); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Magic method. |
|
42
|
|
|
* |
|
43
|
|
|
* Available methods (for example, role state with acceptable values are member and admin): |
|
44
|
|
|
* getRoleValues() // returns all the acceptable values, example: ['member', 'admin'] |
|
45
|
|
|
* getRoleValuesWithLocales() // returns array, example: ['member' => 'Member', 'admin' => 'Administrator'] |
|
46
|
|
|
* getRoleLocale($role_value) // return string, example: 'Administrator' |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $name |
|
49
|
|
|
* @param array $arguments |
|
50
|
|
|
* @throws \BadMethodCallException If the there is not a calling method |
|
51
|
|
|
* @throws \Exception If the constant not found |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __call(string $name, array $arguments) |
|
55
|
|
|
{ |
|
56
|
|
|
$matches = []; |
|
57
|
|
|
|
|
58
|
|
|
// check method pattern |
|
59
|
|
|
if (! preg_match('/^(get)([a-zA-Z_\x80-\xff]+)(Values|ValuesWithLocales|Locale)$/', $name, $matches)) { |
|
60
|
|
|
throw new BadMethodCallException("The {$name}() method you are calling was not found."); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
list($method_name, $prefix, $state_name, $postfix) = $matches; |
|
64
|
|
|
$state_name_snake_case = $this->makeSnakeCase($state_name); |
|
65
|
|
|
$state_constant_name = $this->makeConstantName($state_name); |
|
66
|
|
|
|
|
67
|
|
|
if ($this->reflection->hasConstant($state_constant_name) === false) { |
|
68
|
|
|
throw new Exception("The '{$state_constant_name}' constant not found."); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
switch ($postfix) { |
|
72
|
|
|
case 'Values': |
|
73
|
|
|
return $this->getValues($state_name_snake_case); |
|
74
|
|
|
|
|
75
|
|
|
case 'ValuesWithLocales': |
|
76
|
|
|
return $this->getValuesWithLocales($state_name_snake_case); |
|
77
|
|
|
|
|
78
|
|
|
case 'Locale': |
|
79
|
|
|
return $this->getLocale($state_name_snake_case, $arguments[0]); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the values for the given state name. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $state_name |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
private function getValues(string $state_name): array |
|
90
|
|
|
{ |
|
91
|
|
|
$constant_name = $this->makeConstantName($state_name); |
|
92
|
|
|
|
|
93
|
|
|
return array_values($this->reflection->getConstant($constant_name)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get values and locales for the given state. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $state_name |
|
100
|
|
|
* @throws \UnexpectedValueException If amount of the state values and their localizations are different |
|
101
|
|
|
* @throws \UnexpectedValueException If the state values and their localizations don't match by key |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
private function getValuesWithLocales(string $state_name): array |
|
105
|
|
|
{ |
|
106
|
|
|
$values = array_flip($this->getValues($state_name)); // from a CONSTANT |
|
107
|
|
|
$values_with_locales = $this->getLocales($state_name); // from a localization file |
|
108
|
|
|
|
|
109
|
|
|
if (count($values) !== count($values_with_locales)) { |
|
110
|
|
|
throw new UnexpectedValueException("Amount of the state values and their localizations are different."); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (count(array_diff_key($values, $values_with_locales))) { |
|
114
|
|
|
throw new UnexpectedValueException("The state values and their localizations don't match by key."); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $values_with_locales; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|