JavascriptValidator   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 4
dl 0
loc 220
ccs 47
cts 50
cp 0.94
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setDefaults() 0 10 5
A render() 0 8 1
A toArray() 0 4 1
A __toString() 0 8 2
A __get() 0 9 2
A getViewData() 0 12 2
A setSelector() 0 4 1
A selector() 0 6 2
A ignore() 0 6 1
A view() 0 6 2
A remote() 0 6 1
A sometimes() 0 6 1
1
<?php
2
3
namespace Proengsoft\JsValidation\Javascript;
4
5
use Exception;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Support\Facades\View;
8
use Proengsoft\JsValidation\Exceptions\PropertyNotFoundException;
9
10
class JavascriptValidator implements Arrayable
11
{
12
    /**
13
     * Registered validator instance.
14
     *
15
     * @var ValidatorHandler
16
     */
17
    protected $validator;
18
19
    /**
20
     * Selector used in javascript generation.
21
     *
22
     * @var string
23
     */
24
    protected $selector;
25
26
    /**
27
     * View that renders Javascript.
28
     *
29
     * @var
30
     */
31
    protected $view;
32
33
    /**
34
     * Enable or disable remote validations.
35
     *
36
     * @var bool
37
     */
38
    protected $remote;
39
40
    /**
41
     * 'ignore' option for jQuery Validation Plugin.
42
     *
43
     * @var string
44
     */
45
    protected $ignore;
46
47
    /**
48
     * Constructor.
49
     *
50
     * @param ValidatorHandler $validator
51 192
     * @param array            $options
52
     */
53 192
    public function __construct(ValidatorHandler $validator, $options = [])
54 192
    {
55 192
        $this->validator = $validator;
56
        $this->setDefaults($options);
57
    }
58
59
    /**
60
     * Set default parameters.
61
     *
62
     * @param $options
63 192
     * @return void
64
     */
65 192
    protected function setDefaults($options)
66 192
    {
67 192
        $this->selector = empty($options['selector']) ? 'form' : $options['selector'];
68 192
        $this->view = empty($options['view']) ? 'jsvalidation::bootstrap' : $options['view'];
69
        $this->remote = isset($options['remote']) ? $options['remote'] : true;
70
71
        if (isset($options['ignore'])) {
72
            $this->ignore = $options['ignore'];
73
        }
74
    }
75
76
    /**
77 48
     * Render the specified view with validator data.
78
     *
79 48
     * @param null|\Illuminate\Contracts\View\View|string $view
80 48
     * @param null|string $selector
81
     * @return string
82 48
     */
83 36
    public function render($view = null, $selector = null)
84
    {
85
        $this->view($view);
86
        $this->selector($selector);
87
88
        return View::make($this->view, ['validator' => $this->getViewData()])
89
            ->render();
90
    }
91 48
92
    /**
93 48
     * Get the view data as an array.
94
     *
95
     * @return array
96
     */
97
    public function toArray()
98
    {
99
        return $this->getViewData();
100
    }
101 24
102
    /**
103
     * Get the string resulting of render default view.
104 24
     *
105 12
     * @return string
106 12
     */
107
    public function __toString()
108
    {
109
        try {
110
            return $this->render();
111
        } catch (Exception $exception) {
112
            return trigger_error($exception->__toString(), E_USER_ERROR);
113
        }
114
    }
115
116
    /**
117
     * Gets value from view data.
118 24
     *
119
     * @param $name
120 24
     * @return string
121 24
     *
122 12
     * @throws \Proengsoft\JsValidation\Exceptions\PropertyNotFoundException
123
     */
124
    public function __get($name)
125 12
    {
126
        $data = $this->getViewData();
127
        if (! array_key_exists($name, $data)) {
128
            throw new PropertyNotFoundException($name, get_class());
129
        }
130
131
        return $data[$name];
132
    }
133 120
134
    /**
135 120
     * Gets view data.
136 120
     *
137 108
     * @return array
138
     */
139 108
    protected function getViewData()
140 12
    {
141
        $this->validator->setRemote($this->remote);
142
        $data = $this->validator->validationData();
143 108
        $data['selector'] = $this->selector;
144
145
        if (! is_null($this->ignore)) {
146
            $data['ignore'] = $this->ignore;
147
        }
148
149
        return $data;
150
    }
151
152
    /**
153
     * Set the form selector to validate.
154
     *
155
     * @param string $selector
156
     * @deprecated
157
     */
158
    public function setSelector($selector)
159
    {
160
        $this->selector = $selector;
161
    }
162
163 48
    /**
164
     * Set the form selector to validate.
165 48
     *
166
     * @param string $selector
167 48
     * @return \Proengsoft\JsValidation\Javascript\JavascriptValidator
168
     */
169
    public function selector($selector)
170
    {
171
        $this->selector = is_null($selector) ? $this->selector : $selector;
172
173
        return $this;
174
    }
175
176 12
    /**
177
     * Set the input selector to ignore for validation.
178 12
     *
179
     * @param string $ignore
180 12
     * @return \Proengsoft\JsValidation\Javascript\JavascriptValidator
181
     */
182
    public function ignore($ignore)
183
    {
184
        $this->ignore = $ignore;
185
186
        return $this;
187
    }
188
189 48
    /**
190
     * Set the view to render Javascript Validations.
191 48
     *
192
     * @param null|\Illuminate\Contracts\View\View|string $view
193 48
     * @return \Proengsoft\JsValidation\Javascript\JavascriptValidator
194
     */
195
    public function view($view)
196
    {
197
        $this->view = is_null($view) ? $this->view : $view;
198
199
        return $this;
200
    }
201
202 12
    /**
203
     * Enables or disables remote validations.
204 12
     *
205
     * @param null|bool $enabled
206 12
     * @return \Proengsoft\JsValidation\Javascript\JavascriptValidator
207
     */
208
    public function remote($enabled = true)
209
    {
210
        $this->remote = $enabled;
211
212
        return $this;
213
    }
214
215
    /**
216
     * Validate Conditional Validations using Ajax in specified fields.
217 12
     *
218
     * @param string       $attribute
219 12
     * @param string|array $rules
220
     * @param null         $callback Dummy attribute to make API seamless with Laravel sometimes()
221 12
     * @return \Proengsoft\JsValidation\Javascript\JavascriptValidator
222
     */
223
    public function sometimes($attribute, $rules, $callback = null)
0 ignored issues
show
Unused Code introduced by
The parameter $callback is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
224
    {
225
        $this->validator->sometimes($attribute, $rules);
226
227
        return $this;
228
    }
229
}
230