1 | <?php |
||
2 | |||
3 | namespace OkayBueno\Validation\src; |
||
4 | |||
5 | use Illuminate\Contracts\Validation\Factory; |
||
6 | use OkayBueno\Validation\Exceptions\ValidationFunctionDoesNotExist; |
||
7 | use OkayBueno\Validation\ValidatorInterface; |
||
8 | |||
9 | /** |
||
10 | * Class LaravelValidator |
||
11 | * @package OkayBueno\Validation\src |
||
12 | */ |
||
13 | abstract class LaravelValidator implements ValidatorInterface |
||
14 | { |
||
15 | |||
16 | protected $validator; |
||
17 | protected $errors = array(); |
||
18 | protected $data = array(); |
||
19 | |||
20 | |||
21 | /** |
||
22 | * @param Factory $validator |
||
23 | */ |
||
24 | public function __construct( Factory $validator ) |
||
25 | { |
||
26 | $this->validator = $validator; |
||
27 | } |
||
28 | |||
29 | |||
30 | /** |
||
31 | * @param array $data |
||
32 | * @return $this |
||
33 | */ |
||
34 | public function with( array $data ) |
||
35 | { |
||
36 | $this->data = $data; |
||
37 | return $this; |
||
38 | } |
||
39 | |||
40 | |||
41 | /** |
||
42 | * @param array $setOfRules |
||
43 | * @return bool |
||
44 | * @throws ValidationFunctionDoesNotExist |
||
45 | */ |
||
46 | public function passes( $setOfRules ) |
||
47 | { |
||
48 | if ( is_string( $setOfRules ) || is_array( $setOfRules ) ) |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
49 | { |
||
50 | $validator = NULL; |
||
51 | if ( is_string( $setOfRules ) && method_exists( $this, $setOfRules ) ) $validator = $this->validator->make( $this->data, $this->{$setOfRules}() ); |
||
0 ignored issues
–
show
|
|||
52 | else if ( is_array( $setOfRules ) ) $validator = $this->validator->make( $this->data, $setOfRules ); |
||
0 ignored issues
–
show
|
|||
53 | else |
||
54 | { |
||
55 | throw new ValidationFunctionDoesNotExist(); |
||
56 | } |
||
57 | |||
58 | if ( !is_null( $validator ) ) |
||
59 | { |
||
60 | if( !$validator->fails() ) return TRUE; |
||
61 | else $this->errors = $validator->messages()->getMessages(); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | return FALSE; |
||
66 | } |
||
67 | |||
68 | |||
69 | /** |
||
70 | * @return mixed |
||
71 | */ |
||
72 | public function errors() |
||
73 | { |
||
74 | return $this->errors; |
||
75 | } |
||
76 | |||
77 | |||
78 | /** |
||
79 | * @param $key |
||
80 | * @return mixed |
||
81 | */ |
||
82 | public function get( $key ) |
||
83 | { |
||
84 | return @$this->data[ $key ]; |
||
85 | } |
||
86 | |||
87 | |||
88 | } |