Completed
Push — master ( c70cfd...9bf1c7 )
by Renato
06:14
created

ValidatorResolver::validateNotExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace NwLaravel\Validation;
4
5
use ReflectionClass;
6
use Illuminate\Validation\Validator as BaseValidator;
7
8
/**
9
 * Class ValidatorResolver
10
 *
11
 * @method bool validateCpf(string $attribute, mixed $value, array $parameters)
12
 * @method bool validateCnpj(string $attribute, mixed $value, array $parameters)
13
 */
14
class ValidatorResolver extends BaseValidator
15
{
16
    /**
17
     * Validate Pattern Valid
18
     *
19
     * @param string $attribute  String Attribute
20
     * @param string $value      String Value
21
     * @param array  $parameters Array Parameters
22
     *
23
     * @return boolean
24
     */
25 1
    public function validatePattern($attribute, $value, $parameters = array())
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
    {
27 1
        return (bool) (@preg_match($value, "subject") !== false);
28
    }
29
30
    /**
31
     * Validate Current Password
32
     *
33
     * @param string $attribute  String Attribute
34
     * @param mixed  $value      Mixed Value
35
     * @param array  $parameters Array Parameters
36
     *
37
     * @return bool
38
     */
39 1
    public function validateCurrentPassword($attribute, $value, $parameters = array())
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41 1
        return password_verify($value, $parameters[0]);
42
    }
43
44
    /**
45
     * Validate that an attribute is cpf valid
46
     *
47
     * @param string $attribute String Attribute
48
     * @param mixed  $value     Mixed Value
49
     * @param array  $parameters Array Parameters
50
     *
51
     * @return bool
52
     */
53 1
    public function validateDocument($attribute, $value, $parameters = array())
54
    {
55 1
        $value = preg_replace('/[^0-9]/', '', $value);
56 1
        if (strlen($value) == 11) {
57 1
            return $this->validateCpf($attribute, $value, $parameters);
58
        }
59
60 1
        return $this->validateCnpj($attribute, $value, $parameters);
61
    }
62
63
    /**
64
     * Validate the not existence of an attribute value in a database table.
65
     *
66 9
     * @param  string  $attribute
67
     * @param  mixed   $value
68 9
     * @param  array   $parameters
69 8
     * @return bool
70
     */
71 8
    public function validateNotExists($attribute, $value, $parameters)
72 8
    {
73 8
        return !$this->validateExists($attribute, $value, $parameters);
74 8
    }
75 8
76 8
    /**
77 8
     * {@inheritdoc}
78 8
     */
79
    public function __call($method, $parameters)
80 9
    {
81 8
        if (preg_match('/^validate([A-Z][a-z][a-zA-Z]*)$/', $method, $match) && count($parameters) >= 2) {
82
            $className = 'Respect\\Validation\\Rules\\'.ucfirst($match[1]);
83
84 1
            if (class_exists($className)) {
85
                $reflection = new ReflectionClass($className);
86
                if (!$reflection->isAbstract() && $reflection->isSubclassOf('Respect\\Validation\\Validatable')) {
87
                    $arguments = (array) (isset($parameters[2]) ? $parameters[2] : []);
88
                    $valided = app($className, $arguments)->validate($parameters[1]);
89
                }
90
            }
91
        }
92
93
        if (isset($valided)) {
94
            return $valided;
95
        }
96
97
        return parent::__call($method, $parameters);
98
    }
99
}
100