Rules::parseRules()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
ccs 10
cts 10
cp 1
crap 3
1
<?php namespace Arcanedev\Sanitizer\Entities;
2
3
use Arcanedev\Sanitizer\Exceptions\InvalidFilterException;
4
5
/**
6
 * Class     Rules
7
 *
8
 * @package  Arcanedev\Sanitizer\Entities
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class Rules
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /** @var array */
18
    protected $items = [];
19
20
    /* ------------------------------------------------------------------------------------------------
21
     |  Constructor
22
     | ------------------------------------------------------------------------------------------------
23
     */
24
    /**
25
     * Rules constructor.
26
     */
27 315
    public function __construct()
28
    {
29 315
        $this->items = [];
30 315
    }
31
32
    /* ------------------------------------------------------------------------------------------------
33
     |  Main Functions
34
     | ------------------------------------------------------------------------------------------------
35
     */
36
    /**
37
     * Set multiple rules.
38
     *
39
     * @param  array  $rules
40
     *
41
     * @return $this
42
     */
43 225
    public function set(array $rules)
44
    {
45 225
        $this->items = array_merge(
46 225
            $this->items,
47 225
            $this->parseRules($rules)
48 72
        );
49
50 216
        return $this;
51
    }
52
53
    /**
54
     * Check attribute if has a rule.
55
     *
56
     * @param  string  $attribute
57
     *
58
     * @return bool
59
     */
60 216
    public function has($attribute)
61
    {
62 216
        return isset($this->items[$attribute]);
63
    }
64
65
    /*
66
     * Get the attribute filters.
67
     *
68
     * @param  string  $attribute
69
     *
70
     * @return array
71
     */
72 216
    public function get($attribute)
73
    {
74 216
        return $this->has($attribute) ? $this->items[$attribute] : [];
75
    }
76
77
    /* ------------------------------------------------------------------------------------------------
78
     |  Other Functions
79
     | ------------------------------------------------------------------------------------------------
80
     */
81
    /**
82
     * Parse a rules array.
83
     *
84
     * @param  array  $rules
85
     *
86
     * @return array
87
     *
88
     * @throws \Arcanedev\Sanitizer\Exceptions\InvalidFilterException
89
     */
90 225
    protected function parseRules(array $rules)
91
    {
92 225
        $parsedRules = [];
93
94 225
        foreach ($rules as $attribute => $filters) {
95 225
            if (empty($filters)) {
96 9
                throw new InvalidFilterException(
97 9
                    "The attribute [$attribute] must contain at least one filter."
98 3
                );
99
            }
100
101 216
            $this->parseAttributeFilters($parsedRules, $attribute, $filters);
102 72
        }
103
104 216
        return $parsedRules;
105
    }
106
107
    /**
108
     * Parse attribute's filters.
109
     *
110
     * @param  array   $parsedRules
111
     * @param  string  $attribute
112
     * @param  string  $filters
113
     */
114 216
    protected function parseAttributeFilters(array &$parsedRules, $attribute, $filters)
115
    {
116 216
        foreach (explode('|', $filters) as $filter) {
117 216
            $parsedFilter = $this->parseRule($filter);
118
119 216
            if ( ! empty($parsedFilter)) {
120 216
                $parsedRules[$attribute][] = $parsedFilter;
121 72
            }
122 72
        }
123 216
    }
124
125
    /**
126
     * Parse a rule.
127
     *
128
     * @param  string  $rule
129
     *
130
     * @return array
131
     */
132 216
    protected function parseRule($rule)
133
    {
134 216
        $name    = $rule;
135 216
        $options = [];
136
137 216
        if (str_contains($rule, ':')) {
138 36
            list($name, $options) = explode(':', $rule, 2);
139 36
            $options              = array_map('trim', explode(',', $options));
140 12
        }
141
142 216
        return empty($name) ? [] : compact('name', 'options');
143
    }
144
}
145