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 / Max.php

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

113 lines 2.5 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;
6
7 use Closure;
8 use Give\Vendors\StellarWP\Validation\Config;
9 use Give\Vendors\StellarWP\Validation\Contracts\ValidatesOnFrontEnd;
10 use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule;
11 use Give\Vendors\StellarWP\Validation\Exceptions\ValidationException;
12
13 /**
14 * @since 1.0.0
15 */
16 class Max implements ValidationRule, ValidatesOnFrontEnd
17 {
18 /**
19 * @var int
20 */
21 private $size;
22
23 /**
24 * @since 1.0.0
25 */
26 public function __construct(int $size)
27 {
28 if ($size <= 0) {
29 Config::throwInvalidArgumentException('Max validation rule requires a non-negative value');
30 }
31
32 $this->size = $size;
33 }
34
35 /**
36 * @inheritDoc
37 *
38 * @since 1.0.0
39 */
40 public static function id(): string
41 {
42 return 'max';
43 }
44
45 /**
46 * @inheritDoc
47 *
48 * @since 1.0.0
49 */
50 public static function fromString(?string $options = null): ValidationRule
51 {
52 if (!is_numeric($options)) {
53 Config::throwInvalidArgumentException('Max validation rule requires a numeric value');
54 }
55
56 return new self((int)$options);
57 }
58
59 /**
60 * @inheritDoc
61 *
62 * @since 1.0.0
63 *
64 * @throws ValidationException
65 */
66 public function __invoke($value, Closure $fail, string $key, array $values)
67 {
68 if (is_int($value) || is_float($value)) {
69 if ($value > $this->size) {
70 $fail(sprintf(__('%s must be less than or equal to %d', 'give'), '{field}', $this->size));
71 }
72 } elseif (is_string($value)) {
73 if (mb_strlen($value) > $this->size) {
74 $fail(sprintf(__('%s must be less than or equal to %d characters', 'give'), '{field}', $this->size));
75 }
76 } else {
77 Config::throwValidationException("Field value must be a number or string");
78 }
79 }
80
81 /**
82 * @inheritDoc
83 *
84 * @since 1.0.0
85 */
86 public function serializeOption(): int
87 {
88 return $this->size;
89 }
90
91 /**
92 * @since 1.0.0
93 */
94 public function getSize(): int
95 {
96 return $this->size;
97 }
98
99 /**
100 * @since 1.0.0
101 *
102 * @return void
103 */
104 public function size(int $size)
105 {
106 if ($size <= 0) {
107 Config::throwInvalidArgumentException('Max validation rule requires a non-negative value');
108 }
109
110 $this->size = $size;
111 }
112 }
113