Skip to content

Commit d9eb401

Browse files
authored
Merge pull request #27 from mapb1990/master
Compatibility with laravel 5.4
2 parents 64f66f7 + d7134c0 commit d9eb401

File tree

5 files changed

+44
-49
lines changed

5 files changed

+44
-49
lines changed

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
],
1111
"require": {
1212
"php": ">=5.4.0",
13-
"illuminate/support": "~5.0|~5.1",
14-
"illuminate/validation": "~5.0|~5.1"
13+
"illuminate/support": "~5.4",
14+
"illuminate/validation": "~5.4"
1515
},
1616
"autoload": {
1717
"psr-4": {
1818
"Prettus\\Validator\\": "src/Prettus/Validator/"
1919
}
2020
},
21-
"minimum-stability": "stable"
21+
"minimum-stability": "dev"
2222
}

src/Prettus/Validator/AbstractValidator.php

+23-26
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* Class AbstractValidator
99
* @package Prettus\Validator
1010
*/
11-
abstract class AbstractValidator implements ValidatorInterface {
12-
11+
abstract class AbstractValidator implements ValidatorInterface
12+
{
1313
/**
1414
* @var int
1515
*/
@@ -64,7 +64,8 @@ abstract class AbstractValidator implements ValidatorInterface {
6464
* @param $id
6565
* @return $this
6666
*/
67-
public function setId($id){
67+
public function setId($id)
68+
{
6869
$this->id = $id;
6970
return $this;
7071
}
@@ -108,7 +109,7 @@ public function errorsBag()
108109
* @param string $action
109110
* @return boolean
110111
*/
111-
abstract public function passes( $action = null );
112+
abstract public function passes($action = null);
112113

113114
/**
114115
* Pass the data and the rules to the validator or throws ValidatorException
@@ -119,8 +120,8 @@ abstract public function passes( $action = null );
119120
*/
120121
public function passesOrFail($action = null)
121122
{
122-
if( !$this->passes($action) ){
123-
throw new ValidatorException( $this->errorsBag() );
123+
if (!$this->passes($action)) {
124+
throw new ValidatorException($this->errorsBag());
124125
}
125126

126127
return true;
@@ -134,11 +135,11 @@ public function passesOrFail($action = null)
134135
* @param null $action
135136
* @return array
136137
*/
137-
public function getRules($action = null){
138-
138+
public function getRules($action = null)
139+
{
139140
$rules = $this->rules;
140141

141-
if( isset($this->rules[$action]) ){
142+
if (isset($this->rules[$action])) {
142143
$rules = $this->rules[$action];
143144
}
144145

@@ -162,14 +163,15 @@ public function setRules(array $rules)
162163
*
163164
* @return array
164165
*/
165-
public function getMessages(){
166-
166+
public function getMessages()
167+
{
167168
return $this->messages;
168169
}
169170

170171
/**
171172
* Set Custom error messages for Validation
172173
*
174+
* @param array $messages
173175
* @return $this
174176
*/
175177
public function setMessages(array $messages)
@@ -183,14 +185,15 @@ public function setMessages(array $messages)
183185
*
184186
* @return array
185187
*/
186-
public function getAttributes(){
187-
188+
public function getAttributes()
189+
{
188190
return $this->attributes;
189191
}
190192

191193
/**
192194
* Set Custom error attributes for Validation
193195
*
196+
* @param array $attributes
194197
* @return $this
195198
*/
196199
public function setAttributes(array $attributes)
@@ -208,33 +211,28 @@ public function setAttributes(array $attributes)
208211
*/
209212
protected function parserValidationRules($rules, $id = null)
210213
{
211-
212-
if($id === null)
213-
{
214+
if (null === $id) {
214215
return $rules;
215216
}
216217

217-
array_walk($rules, function(&$rules, $field) use ($id)
218-
{
219-
if(!is_array($rules))
220-
{
218+
array_walk($rules, function (&$rules, $field) use ($id) {
219+
if (!is_array($rules)) {
221220
$rules = explode("|", $rules);
222221
}
223222

224-
foreach($rules as $ruleIdx => $rule)
225-
{
223+
foreach ($rules as $ruleIdx => $rule) {
226224
// get name and parameters
227225
@list($name, $params) = array_pad(explode(":", $rule), 2, null);
228226

229227
// only do someting for the unique rule
230-
if(strtolower($name) != "unique") {
228+
if (strtolower($name) != "unique") {
231229
continue; // continue in foreach loop, nothing left to do here
232230
}
233231

234232
$p = array_map("trim", explode(",", $params));
235233

236234
// set field name to rules key ($field) (laravel convention)
237-
if(!isset($p[1])) {
235+
if (!isset($p[1])) {
238236
$p[1] = $field;
239237
}
240238

@@ -248,5 +246,4 @@ protected function parserValidationRules($rules, $id = null)
248246

249247
return $rules;
250248
}
251-
252-
}
249+
}

src/Prettus/Validator/Contracts/ValidatorInterface.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* Interface ValidatorInterface
88
* @package Prettus\Validator\Contracts
99
*/
10-
interface ValidatorInterface {
11-
10+
interface ValidatorInterface
11+
{
1212
const RULE_CREATE = 'create';
1313
const RULE_UPDATE = 'update';
1414

@@ -34,8 +34,7 @@ public function with(array $input);
3434
* @param string $action
3535
* @return boolean
3636
*/
37-
public function passes( $action = null );
38-
37+
public function passes($action = null);
3938

4039
/**
4140
* Pass the data and the rules to the validator or throws ValidatorException
@@ -44,7 +43,7 @@ public function passes( $action = null );
4443
* @param string $action
4544
* @return boolean
4645
*/
47-
public function passesOrFail( $action = null );
46+
public function passesOrFail($action = null);
4847

4948
/**
5049
* Errors
@@ -77,4 +76,4 @@ public function setRules(array $rules);
7776
* @return array
7877
*/
7978
public function getRules($action = null);
80-
}
79+
}

src/Prettus/Validator/Exceptions/ValidatorException.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* Class ValidatorException
99
* @package Prettus\Validator\Exceptions
1010
*/
11-
class ValidatorException extends \Exception implements Jsonable, Arrayable {
12-
11+
class ValidatorException extends \Exception implements Jsonable, Arrayable
12+
{
1313
/**
1414
* @var MessageBag
1515
*/
@@ -18,14 +18,16 @@ class ValidatorException extends \Exception implements Jsonable, Arrayable {
1818
/**
1919
* @param MessageBag $messageBag
2020
*/
21-
public function __construct(MessageBag $messageBag){
21+
public function __construct(MessageBag $messageBag)
22+
{
2223
$this->messageBag = $messageBag;
2324
}
2425

2526
/**
2627
* @return MessageBag
2728
*/
28-
public function getMessageBag(){
29+
public function getMessageBag()
30+
{
2931
return $this->messageBag;
3032
}
3133

@@ -52,5 +54,4 @@ public function toJson($options = 0)
5254
{
5355
return json_encode($this->toArray(), $options);
5456
}
55-
56-
}
57+
}

src/Prettus/Validator/LaravelValidator.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php namespace Prettus\Validator;
22

3-
use Illuminate\Validation\Factory;
3+
use Illuminate\Contracts\Validation\Factory;
44

55
/**
66
* Class LaravelValidator
77
* @package Prettus\Validator
88
*/
9-
class LaravelValidator extends AbstractValidator {
10-
9+
class LaravelValidator extends AbstractValidator
10+
{
1111
/**
1212
* Validator
1313
*
@@ -18,7 +18,7 @@ class LaravelValidator extends AbstractValidator {
1818
/**
1919
* Construct
2020
*
21-
* @param \Illuminate\Validation\Factory $validator
21+
* @param \Illuminate\Contracts\Validation\Factory $validator
2222
*/
2323
public function __construct(Factory $validator)
2424
{
@@ -38,13 +38,11 @@ public function passes($action = null)
3838
$attributes = $this->getAttributes();
3939
$validator = $this->validator->make($this->data, $rules, $messages, $attributes);
4040

41-
if( $validator->fails() )
42-
{
41+
if ($validator->fails()) {
4342
$this->errors = $validator->messages();
4443
return false;
4544
}
4645

4746
return true;
4847
}
49-
50-
}
48+
}

0 commit comments

Comments
 (0)