Completed
Branch issue/formrequest-di (558d00)
by Albert
02:16
created

JsValidatorFactory   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 17
Bugs 4 Features 0
Metric Value
wmc 21
c 17
b 4
f 0
lcom 1
cbo 6
dl 0
loc 189
ccs 64
cts 64
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A make() 0 6 1
A setOptions() 0 8 4
A getValidatorInstance() 0 9 1
A formRequest() 0 12 3
A parseFormRequestName() 0 9 3
A createFormRequest() 0 20 2
A validator() 0 4 1
A jsValidator() 0 16 2
A getSessionToken() 0 13 3
1
<?php
2
3
namespace Proengsoft\JsValidation;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
use Illuminate\Http\Request;
7
use Illuminate\Validation\Validator;
8
use Proengsoft\JsValidation\Exceptions\FormRequestArgumentException;
9
use Proengsoft\JsValidation\Javascript\JavascriptValidator;
10
use Proengsoft\JsValidation\Javascript\MessageParser;
11
use Proengsoft\JsValidation\Javascript\RuleParser;
12
use Proengsoft\JsValidation\Javascript\ValidatorHandler;
13
use Proengsoft\JsValidation\Support\DelegatedValidator;
14
15
class JsValidatorFactory
16
{
17
    /**
18
     * The application instance.
19
     *
20
     * @var \Illuminate\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\Container\Container $app
35
     * @param array                                        $options
36
     */
37 5
    public function __construct($app, array $options = [])
38
    {
39 5
        $this->app = $app;
40 5
        $this->setOptions($options);
41 5
    }
42
43 5
    protected function setOptions($options)
44
    {
45 5
        $options['disable_remote_validation'] = empty($options['disable_remote_validation']) ? false : $options['disable_remote_validation'];
46 5
        $options['view'] = empty($options['view']) ? 'jsvalidation:bootstrap' : $options['view'];
47 5
        $options['form_selector'] = empty($options['form_selector']) ? 'form' : $options['form_selector'];
48
49 5
        $this->options = $options;
50 5
    }
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)
63
    {
64 2
        $validator = $this->getValidatorInstance($rules, $messages, $customAttributes);
65
66 2
        return $this->validator($validator, $selector);
67
    }
68
69
    /**
70
     * Get the validator instance for the request.
71
     *
72
     * @param array $rules
73
     * @param array $messages
74
     * @param array $customAttributes
75
     * @return Validator
76
     */
77 4
    protected function getValidatorInstance(array $rules, array $messages = array(), array $customAttributes = array())
78
    {
79 4
        $factory = $this->app->make('Illuminate\Contracts\Validation\Factory');
80
81 4
        $validator = $factory->make([], $rules, $messages, $customAttributes);
82 4
        $validator->addCustomAttributes($customAttributes);
83
84 4
        return $validator;
85
    }
86
87
    /**
88
     * Creates JsValidator instance based on FormRequest.
89
     *
90
     * @param $formRequest
91
     * @param null $selector
92
     *
93
     * @return JavascriptValidator
94
     * @throws FormRequestArgumentException
95
     */
96 2
    public function formRequest($formRequest, $selector = null)
97
    {
98 2
        if (!is_object($formRequest)) {
99 1
            $formRequest = $this->createFormRequest($formRequest);
100 1
        }
101
102 2
        $rules = method_exists($formRequest, 'rules') ? $formRequest->rules() : [];
103
104 2
        $validator = $this->getValidatorInstance($rules, $formRequest->messages(), $formRequest->attributes());
105
106 2
        return $this->validator($validator, $selector);
107
    }
108
109
110 1
    protected function parseFormRequestName($class) {
111 1
        $params = [];
112 1
        if (is_array($class)) {
113 1
            $params = empty($class[1])?$params:$class[1];
114 1
            $class = $class[0];
115 1
        }
116 1
        return [$class,$params];
117
118
    }
119
120
    /**
121
     *  Creates and initializes an Form Request instance.
122
     *
123
     * @param string $class
124
     * @return FormRequest
125
     */
126 1
    protected function createFormRequest($class)
127
    {
128
        /**
129
         * @var $formRequest \Illuminate\Foundation\Http\FormRequest
130
         * @var $request Request
131
         */
132 1
        list($class, $params) = $this->parseFormRequestName($class);
133
134 1
        $request = $this->app->__get('request');
135 1
        $formRequest = $this->app->build($class, $params);
136
137 1
        if ($session = $request->getSession()) {
138 1
            $formRequest->setSession($session);
139 1
        }
140 1
        $formRequest->setUserResolver($request->getUserResolver());
141 1
        $formRequest->setRouteResolver($request->getRouteResolver());
142 1
        $formRequest->setContainer($this->app);
143
144 1
        return $formRequest;
145
    }
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
    public function validator(Validator $validator, $selector = null)
156
    {
157 4
        return $this->jsValidator($validator, $selector);
158
    }
159
160
    /**
161
     * Creates JsValidator instance based on Validator.
162
     *
163
     * @param \Illuminate\Validation\Validator $validator
164
     * @param string|null                      $selector
165
     *
166
     * @return JavascriptValidator
167
     */
168 4
    protected function jsValidator(Validator $validator, $selector = null)
169
    {
170 4
        $remote = ! $this->options['disable_remote_validation'];
171 4
        $view = $this->options['view'];
172 4
        $selector = is_null($selector) ? $this->options['form_selector'] : $selector;
173
174 4
        $delegated = new DelegatedValidator($validator);
175 4
        $rules = new RuleParser($delegated, $this->getSessionToken());
176 4
        $messages = new MessageParser($delegated);
177
178 4
        $jsValidator = new ValidatorHandler($rules, $messages);
179
180 4
        $manager = new JavascriptValidator($jsValidator, compact('view', 'selector', 'remote'));
181
182 4
        return $manager;
183
    }
184
185
    /**
186
     * Get and encrypt token from session store.
187
     *
188
     * @return null|string
189
     */
190 4
    protected function getSessionToken()
191
    {
192 4
        $token = null;
193 4
        if ($session = $this->app->__get('session')) {
194 1
            $token = $session->token();
195 1
        }
196
197 4
        if ($encrypter = $this->app->__get('encrypter')) {
198 1
            $token = $encrypter->encrypt($token);
199 1
        }
200
201 4
        return $token;
202
    }
203
}
204