Completed
Push — master ( a91d24...d5e6c7 )
by Renato
07:02
created

BaseValidator::getValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 9.4285
1
<?php
2
namespace NwLaravel\Validation;
3
4
use Prettus\Validator\LaravelValidator;
5
6
/**
7
 * Class BaseValidator
8
 * @abstract
9
 */
10
abstract class BaseValidator extends LaravelValidator
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $keyName;
16
17
    /**
18
     * Get Validator
19
     *
20
     * @return \Illuminate\Validation\Factory
21
     */
22 1
    public function getValidator()
23
    {
24 1
        return $this->validator;
25
    }
26
27
    /**
28
     * Set Key Name
29
     *
30
     * @param string $keyName
31
     *
32
     * @return BaseValidator
33
     */
34 1
    public function setKeyName($keyName)
35
    {
36 1
        $this->keyName = $keyName;
37 1
    }
38
39
    /**
40
     * Get rule for validation by action ValidatorInterface::RULE_CREATE or ValidatorInterface::RULE_UPDATE
41
     *
42
     * Default rule: ValidatorInterface::RULE_CREATE
43
     *
44
     * @param string|null $action
45
     *
46
     * @return array
47
     */
48 1
    public function getRules($action = null)
49
    {
50 1
        $rules = [];
51
52 1
        if (isset($this->rules[$action])) {
53 1
            $rules = $this->rules[$action];
54 1
        }
55
56 1
        return $this->parserValidationRules($rules, $this->id);
57
    }
58
59
    /**
60
     * Parser Validation Rules
61
     *
62
     * @param array    $rules
63
     * @param int|null $id
64
     *
65
     * @return array
66
     */
67 1
    protected function parserValidationRules($rules, $id = null)
68
    {
69
70 1
        if ($id === null) {
71 1
            return $rules;
72
        }
73
74 1
        array_walk($rules, function (&$rules, $field) use ($id) {
75 1
            if (!is_array($rules)) {
76 1
                $rules = explode("|", $rules);
77 1
            }
78
79 1
            foreach ($rules as $ruleIdx => $rule) {
80
                // get name and parameters
81 1
                list($name, $params) = array_pad(explode(":", $rule), 2, null);
82
83
                // only do someting for the unique rule
84 1
                if (strtolower($name) != "unique") {
85 1
                    if (preg_match('/\[(.*)\]/', $params, $matches)) {
86 1
                        if (array_key_exists($matches[1], $this->data)) {
87 1
                            $params = str_replace("[".$matches[1]."]", $this->getValue($matches[1]), $params);
88 1
                            $rules[$ruleIdx] = $name.":".$params;
89 1
                        }
90 1
                    }
91 1
                    continue; // continue in foreach loop, nothing left to do here
92
                }
93
94 1
                $p = array_map("trim", explode(",", $params));
95
96
                // set field name to rules key ($field) (laravel convention)
97 1
                if (!isset($p[1])) {
98 1
                    $p[1] = $field;
99 1
                }
100
101
                // set 3rd parameter to id given to getValidationRules()
102 1
                if (!isset($p[2]) || empty($p[2])) {
103 1
                    $p[2] = $id;
104 1
                }
105
106 1
                if ($this->keyName && (!isset($p[3]) || empty($p[3]))) {
107 1
                    $p[3] = $this->keyName;
108 1
                }
109
110 1
                $params = implode(",", $p);
111 1
                $rules[$ruleIdx] = $name.":".$params;
112 1
            }
113 1
        });
114
115 1
        return $rules;
116
    }
117
118
    /**
119
     * Get the value of a given attribute.
120
     *
121
     * @param  string  $attribute
122
     * @return mixed
123
     */
124 1
    protected function getValue($attribute)
125
    {
126 1
        if (! is_null($value = array_get($this->data, $attribute))) {
127 1
            return $value;
128
        }
129
    }
130
}
131