AllowMultiple.php
4 years ago
HasDefaultValue.php
4 years ago
HasEmailTag.php
4 years ago
HasHelpText.php
4 years ago
HasLabel.php
4 years ago
HasMaxLength.php
4 years ago
HasMinLength.php
4 years ago
HasName.php
4 years ago
HasNodes.php
4 years ago
HasOptions.php
4 years ago
HasPlaceholder.php
4 years ago
HasType.php
4 years ago
HasVisibilityConditions.php
4 years ago
InsertNode.php
4 years ago
IsReadOnly.php
4 years ago
IsRequired.php
4 years ago
Macroable.php
4 years ago
MoveNode.php
4 years ago
MoveNodeProxy.php
4 years ago
NameCollision.php
4 years ago
RemoveNode.php
4 years ago
SerializeAsJson.php
4 years ago
ShowInReceipt.php
4 years ago
StoreAsMeta.php
4 years ago
ValidationRules.php
4 years ago
WalkNodes.php
4 years ago
ValidationRules.php
97 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\FieldsAPI\Concerns; |
| 4 | |
| 5 | use JsonSerializable; |
| 6 | |
| 7 | /** |
| 8 | * @since 2.12.0 |
| 9 | */ |
| 10 | class ValidationRules implements JsonSerializable |
| 11 | { |
| 12 | |
| 13 | /** @var array */ |
| 14 | protected $rules; |
| 15 | |
| 16 | /** |
| 17 | * ValidationRules constructor. |
| 18 | * |
| 19 | * @param array $rules |
| 20 | */ |
| 21 | public function __construct($rules = []) |
| 22 | { |
| 23 | $this->rules = $rules; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Set a rule. |
| 28 | * |
| 29 | * @since 2.12.0 |
| 30 | * |
| 31 | * @param string $rule |
| 32 | * @param mixed $value |
| 33 | * |
| 34 | * @return $this |
| 35 | */ |
| 36 | public function rule($rule, $value) |
| 37 | { |
| 38 | $this->rules[$rule] = $value; |
| 39 | |
| 40 | return $this; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get a rule. |
| 45 | * |
| 46 | * @since 2.12.0 |
| 47 | * |
| 48 | * @param string $rule |
| 49 | * |
| 50 | * @return mixed |
| 51 | */ |
| 52 | public function getRule($rule) |
| 53 | { |
| 54 | return array_key_exists($rule, $this->rules) |
| 55 | ? $this->rules[$rule] |
| 56 | : null; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Forget a rule. |
| 61 | * |
| 62 | * @since 2.12.0 |
| 63 | * |
| 64 | * @param string $rule |
| 65 | * |
| 66 | * @return $this |
| 67 | */ |
| 68 | public function forgetRule($rule) |
| 69 | { |
| 70 | if (array_key_exists($rule, $this->rules)) { |
| 71 | unset($this->rules[$rule]); |
| 72 | } |
| 73 | |
| 74 | return $this; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get all the rules. |
| 79 | * |
| 80 | * @since 2.12.0 |
| 81 | * |
| 82 | * @return array |
| 83 | */ |
| 84 | public function all() |
| 85 | { |
| 86 | return $this->rules; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * {@inheritdoc}} |
| 91 | */ |
| 92 | public function jsonSerialize() |
| 93 | { |
| 94 | return (object)$this->all(); |
| 95 | } |
| 96 | } |
| 97 |