Completed
Branch delegated-factory (41b3c7)
by Albert
02:04
created

JavascriptValidator::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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