1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelPlus\Extension\Specs; |
4
|
|
|
|
5
|
|
|
use LaravelPlus\Extension\Repository\NamespacedRepository; |
6
|
|
|
use Illuminate\Contracts\Translation\Translator as TranslatorInterface; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
class InputSpec |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $namespace; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $path; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $rules = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \LaravelPlus\Extension\Specs\Translator |
28
|
|
|
*/ |
29
|
|
|
protected $translator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \LaravelPlus\Extension\Repository\NamespacedRepository $specs |
33
|
|
|
* @param \Illuminate\Contracts\Translation\Translator $translator |
34
|
|
|
* @param string $path |
35
|
|
|
*/ |
36
|
6 |
|
public function __construct(NamespacedRepository $specs, TranslatorInterface $translator, $path) |
37
|
|
|
{ |
38
|
6 |
|
$rules = $specs->get($path); |
39
|
|
|
|
40
|
6 |
|
if (is_null($rules)) { |
41
|
|
|
throw new InvalidArgumentException("spec '$path' is not found"); |
42
|
|
|
} |
43
|
6 |
|
if (!is_array($rules)) { |
44
|
|
|
throw new InvalidArgumentException('$rules must array in path '.$path); |
45
|
|
|
} |
46
|
6 |
|
if (!$translator->has($path)) { |
47
|
|
|
throw new InvalidArgumentException("translate '$path' is not found"); |
48
|
|
|
} |
49
|
|
|
|
50
|
6 |
|
if (strpos($path, '::') !== false) { |
51
|
|
|
list($namespace, $path) = explode('::', $path, 2); |
52
|
|
|
} else { |
53
|
6 |
|
$namespace = ''; |
54
|
|
|
} |
55
|
|
|
|
56
|
6 |
|
$this->path = $path; |
57
|
6 |
|
$this->namespace = $namespace; |
58
|
6 |
|
$this->rules = $rules; |
59
|
6 |
|
$this->translator = new Translator($translator, $namespace); |
60
|
|
|
|
61
|
6 |
|
$this->resolveSpecReferences(); |
62
|
6 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
3 |
|
public function attributes() |
68
|
|
|
{ |
69
|
3 |
|
return array_keys($this->rules); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
3 |
|
public function rules() |
76
|
|
|
{ |
77
|
3 |
|
return $this->rules; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
3 |
|
public function ruleMessages() |
84
|
|
|
{ |
85
|
3 |
|
$path = $this->path.'.rules'; |
86
|
|
|
|
87
|
3 |
|
return $this->translator->get($path, []); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
3 |
|
public function labels() |
94
|
|
|
{ |
95
|
3 |
|
$path = $this->path.'.attributes'; |
96
|
|
|
|
97
|
3 |
|
return $this->translator->get($path, []); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
1 |
|
public function values() |
104
|
|
|
{ |
105
|
1 |
|
$path = $this->path.'.values'; |
106
|
|
|
|
107
|
1 |
|
return $this->translator->get($path, []); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $name |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
1 |
|
public function required($name) |
116
|
|
|
{ |
117
|
1 |
|
return $this->hasRule($this->rules[$name], 'required'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $name |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
1 |
|
public function label($name) |
126
|
|
|
{ |
127
|
1 |
|
$path = $this->path.'.attributes.'.$name; |
128
|
|
|
|
129
|
1 |
|
return $this->translator->get($path); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $name |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
1 |
|
public function helptext($name) |
138
|
|
|
{ |
139
|
1 |
|
$path = $this->path.'.helptexts.'.$name; |
140
|
|
|
|
141
|
1 |
|
return $this->translator->get($path, ''); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param mixed $ruleOrRules |
146
|
|
|
* @param string $name |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
1 |
|
protected function hasRule($ruleOrRules, $name) |
151
|
|
|
{ |
152
|
1 |
|
if (is_string($ruleOrRules)) { |
153
|
1 |
|
return $this->hasRuleInArray(explode('|', $ruleOrRules), 'required'); |
154
|
|
|
} elseif (is_array($ruleOrRules)) { |
155
|
|
|
return $this->hasRuleInArray($ruleOrRules, 'required'); |
156
|
|
|
} else { |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param array $rules |
163
|
|
|
* @param string $name |
164
|
|
|
* |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
1 |
|
private function hasRuleInArray(array $rules, $name) |
168
|
|
|
{ |
169
|
1 |
|
foreach ($rules as $rule) { |
170
|
1 |
|
if ($rule == $name) { |
171
|
1 |
|
return true; |
172
|
|
|
} |
173
|
1 |
|
} |
174
|
|
|
|
175
|
1 |
|
return false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
*/ |
180
|
6 |
|
private function resolveSpecReferences() |
181
|
|
|
{ |
182
|
6 |
|
foreach ($this->rules as &$value) { |
183
|
2 |
|
if (strpos($value, '@') !== false) { |
184
|
|
|
list(, $key) = explode('@', $value, 2); |
185
|
|
|
|
186
|
|
|
$rule = app('specs')->get($this->fullkey('vocabulary.'.$key), null); |
187
|
|
|
|
188
|
|
|
if (empty($rule)) { |
189
|
|
|
throw new InvalidArgumentException('specs/vocabulary '.$key.' not found.'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$value = $rule; |
193
|
|
|
} |
194
|
6 |
|
} |
195
|
6 |
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $key |
199
|
|
|
* |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
|
|
private function fullkey($key) |
203
|
|
|
{ |
204
|
|
|
return $this->namespace ? $this->namespace.'::'.$key : $key; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|