Completed
Push — master ( 328c23...368b19 )
by
unknown
07:01 queued 12s
created

JavascriptValidator::setDefaults()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 8
nop 1
crap 4
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 \Proengsoft\JsValidation\Javascript\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
     * @param \Proengsoft\JsValidation\Javascript\ValidatorHandler $validator
49
     * @param array $options
50
     */
51 192
    public function __construct(ValidatorHandler $validator, $options = [])
52
    {
53 192
        $this->validator = $validator;
54 192
        $this->setDefaults($options);
55 192
    }
56
57
    /**
58
     * Set default parameters.
59
     *
60
     * @param $options
61
     * @return void
62
     */
63 192
    protected function setDefaults($options)
64
    {
65 192
        $this->selector = empty($options['selector']) ? 'form' : $options['selector'];
66 192
        $this->view = empty($options['view']) ? 'jsvalidation::bootstrap' : $options['view'];
67 192
        $this->remote = isset($options['remote']) ? $options['remote'] : true;
68 192
    }
69
70
    /**
71
     * Render the specified view with validator data.
72
     *
73
     * @param null|\Illuminate\Contracts\View\View|string $view
74
     * @param null|string $selector
75
     * @return string
76
     */
77 48
    public function render($view = null, $selector = null)
78
    {
79 48
        $this->view($view);
80 48
        $this->selector($selector);
81
82 48
        return View::make($this->view, ['validator' => $this->getViewData()])
83 36
            ->render();
84
    }
85
86
    /**
87
     * Get the view data as an array.
88
     *
89
     * @return array
90
     */
91 48
    public function toArray()
92
    {
93 48
        return $this->getViewData();
94
    }
95
96
    /**
97
     * Get the string resulting of render default view.
98
     *
99
     * @return string
100
     */
101 24
    public function __toString()
102
    {
103
        try {
104 24
            return $this->render();
105 12
        } catch (Exception $exception) {
106 12
            return trigger_error($exception->__toString(), E_USER_ERROR);
107
        }
108
    }
109
110
    /**
111
     * Gets value from view data.
112
     *
113
     * @param $name
114
     * @return string
115
     *
116
     * @throws \Proengsoft\JsValidation\Exceptions\PropertyNotFoundException
117
     */
118 24
    public function __get($name)
119
    {
120 24
        $data = $this->getViewData();
121 24
        if (! array_key_exists($name, $data)) {
122 12
            throw new PropertyNotFoundException($name, get_class());
123
        }
124
125 12
        return $data[$name];
126
    }
127
128
    /**
129
     * Gets view data.
130
     *
131
     * @return array
132
     */
133 120
    protected function getViewData()
134
    {
135 120
        $this->validator->setRemote($this->remote);
136 120
        $data = $this->validator->validationData();
137 108
        $data['selector'] = $this->selector;
138
139 108
        if (! is_null($this->ignore)) {
140 12
            $data['ignore'] = $this->ignore;
141 1
        }
142
143 108
        return $data;
144
    }
145
146
    /**
147
     * Set the form selector to validate.
148
     *
149
     * @param string $selector
150
     * @deprecated
151
     */
152
    public function setSelector($selector)
153
    {
154
        $this->selector = $selector;
155
    }
156
157
    /**
158
     * Set the form selector to validate.
159
     *
160
     * @param string $selector
161
     * @return \Proengsoft\JsValidation\Javascript\JavascriptValidator
162
     */
163 48
    public function selector($selector)
164
    {
165 48
        $this->selector = is_null($selector) ? $this->selector : $selector;
166
167 48
        return $this;
168
    }
169
170
    /**
171
     * Set the input selector to ignore for validation.
172
     *
173
     * @param string $ignore
174
     * @return \Proengsoft\JsValidation\Javascript\JavascriptValidator
175
     */
176 12
    public function ignore($ignore)
177
    {
178 12
        $this->ignore = $ignore;
179
180 12
        return $this;
181
    }
182
183
    /**
184
     * Set the view to render Javascript Validations.
185
     *
186
     * @param null|\Illuminate\Contracts\View\View|string $view
187
     * @return \Proengsoft\JsValidation\Javascript\JavascriptValidator
188
     */
189 48
    public function view($view)
190
    {
191 48
        $this->view = is_null($view) ? $this->view : $view;
192
193 48
        return $this;
194
    }
195
196
    /**
197
     * Enables or disables remote validations.
198
     *
199
     * @param null|bool $enabled
200
     * @return \Proengsoft\JsValidation\Javascript\JavascriptValidator
201
     */
202 12
    public function remote($enabled = true)
203
    {
204 12
        $this->remote = $enabled;
205
206 12
        return $this;
207
    }
208
209
    /**
210
     * Validate Conditional Validations using Ajax in specified fields.
211
     *
212
     * @param string $attribute
213
     * @param string|array $rules
214
     * @return \Proengsoft\JsValidation\Javascript\JavascriptValidator
215
     */
216 12
    public function sometimes($attribute, $rules)
217
    {
218 12
        $this->validator->sometimes($attribute, $rules);
219
220 12
        return $this;
221
    }
222
}
223