PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / vendor / vendor-prefixed / stellarwp / validation / src / Rules / Abstracts / ConditionalRule.php

ConditionalRule.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at vendor/vendor-prefixed/stellarwp/validation/src/Rules/Abstracts/ConditionalRule.php

75 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Give\Vendors\StellarWP\Validation\Rules\Abstracts;
6
7 use Give\Vendors\StellarWP\FieldConditions\ComplexConditionSet;
8 use Give\Vendors\StellarWP\FieldConditions\SimpleConditionSet;
9 use Give\Vendors\StellarWP\FieldConditions\Contracts\Condition;
10 use Give\Vendors\StellarWP\FieldConditions\Contracts\ConditionSet;
11 use Give\Vendors\StellarWP\Validation\Config;
12 use Give\Vendors\StellarWP\Validation\Contracts\ValidatesOnFrontEnd;
13 use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule;
14
15 abstract class ConditionalRule implements ValidationRule, ValidatesOnFrontEnd
16 {
17 /**
18 * @var SimpleConditionSet|ComplexConditionSet
19 */
20 protected ConditionSet $conditions;
21
22 /**
23 * @param SimpleConditionSet|ComplexConditionSet|Condition[] $conditions
24 */
25 public function __construct($conditions)
26 {
27 if ($conditions instanceof ConditionSet) {
28 $this->conditions = $conditions;
29 } else {
30 $this->conditions = new ComplexConditionSet(...$conditions);
31 }
32 }
33
34 /**
35 * Supports a simple syntax for defining conditions. Example:
36 * - ruleId:field1,value1;field2,value2
37 *
38 * Each rule is assumed to be a basic condition with an equals operator.
39 *
40 * @since 1.2.0
41 */
42 public static function fromString(?string $options = null): ValidationRule
43 {
44 if (empty($options)) {
45 Config::throwInvalidArgumentException(static::class . ' rule requires at least one condition');
46 }
47
48 $rules = explode(';', $options);
49
50 $conditionSet = new SimpleConditionSet();
51 foreach ($rules as $rule) {
52 $rule = explode(',', $rule);
53
54 if (count($rule) !== 2) {
55 Config::throwInvalidArgumentException(static::class . ' rule requires one field name and one value');
56 }
57
58 $conditionSet->and($rule[0], '=', $rule[1]);
59 }
60
61 // @phpstan-ignore-next-line
62 return new static($conditionSet);
63 }
64
65 /**
66 * {@inheritdoc}
67 *
68 * @since 1.2.0
69 */
70 public function serializeOption()
71 {
72 return $this->conditions->jsonSerialize();
73 }
74 }
75