Completed
Branch feature/conditional-validation... (02769e)
by Albert
34:59 queued 25:31
created

JavascriptValidator::sometimes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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