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 / src / DonationForms / Rules / Size.php

Size.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/DonationForms/Rules/Size.php

120 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Give\DonationForms\Rules;
3
4
5 use Closure;
6 use Give\Vendors\StellarWP\Validation\Config;
7 use Give\Vendors\StellarWP\Validation\Contracts\ValidatesOnFrontEnd;
8 use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule;
9
10 use function is_numeric;
11
12 class Size implements ValidationRule, ValidatesOnFrontEnd
13 {
14 /**
15 * @var numeric
16 */
17 protected $size;
18
19 /**
20 * @inheritDoc
21 */
22 public static function id(): string
23 {
24 return 'size';
25 }
26
27 /**
28 * @since 3.0.0
29 */
30 public function __construct($size)
31 {
32 if ($size <= 0) {
33 Config::throwInvalidArgumentException('Size validation rule requires a non-negative value');
34 }
35
36 $this->size = $this->sanitize($size);
37 }
38
39 public function sanitize($value)
40 {
41 if (is_numeric($value)) {
42 if (strpos($value, '.') !== false) {
43 return (float)$value;
44 }
45
46 return (int)$value;
47 }
48
49 return $value;
50 }
51
52 /**
53 * @since 4.17.0 Declare the nullable parameter explicitly.
54 * @since 3.0.0
55 */
56 public static function fromString(?string $options = null): ValidationRule
57 {
58 if (!is_numeric($options)) {
59 Config::throwInvalidArgumentException('Size validation rule requires a numeric value');
60 }
61
62 return new self($options);
63 }
64
65 /**
66 * @since 3.0.0
67 */
68 public function __invoke($value, Closure $fail, string $key, array $values)
69 {
70 $value = $this->sanitize($value);
71
72 if (is_numeric($value)) {
73 if ($value !== $this->getSize()) {
74 $fail(sprintf(__('%s must be exactly %s', 'give'), '{field}', $this->getSize()));
75 }
76 } elseif (is_string($value)) {
77 if (mb_strlen($value) !== $this->getSize()) {
78 $fail(sprintf(__('%s must be exactly %d characters', 'give'), '{field}', $this->getSize()));
79 }
80 } else {
81 Config::throwValidationException("Field value must be a number or string");
82 }
83 }
84
85 /**
86 * @since 3.0.0
87 *
88 * @return numeric
89 */
90 public function serializeOption()
91 {
92 return $this->size;
93 }
94
95 /**
96 * @since 3.0.0
97 *
98 * @return numeric
99 */
100 public function getSize()
101 {
102 return $this->size;
103 }
104
105 /**
106 * @since 3.0.0
107 *
108 * @param numeric $size
109 *
110 * @return void
111 */
112 public function size($size)
113 {
114 if ($size <= 0) {
115 Config::throwInvalidArgumentException('Size validation rule requires a non-negative value');
116 }
117
118 $this->size = $size;
119 }
120 }