1 | <?php |
||
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 \Symfony\Component\Translation\TranslatorInterface $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() |
|
71 | |||
72 | /** |
||
73 | * @return array |
||
74 | */ |
||
75 | 3 | public function rules() |
|
79 | |||
80 | /** |
||
81 | * @return array |
||
82 | */ |
||
83 | 3 | public function ruleMessages() |
|
89 | |||
90 | /** |
||
91 | * @return array |
||
92 | */ |
||
93 | 3 | public function labels() |
|
99 | |||
100 | /** |
||
101 | * @return array |
||
102 | */ |
||
103 | 1 | public function values() |
|
109 | |||
110 | /** |
||
111 | * @param string $name |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | 1 | public function required($name) |
|
119 | |||
120 | /** |
||
121 | * @param string $name |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | 1 | public function label($name) |
|
131 | |||
132 | /** |
||
133 | * @param string $name |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | 1 | public function helptext($name) |
|
143 | |||
144 | /** |
||
145 | * @param mixed $ruleOrRules |
||
146 | * @param string $name |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | 1 | protected function hasRule($ruleOrRules, $name) |
|
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) |
||
206 | } |
||
207 |