Completed
Branch delegated-factory (a380a7)
by Albert
05:25
created

JsValidatorFactory::formRequest()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 7
Bugs 1 Features 0
Metric Value
c 7
b 1
f 0
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 8
nc 5
nop 2
crap 4
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\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 = [])
38
    {
39 6
        $this->app = $app;
40 6
        $this->setOptions($options);
41 6
    }
42
43 6
    protected function setOptions($options) {
44 6
        $options['disable_remote_validation'] = empty($options['disable_remote_validation'])?false:$options['disable_remote_validation'];
45 6
        $options['view'] = empty($options['view'])?'jsvalidation:bootstrap':$options['view'];
46 6
        $options['form_selector'] = empty($options['form_selector'])?'form':$options['form_selector'];
47
48 6
        $this->options = $options;
49 6
    }
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)
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
     * @return \Illuminate\Validation\Validator
73
     */
74 4
    protected function getValidatorInstance(array $rules, array $messages = array(), array $customAttributes = array())
75
    {
76 4
        $factory = $this->app->make('Illuminate\Contracts\Validation\Factory');
77
78 4
        $validator = $factory->make([], $rules, $messages, $customAttributes);
79 4
        $validator->addCustomAttributes($customAttributes);
80
81 4
        return $validator;
82
    }
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)
143
    {
144 4
        return $this->jsValidator($validator, $selector);
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
    protected function jsValidator(Validator $validator, $selector = null)
156
    {
157 4
        $remote = !$this->options['disable_remote_validation'];
158 4
        $view = $this->options['view'];
159 4
        $selector = is_null($selector) ? $this->options['form_selector'] : $selector;
160
161 4
        $delegated = new DelegatedValidator($validator);
162 4
        $rules = new RuleParser($delegated, $this->getSessionToken());
163 4
        $messages = new MessageParser($delegated);
164
165 4
        $jsValidator = new ValidatorHandler($rules, $messages);
166
167 4
        $manager = new JavascriptValidator($jsValidator, compact('view', 'selector', 'remote'));
168
169 4
        return $manager;
170
    }
171
172
    /**
173
     * Get and encrypt token from session store.
174
     *
175
     * @return null|string
176
     */
177 4
    protected function getSessionToken()
178
    {
179 4
        $token = null;
180 4
        if ($session = $this->app->__get('session')) {
181 1
            $token = $session->token();
182 1
        }
183
184 4
        if ($encrypter = $this->app->__get('encrypter')) {
185 1
            $token = $encrypter->encrypt($token);
186 1
        }
187
188 4
        return $token;
189
    }
190
191
}
192