-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvalidator.php
More file actions
194 lines (160 loc) · 3.95 KB
/
Copy pathvalidator.php
File metadata and controls
194 lines (160 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php namespace Feather\Components\Validation;
use DB;
use Laravel\Messages;
use InvalidArgumentException;
use FeatherValidationException;
use Feather\Components\Foundation\Component;
class Validator extends Component {
/**
* Application to load from.
*
* @var string
*/
public $application = 'core';
/**
* The items currently validating.
*
* @var array
*/
public $validating;
/**
* Input to validate against.
*
* @var array
*/
public $input = array();
/**
* Rules to use for validation.
*
* @var array
*/
public $rules = array();
/**
* Error messages to use for validation.
*
* @var array
*/
public $messages = array();
/**
* Data to be used when setting rules.
*
* @var array
*/
public $data = array();
/**
* Sets the application.
*
* @param string $application
* @return Feather\Components\Support\Validation
*/
public function app($application)
{
$this->application = $application;
return $this;
}
/**
* Get a validator.
*
* @param string $validator
* @return Feather\Components\Support\Validation
*/
public function get($validator)
{
$segments = explode('.', $validator);
if(file_exists($path = path('applications') . $this->application . DS . 'validation' . DS . $segments[0] . EXT))
{
if($this->validating[$validator] = array_get(require $path, implode('.', array_slice($segments, 1)) ?: null))
{
return $this;
}
}
if($this->validating[$validator] = $this->feather['config']->get("feather {$this->application}: validation.{$validator}"))
{
return $this;
}
throw new InvalidArgumentException("Invalid validator [{$validator}] supplied.");
}
/**
* Sets input to validate against.
*
* @param array $input
* @return Feather\Components\Support\Validation
*/
public function against($input)
{
$this->input = $input;
return $this;
}
/**
* Executes validation and returns true on success.
*
* @return bool
*/
public function passes()
{
// If a response is returned from the closure then we have a custom error messages
// to throw with the exception.
if($responses = call_user_func_array(reset($this->validating), array($this, $this->feather)))
{
if(!is_array(reset($responses))) $responses = array(array_shift($responses) => array());
foreach($responses as $response => $replacements)
{
$responses[$response] = __("feather {$this->application}::{$response}", $replacements)->get();
}
throw new FeatherValidationException(new Messages($responses));
}
$event = key($this->validating);
$this->feather['gear']->fire("validation: before {$event}", array($this));
$validator = new \Validator($this->input, $this->rules, $this->messages);
if($validator->connection(DB::connection(FEATHER_DATABASE))->fails())
{
throw new FeatherValidationException($validator->errors);
}
return true;
}
/**
* Adds a rule or array of rules to the rules array.
*
* @param string $name
* @param array|string $rule
* @return Feather\Components\Support\Validation
*/
public function rule($name, $rule)
{
if(!isset($this->rules[$name]))
{
$this->rules[$name] = array();
}
$this->rules[$name] = array_merge($this->rules[$name], (array) $rule);
return $this;
}
/**
* Adds a message to the messages array.
*
* @param string $rule
* @param array|string $message
* @return Feather\Components\Support\Validation
*/
public function message($rule, $message)
{
$replacements = array();
if(is_array($message))
{
list($message, $replacements) = $message;
}
$this->messages[str_replace('.', '_', $rule)] = __("feather {$this->application}::{$message}", $replacements)->get();
return $this;
}
/**
* Adds a data key/value pair to the data array.
*
* @param string $key
* @param mixed $value
* @return Feather\Components\Support\Validation
*/
public function with($key, $value)
{
$this->data[$key] = $value;
return $this;
}
}