1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Konsulting\Laravel\RuleRepository; |
4
|
|
|
|
5
|
|
|
use Konsulting\Laravel\RuleRepository\Contracts\RuleRepository; |
6
|
|
|
|
7
|
|
|
abstract class AbstractRepository implements RuleRepository |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Prepend a rule to all fields. Rules expressed as pipe-delimited strings will be converted to arrays. |
11
|
|
|
* |
12
|
|
|
* @param string $rule |
13
|
|
|
* @param array $baseRules |
14
|
|
|
* @return array[] |
15
|
|
|
*/ |
16
|
1 |
|
protected function prependRule($rule, $baseRules) |
17
|
|
|
{ |
18
|
1 |
|
return $this->prependRules([$rule], $baseRules); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Prepend rules to all fields. Rules expressed as pipe-delimited strings will be converted to arrays. |
23
|
|
|
* |
24
|
|
|
* @param array $rules |
25
|
|
|
* @param array $baseRules |
26
|
|
|
* @return array[] |
27
|
|
|
*/ |
28
|
2 |
|
protected function prependRules($rules, $baseRules) |
29
|
|
|
{ |
30
|
|
|
return array_map(function ($ruleLine) use ($rules) { |
31
|
2 |
|
return $this->removeDuplicates( |
32
|
2 |
|
array_merge($rules, $this->ensureArray($ruleLine)) |
33
|
|
|
); |
34
|
2 |
|
}, $baseRules); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Append rules to all fields. Rules expressed as pipe-delimited strings will be converted to arrays. |
39
|
|
|
* |
40
|
|
|
* @param array $rules |
41
|
|
|
* @param array $baseRules |
42
|
|
|
* @return array[] |
43
|
|
|
*/ |
44
|
2 |
|
protected function appendRules($rules, $baseRules) |
45
|
|
|
{ |
46
|
|
|
return array_map(function ($ruleLine) use ($rules) { |
47
|
2 |
|
return $this->removeDuplicates( |
48
|
2 |
|
array_merge($this->ensureArray($ruleLine), $rules) |
49
|
|
|
); |
50
|
2 |
|
}, $baseRules); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Append a rule to all fields. Rules expressed as pipe-delimited strings will be converted to arrays. |
55
|
|
|
* |
56
|
|
|
* @param string $rule |
57
|
|
|
* @param array $baseRules |
58
|
|
|
* @return array[] |
59
|
|
|
*/ |
60
|
1 |
|
protected function appendRule($rule, $baseRules) |
61
|
|
|
{ |
62
|
1 |
|
return $this->appendRules([$rule], $baseRules); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Make sure the rules are expressed in array format. |
67
|
|
|
* |
68
|
|
|
* @param array|string|mixed $ruleLine |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
4 |
|
private function ensureArray($ruleLine) |
72
|
|
|
{ |
73
|
4 |
|
if (empty($ruleLine)) { |
74
|
4 |
|
return []; |
75
|
4 |
|
} elseif (is_string($ruleLine)) { |
76
|
4 |
|
return explode('|', $ruleLine); |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
return is_array($ruleLine) ? $ruleLine : [$ruleLine]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Remove duplicate items and reset numerical keys. |
84
|
|
|
* |
85
|
|
|
* @param array $array |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
4 |
|
private function removeDuplicates($array) |
89
|
|
|
{ |
90
|
4 |
|
return array_values(array_unique($array, SORT_REGULAR)); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|