1
|
|
|
<?php namespace Rocket\UI\Forms\ValidatorAdapters; |
2
|
|
|
|
3
|
|
|
use Illuminate\Validation\Validator; |
4
|
|
|
|
5
|
|
|
class LaravelValidator implements ValidatorInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var Validator |
9
|
|
|
*/ |
10
|
|
|
protected $validator; |
11
|
|
|
protected $data; |
12
|
|
|
protected $defaults; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var \Illuminate\Session\Store |
16
|
|
|
*/ |
17
|
|
|
protected $session; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
public function __construct($validator, $data, $defaults) |
23
|
|
|
{ |
24
|
|
|
$this->validator = $validator; |
25
|
|
|
$this->data = $data; |
26
|
|
|
$this->defaults = $defaults; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return \Illuminate\Session\Store |
31
|
|
|
*/ |
32
|
|
|
protected function getSession() |
33
|
|
|
{ |
34
|
|
|
//TODO :: do that by injection |
35
|
|
|
if (!$this->session) { |
36
|
|
|
$this->session = app('session'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this->session; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function hasError($name) |
46
|
|
|
{ |
47
|
|
|
// The errors must be taken from the session, or else we have errors even if the form wasn't sent |
48
|
|
|
$session = $this->getSession(); |
49
|
|
|
if ($session->has('errors')) { |
50
|
|
|
return $session->get('errors')->has($name); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function getErrors($name) |
60
|
|
|
{ |
61
|
|
|
// The errors must be taken from the session, or else we have errors even if the form wasn't sent |
62
|
|
|
$session = $this->getSession(); |
63
|
|
|
if ($session->has('errors')) { |
64
|
|
|
$errors = $session->get('errors'); |
65
|
|
|
|
66
|
|
|
if ($errors->has($name)) { |
67
|
|
|
return $errors->get($name); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return ''; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Transform key from array to dot syntax. |
76
|
|
|
* |
77
|
|
|
* @param string $key |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
protected function transformKey($key) |
82
|
|
|
{ |
83
|
|
|
return str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], $key); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the current value. |
88
|
|
|
* |
89
|
|
|
* With the following priority: |
90
|
|
|
* 1. If the field was posted, take that value |
91
|
|
|
* 2. If there is a model that has a value, take it |
92
|
|
|
* 3. If there is a value defined when showing the field |
93
|
|
|
* 4. If there is a default set in the validator |
94
|
|
|
* |
95
|
|
|
* @param string $name |
96
|
|
|
* @param string $default |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
public function getValue($name, $default = '') |
100
|
|
|
{ |
101
|
|
|
// 1. |
102
|
|
|
$old = $this->getSession()->getOldInput($this->transformKey($name)); |
103
|
|
|
if (!is_null($old)) { |
104
|
|
|
return $old; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// 2. |
108
|
|
|
if (!empty($this->data) && $value = data_get($this->model, $this->transformKey($name))) { |
|
|
|
|
109
|
|
|
return $value; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// 3. |
113
|
|
|
if (!empty($default)) { |
114
|
|
|
return $default; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// 4. |
118
|
|
|
if (!empty($this->defaults) && $value = data_get($this->defaults, $this->transformKey($name))) { |
119
|
|
|
return $value; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return ''; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function isRequired($name) |
129
|
|
|
{ |
130
|
|
|
$rules = $this->validator->getRules($name); |
131
|
|
|
if (array_key_exists($name, $rules)) { |
132
|
|
|
return is_array($rules[$name]) ? in_array('required', $rules[$name]) : |
133
|
|
|
str_contains($rules[$name], 'required'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public static function supports($object) |
143
|
|
|
{ |
144
|
|
|
return $object instanceof Validator; |
|
|
|
|
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: