Passed
Pull Request — master (#27)
by Jitendra
01:46
created

Rules::parseRules()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace PhalconExt\Validation;
4
5
/**
6
 * Validation rules helper.
7
 *
8
 * @author  Jitendra Adhikari <[email protected]>
9
 * @license MIT
10
 *
11
 * @link    https://github.com/adhocore/phalcon-ext
12
 */
13
class Rules
14
{
15
    /** @var Validation */
16
    protected $validation;
17
18
    public function __construct(Validation $validation)
19
    {
20
        $this->validation = $validation;
21
    }
22
23
    /**
24
     * Normalize rules if needed.
25
     *
26
     * @param mixed $rules
27
     *
28
     * @return array
29
     */
30
    public function normalizeRules($rules): array
31
    {
32
        if (\is_string($rules)) {
33
            $rules = $this->parseRules($rules);
34
        }
35
36
        if (!\is_array($rules)) {
37
            throw new \UnexpectedValueException('The rules should be an array or string');
38
        }
39
40
        return $this->cancelOnFail($rules);
41
    }
42
43
    /**
44
     * Parse string representation of the rules and make it array.
45
     *
46
     * Rule Format: `rule1:key1:value11,value12;key2:value22|rule2:key21:value21|rule3`
47
     *
48
     * @param string $rules Example: 'required|length:min:1;max:2;|in:domain:1,12,30'
49
     *
50
     * @return array
51
     */
52
    protected function parseRules(string $rules): array
53
    {
54
        $parsed = [];
55
56
        foreach (\explode('|', $rules) as $rule) {
57
            if (false === \strpos($rule, ':')) {
58
                $parsed[$rule] = [];
59
                continue;
60
            }
61
62
            list($name, $options) = \explode(':', $rule, 2);
63
            $parsed[$name]        = $this->parseOptions($options);
64
        }
65
66
        return $parsed;
67
    }
68
69
    /**
70
     * Parse rule options.
71
     *
72
     * @param string $options
73
     *
74
     * @return array
75
     */
76
    protected function parseOptions(string $options): array
77
    {
78
        $parsed = [];
79
80
        foreach (\explode(';', $options) as $parts) {
81
            list($key, $value) = \explode(':', $parts) + ['', ''];
82
            if (\strpos($value, ',')) {
83
                $value = \explode(',', $value);
84
            }
85
86
            $parsed[$key] = $value;
87
        }
88
89
        return $parsed;
90
    }
91
92
    /**
93
     * Make the validator cancel on fail i.e bail on first ever invalid field.
94
     *
95
     * @param array $rules
96
     *
97
     * @return array
98
     */
99
    protected function cancelOnFail(array $rules): array
100
    {
101
        if (!isset($rules['abort'])) {
102
            return $rules;
103
        }
104
105
        unset($rules['abort']);
106
        foreach ($rules as &$rule) {
107
            $rule = (array) $rule + ['cancelOnFail' => true];
108
        }
109
110
        return $rules;
111
    }
112
}
113