Passed
Push — master ( 731a27...4e7602 )
by Jesús
01:49
created

LaravelValidator::passes()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
rs 7.7777
cc 8
eloc 10
nc 7
nop 1
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 ) )
49
        {
50
            $validator = NULL;
51
            if ( is_string( $setOfRules ) && method_exists( $this, $setOfRules ) ) $validator = $this->validator->make( $this->data, $this->{$setOfRules}() );
52
            else if ( is_array( $setOfRules ) ) $validator = $this->validator->make( $this->data, $setOfRules );
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();
0 ignored issues
show
Bug introduced by
The method messages() does not exist on Illuminate\Contracts\Validation\Validator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
                else $this->errors = $validator->/** @scrutinizer ignore-call */ messages()->getMessages();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
}