| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentValidator; |
| 4 |
|
| 5 |
class ValidationRuleParser |
| 6 |
{ |
| 7 |
/** |
| 8 |
* The data being validated. |
| 9 |
* |
| 10 |
* @var array |
| 11 |
*/ |
| 12 |
public $data; |
| 13 |
|
| 14 |
/** |
| 15 |
* Create a new validation rule parser. |
| 16 |
* |
| 17 |
* @param array $data |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
public function __construct(array $data) |
| 22 |
{ |
| 23 |
$this->data = $data; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Parse the human-friendly rules into a full rules array for the validator. |
| 28 |
* |
| 29 |
* @param $rules |
| 30 |
* |
| 31 |
* @return array |
| 32 |
*/ |
| 33 |
public function explode($rules) |
| 34 |
{ |
| 35 |
return $this->explodeRules($rules); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Explode the rules into an array of explicit rules. |
| 40 |
* |
| 41 |
* @param array $rules |
| 42 |
* |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
protected function explodeRules($rules) |
| 46 |
{ |
| 47 |
foreach ($rules as $attribute => $rule) { |
| 48 |
if (fluentform_mb_strpos($attribute, '*') !== false) { |
| 49 |
$rules = $this->explodeWildcardRules($rules, $attribute, [$rule]); |
| 50 |
|
| 51 |
unset($rules[$attribute]); |
| 52 |
} else { |
| 53 |
$rules[$attribute] = $this->explodeExplicitRule($rule); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return $rules; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Explode the explicit rule into an array if necessary. |
| 62 |
* |
| 63 |
* @param mixed $rule |
| 64 |
* |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
protected function explodeExplicitRule($rule) |
| 68 |
{ |
| 69 |
if (is_string($rule)) { |
| 70 |
return explode('|', $rule); |
| 71 |
} |
| 72 |
|
| 73 |
var_dump('check laravel'); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Define a set of rules that apply to each element in an array attribute. |
| 78 |
* |
| 79 |
* @param array $results |
| 80 |
* @param string $attribute |
| 81 |
* @param string|array $rules |
| 82 |
* |
| 83 |
* @return array |
| 84 |
*/ |
| 85 |
protected function explodeWildcardRules($results, $attribute, $rules) |
| 86 |
{ |
| 87 |
$pattern = str_replace('\*', '[^\.]*', preg_quote($attribute)); |
| 88 |
|
| 89 |
$data = ValidationData::initializeAndGatherData($attribute, $this->data); |
| 90 |
|
| 91 |
foreach ($data as $key => $value) { |
| 92 |
if (substr($key, 0, strlen($attribute)) === $attribute || (bool) preg_match('/^'.$pattern.'\z/', $key)) { |
| 93 |
foreach ((array) $rules as $rule) { |
| 94 |
$results = $this->mergeRules($results, $key, $rule); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return $results; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Merge additional rules into a given attribute(s). |
| 104 |
* |
| 105 |
* @param array $results |
| 106 |
* @param string|array $attribute |
| 107 |
* @param string|array $rules |
| 108 |
* |
| 109 |
* @return array |
| 110 |
*/ |
| 111 |
public function mergeRules($results, $attribute, $rules = []) |
| 112 |
{ |
| 113 |
if (is_array($attribute)) { |
| 114 |
foreach ((array) $attribute as $innerAttribute => $innerRules) { |
| 115 |
$results = $this->mergeRulesForAttribute($results, $innerAttribute, $innerRules); |
| 116 |
} |
| 117 |
|
| 118 |
return $results; |
| 119 |
} |
| 120 |
|
| 121 |
return $this->mergeRulesForAttribute( |
| 122 |
$results, $attribute, $rules |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Merge additional rules into a given attribute. |
| 128 |
* |
| 129 |
* @param array $results |
| 130 |
* @param string $attribute |
| 131 |
* @param string|array $rules |
| 132 |
* |
| 133 |
* @return array |
| 134 |
*/ |
| 135 |
protected function mergeRulesForAttribute($results, $attribute, $rules) |
| 136 |
{ |
| 137 |
$array = $this->explodeRules([$rules]); |
| 138 |
|
| 139 |
$merge = reset($array); |
| 140 |
|
| 141 |
$results[$attribute] = array_merge( |
| 142 |
isset($results[$attribute]) ? $this->explodeExplicitRule($results[$attribute]) : [], $merge |
| 143 |
); |
| 144 |
|
| 145 |
return $results; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Extract the rule name and parameters from a rule. |
| 150 |
* |
| 151 |
* @param $rule |
| 152 |
* |
| 153 |
* @return array |
| 154 |
*/ |
| 155 |
public static function parse($rule) |
| 156 |
{ |
| 157 |
$parameters = []; |
| 158 |
|
| 159 |
if (strpos($rule, ':') !== false) { |
| 160 |
list($rule, $parameter) = explode(':', $rule, 2); |
| 161 |
|
| 162 |
$parameters = str_getcsv($parameter); |
| 163 |
} |
| 164 |
|
| 165 |
return [trim($rule), $parameters]; |
| 166 |
} |
| 167 |
} |