Completed
Push — master ( 4f2888...c77bf4 )
by Renato
06:46
created

BaseValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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