fluent-boards
/
vendor
/
wpfluent
/
framework
/
src
/
WPFluent
/
Validator
/
ValidationRuleParser.php
ValidationRuleParser.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.35, at vendor/wpfluent/framework/src/WPFluent/Validator/ValidationRuleParser.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentBoards\Framework\Validator; |
| 4 | |
| 5 | use Closure; |
| 6 | use FluentBoards\Framework\Framework\Support\Arr; |
| 7 | use FluentBoards\Framework\Validator\Rules\Exists; |
| 8 | use FluentBoards\Framework\Validator\Rules\Unique; |
| 9 | use FluentBoards\Framework\Validator\Rules\ConditionalRules; |
| 10 | |
| 11 | class ValidationRuleParser |
| 12 | { |
| 13 | /** |
| 14 | * The data being validated. |
| 15 | * |
| 16 | * @var array |
| 17 | */ |
| 18 | public $data; |
| 19 | |
| 20 | /** |
| 21 | * Create a new validation rule parser. |
| 22 | * |
| 23 | * @param array $data |
| 24 | * |
| 25 | * @return void |
| 26 | */ |
| 27 | public function __construct(array $data) |
| 28 | { |
| 29 | $this->data = $data; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Parse the human-friendly rules into a full rules array for the validator. |
| 34 | * |
| 35 | * @param $rules |
| 36 | * |
| 37 | * @return array |
| 38 | */ |
| 39 | public function explode($rules) |
| 40 | { |
| 41 | return $this->explodeRules($rules); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Explode the rules into an array of explicit rules. |
| 46 | * |
| 47 | * @param array $rules |
| 48 | * |
| 49 | * @return array |
| 50 | */ |
| 51 | protected function explodeRules($rules) |
| 52 | { |
| 53 | foreach ($rules as $attribute => $rule) { |
| 54 | if (mb_strpos($attribute, '*') !== false) { |
| 55 | $rules = $this->explodeWildcardRules($rules, $attribute, [$rule]); |
| 56 | |
| 57 | unset($rules[$attribute]); |
| 58 | } else { |
| 59 | $rules[$attribute] = $this->explodeExplicitRule($rule); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return $rules; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Explode the explicit rule into an array if necessary. |
| 68 | * |
| 69 | * @param mixed $rule |
| 70 | * |
| 71 | * @return array |
| 72 | */ |
| 73 | protected function explodeExplicitRule($rule) |
| 74 | { |
| 75 | $rules = $conditionals = []; |
| 76 | |
| 77 | $ruleArray = is_array($rule) ? $rule : [$rule]; |
| 78 | |
| 79 | foreach ($ruleArray as $key => $rule) { |
| 80 | |
| 81 | if ($rule instanceof ConditionalRules) { |
| 82 | $conditionals = $this->parseConditionalRules($rule); |
| 83 | } elseif($rule instanceof Closure) { |
| 84 | $rules[$key] = $rule; |
| 85 | } else { |
| 86 | if ( |
| 87 | ($rule instanceof Exists && $rule->queryCallbacks()) || |
| 88 | ($rule instanceof Unique && $rule->queryCallbacks()) |
| 89 | ) { |
| 90 | $rules[] = $rule; |
| 91 | } else { |
| 92 | $rules[] = (string) $rule; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | $rules = array_merge($rules, $conditionals); |
| 98 | |
| 99 | // Now, we'll check if there is any string rule |
| 100 | // given inside the array using the | sign, i.e: |
| 101 | // required|alpha, if any, we'll convert it to array. |
| 102 | |
| 103 | $result = []; |
| 104 | |
| 105 | foreach ($rules as $key => $value) { |
| 106 | if (is_string($value)) { |
| 107 | $result = array_merge($result, explode('|', $value)); |
| 108 | } else { |
| 109 | $result[$key] = $value; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return $result; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Parse conditional rules. |
| 118 | * |
| 119 | * @param \FluentBoards\Framework\Validator\Rules\ConditionalRules $rule |
| 120 | * @return array |
| 121 | */ |
| 122 | protected function parseConditionalRules($rule) |
| 123 | { |
| 124 | $rules = []; |
| 125 | |
| 126 | $conditionals = $rule->passes($this->data) |
| 127 | ? array_filter($rule->rules()) |
| 128 | : array_filter($rule->defaultRules()); |
| 129 | |
| 130 | if ($conditionals) { |
| 131 | foreach ($conditionals as $conditional) { |
| 132 | if (is_object($conditional) && method_exists($conditional, '__toString')) { |
| 133 | $rules[] = (string) $conditional; |
| 134 | } elseif (is_string($conditional)) { |
| 135 | $rules[] = $conditional; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return $rules; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Define a set of rules that apply to each element in an array attribute. |
| 145 | * |
| 146 | * @param array $results |
| 147 | * @param string $attribute |
| 148 | * @param string|array $rules |
| 149 | * |
| 150 | * @return array |
| 151 | */ |
| 152 | protected function explodeWildcardRules($results, $attribute, $rules) |
| 153 | { |
| 154 | $pattern = str_replace('\*', '[^\.]*', preg_quote($attribute)); |
| 155 | |
| 156 | $data = ValidationData::initializeAndGatherData($attribute, $this->data); |
| 157 | |
| 158 | foreach ($data as $key => $value) { |
| 159 | if (substr($key, 0, strlen($attribute)) === $attribute || (bool) preg_match('/^'.$pattern.'\z/', $key)) { |
| 160 | foreach ((array) $rules as $rule) { |
| 161 | $results = $this->mergeRules($results, $key, $rule, $attribute); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return $results; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Merge additional rules into a given attribute(s). |
| 171 | * |
| 172 | * @param array $results |
| 173 | * @param string|array $attribute |
| 174 | * @param string|array $rules |
| 175 | * @param string|null $ $originalRuleKey |
| 176 | * |
| 177 | * @return array |
| 178 | */ |
| 179 | public function mergeRules($results, $attribute, $rules = [], $originalRuleKey = null) |
| 180 | { |
| 181 | if (is_array($attribute)) { |
| 182 | foreach ((array) $attribute as $innerAttribute => $innerRules) { |
| 183 | $results = $this->mergeRulesForAttribute($results, $innerAttribute, $innerRules, $originalRuleKey); |
| 184 | } |
| 185 | |
| 186 | return $results; |
| 187 | } |
| 188 | |
| 189 | return $this->mergeRulesForAttribute( |
| 190 | $results, $attribute, $rules , $originalRuleKey |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Merge additional rules into a given attribute. |
| 196 | * |
| 197 | * @param array $results |
| 198 | * @param string $attribute |
| 199 | * @param string|array $rules |
| 200 | * @param string|null $ $originalRuleKey |
| 201 | * |
| 202 | * @return array |
| 203 | */ |
| 204 | protected function mergeRulesForAttribute($results, $attribute, $rules, $originalRuleKey = null) |
| 205 | { |
| 206 | $array = $this->explodeRules([$rules]); |
| 207 | |
| 208 | $merge = reset($array); |
| 209 | |
| 210 | if(!empty($originalRuleKey)){ |
| 211 | $merge['rule_key'] = $originalRuleKey; |
| 212 | } |
| 213 | |
| 214 | $results[$attribute] = array_merge( |
| 215 | isset($results[$attribute]) ? |
| 216 | $this->explodeExplicitRule($results[$attribute]) |
| 217 | : [], $merge |
| 218 | ); |
| 219 | |
| 220 | return $results; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Extract the rule name and parameters from a rule. |
| 225 | * |
| 226 | * @param $rule |
| 227 | * |
| 228 | * @return array |
| 229 | */ |
| 230 | public static function parse($rule) |
| 231 | { |
| 232 | if ($rule instanceof Closure) { |
| 233 | return [$rule, []]; |
| 234 | } |
| 235 | |
| 236 | $parameters = []; |
| 237 | |
| 238 | if (strpos($rule, ':') !== false) { |
| 239 | list($rule, $parameter) = explode(':', $rule, 2); |
| 240 | |
| 241 | $parameters = str_getcsv($parameter); |
| 242 | } |
| 243 | |
| 244 | return [trim($rule), $parameters]; |
| 245 | } |
| 246 | } |
| 247 |