Completed
Branch issue/formrequest-di (bce014)
by Albert
02:09
created

JsValidatorFactory::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 4
crap 1
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
     *  Creates and initializes an Form Request instance.
111
     *
112
     * @param string $class
113
     * @return FormRequest
114
     */
115 1
    protected function createFormRequest($class)
116
    {
117
        /**
118
         * @var $formRequest \Illuminate\Foundation\Http\FormRequest
119
         * @var $request Request
120
         */
121
122 1
        $params = [];
123 1
        if (is_array($class)) {
124 1
            $params = empty($class[1])?$params:$class[1];
125 1
            $class = $class[0];
126 1
        }
127
128 1
        $request = $this->app->__get('request');
129 1
        $formRequest = $this->app->build($class, $params);
130
131 1
        if ($session = $request->getSession()) {
132 1
            $formRequest->setSession($session);
133 1
        }
134 1
        $formRequest->setUserResolver($request->getUserResolver());
135 1
        $formRequest->setRouteResolver($request->getRouteResolver());
136 1
        $formRequest->setContainer($this->app);
137
138 1
        return $formRequest;
139
    }
140
141
    /**
142
     * Creates JsValidator instance based on Validator.
143
     *
144
     * @param \Illuminate\Validation\Validator $validator
145
     * @param string|null                      $selector
146
     *
147
     * @return JavascriptValidator
148
     */
149 4
    public function validator(Validator $validator, $selector = null)
150
    {
151 4
        return $this->jsValidator($validator, $selector);
152
    }
153
154
    /**
155
     * Creates JsValidator instance based on Validator.
156
     *
157
     * @param \Illuminate\Validation\Validator $validator
158
     * @param string|null                      $selector
159
     *
160
     * @return JavascriptValidator
161
     */
162 4
    protected function jsValidator(Validator $validator, $selector = null)
163
    {
164 4
        $remote = ! $this->options['disable_remote_validation'];
165 4
        $view = $this->options['view'];
166 4
        $selector = is_null($selector) ? $this->options['form_selector'] : $selector;
167
168 4
        $delegated = new DelegatedValidator($validator);
169 4
        $rules = new RuleParser($delegated, $this->getSessionToken());
170 4
        $messages = new MessageParser($delegated);
171
172 4
        $jsValidator = new ValidatorHandler($rules, $messages);
173
174 4
        $manager = new JavascriptValidator($jsValidator, compact('view', 'selector', 'remote'));
175
176 4
        return $manager;
177
    }
178
179
    /**
180
     * Get and encrypt token from session store.
181
     *
182
     * @return null|string
183
     */
184 4
    protected function getSessionToken()
185
    {
186 4
        $token = null;
187 4
        if ($session = $this->app->__get('session')) {
188 1
            $token = $session->token();
189 1
        }
190
191 4
        if ($encrypter = $this->app->__get('encrypter')) {
192 1
            $token = $encrypter->encrypt($token);
193 1
        }
194
195 4
        return $token;
196
    }
197
}
198