1 | <?php |
||
15 | class JsValidatorFactory |
||
16 | { |
||
17 | /** |
||
18 | * The application instance. |
||
19 | * |
||
20 | * @var \Illuminate\Contracts\Container\Container |
||
21 | */ |
||
22 | protected $app; |
||
23 | |||
24 | /** |
||
25 | * Configuration options. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $options; |
||
30 | |||
31 | /** |
||
32 | * Create a new Validator factory instance. |
||
33 | * |
||
34 | * @param \Illuminate\Contracts\Container\Container $app |
||
35 | * @param array $options |
||
36 | */ |
||
37 | 6 | public function __construct($app, array $options = []) |
|
42 | |||
43 | 6 | protected function setOptions($options) { |
|
50 | |||
51 | |||
52 | /** |
||
53 | * Creates JsValidator instance based on rules and message arrays. |
||
54 | * |
||
55 | * @param array $rules |
||
56 | * @param array $messages |
||
57 | * @param array $customAttributes |
||
58 | * @param null|string $selector |
||
59 | * |
||
60 | * @return JavascriptValidator |
||
61 | */ |
||
62 | 2 | public function make(array $rules, array $messages = array(), array $customAttributes = array(), $selector = null) |
|
68 | |||
69 | /** |
||
70 | * Get the validator instance for the request. |
||
71 | * |
||
72 | * @return \Illuminate\Validation\Validator |
||
73 | */ |
||
74 | 4 | protected function getValidatorInstance(array $rules, array $messages = array(), array $customAttributes = array()) |
|
83 | |||
84 | /** |
||
85 | * Creates JsValidator instance based on FormRequest. |
||
86 | * |
||
87 | * @param $formRequest |
||
88 | * @param null $selector |
||
89 | * |
||
90 | * @return JavascriptValidator |
||
91 | * |
||
92 | * @throws FormRequestArgumentException |
||
93 | */ |
||
94 | 3 | public function formRequest($formRequest, $selector = null) |
|
95 | { |
||
96 | 3 | if (!is_subclass_of($formRequest, 'Illuminate\\Foundation\\Http\\FormRequest')) { |
|
97 | 1 | throw new FormRequestArgumentException((string) $formRequest); |
|
98 | } |
||
99 | |||
100 | 2 | if (is_string($formRequest)) { |
|
101 | 1 | $formRequest = $this->createFormRequest($formRequest); |
|
102 | 1 | } |
|
103 | |||
104 | 2 | $rules = method_exists($formRequest, 'rules') ? $formRequest->rules() : []; |
|
105 | |||
106 | 2 | $validator = $this->getValidatorInstance($rules, $formRequest->messages(), $formRequest->attributes()); |
|
107 | |||
108 | 2 | return $this->validator($validator, $selector); |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Creates and initializes an Form Request instance. |
||
113 | * |
||
114 | * @param string $class |
||
115 | * |
||
116 | * @return FormRequest |
||
117 | */ |
||
118 | 1 | protected function createFormRequest($class) |
|
119 | { |
||
120 | 1 | $request = $this->app->__get('request'); |
|
121 | 1 | $formRequest = call_user_func([$class, 'createFromBase'], $request); |
|
122 | |||
123 | 1 | if ($session = $request->getSession()) { |
|
124 | 1 | $formRequest->setSession($session); |
|
125 | 1 | } |
|
126 | |||
127 | 1 | $formRequest->setUserResolver($request->getUserResolver()); |
|
128 | |||
129 | 1 | $formRequest->setRouteResolver($request->getRouteResolver()); |
|
130 | |||
131 | 1 | return $formRequest; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Creates JsValidator instance based on Validator. |
||
136 | * |
||
137 | * @param \Illuminate\Validation\Validator $validator |
||
138 | * @param string|null $selector |
||
139 | * |
||
140 | * @return JavascriptValidator |
||
141 | */ |
||
142 | 4 | public function validator(Validator $validator, $selector = null) |
|
146 | |||
147 | /** |
||
148 | * Creates JsValidator instance based on Validator. |
||
149 | * |
||
150 | * @param \Illuminate\Validation\Validator $validator |
||
151 | * @param string|null $selector |
||
152 | * |
||
153 | * @return JavascriptValidator |
||
154 | */ |
||
155 | 4 | protected function jsValidator(Validator $validator, $selector = null) |
|
171 | |||
172 | /** |
||
173 | * Get and encrypt token from session store. |
||
174 | * |
||
175 | * @return null|string |
||
176 | */ |
||
177 | 4 | protected function getSessionToken() |
|
190 | |||
191 | } |
||
192 |