PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Http / Rules / WhenFilledRule.php

WhenFilledRule.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Http/Rules/WhenFilledRule.php

99 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Http\Rules;
4
5 use Closure;
6
7 /**
8 * Value rules that apply only once a value is actually supplied.
9 *
10 * These exist because `nullable` cannot be used alongside a conditional
11 * requirement: Validator::filterExcludeables() drops EVERY rule on an attribute
12 * — closures included — the moment `nullable` is present and the value is falsy,
13 * which silently disarms the requirement sitting beside it. An attribute that
14 * must both be demanded under a condition and be shape-checked when present
15 * therefore has to express both halves as closures, with no `nullable` on it.
16 *
17 * Pair with RequiredWhenRule: that one decides whether an empty value is
18 * allowed, these decide whether a supplied value is acceptable. Neither fires on
19 * the other's territory, so "" and null pass through here untouched and are the
20 * requirement's business alone.
21 *
22 * See dev-docs/framework-update-2.12.6-qa.md finding 9.
23 */
24 class WhenFilledRule
25 {
26 /**
27 * Constrain a supplied value to a fixed set. Replaces `in:` on an attribute
28 * that cannot carry `nullable`.
29 *
30 * @param array $allowed
31 * @param string $message Already translated.
32 * @return \Closure
33 */
34 public static function in(array $allowed, $message): Closure
35 {
36 return function ($attribute, $value) use ($allowed, $message) {
37 if (!RequiredWhenRule::isFilled($value)) {
38 return null;
39 }
40
41 return in_array((string) $value, $allowed, true) ? null : $message;
42 };
43 }
44
45 /**
46 * Require a supplied value to be numeric and no smaller than $minimum.
47 * Replaces `numeric|min:` on an attribute that cannot carry `nullable`.
48 *
49 * @param int|float $minimum
50 * @param string $notNumericMessage Already translated.
51 * @param string $belowMinimumMessage Already translated.
52 * @return \Closure
53 */
54 public static function numericAtLeast($minimum, $notNumericMessage, $belowMinimumMessage): Closure
55 {
56 return function ($attribute, $value) use ($minimum, $notNumericMessage, $belowMinimumMessage) {
57 if (!RequiredWhenRule::isFilled($value)) {
58 return null;
59 }
60
61 if (!is_numeric($value)) {
62 return $notNumericMessage;
63 }
64
65 return $value + 0 < $minimum ? $belowMinimumMessage : null;
66 };
67 }
68
69 /**
70 * Require a supplied value to be plain text within $maxLength characters.
71 * Replaces `sanitizeText|maxLength:` on an attribute that cannot carry
72 * `nullable` — and, unlike those, is never handed a null to trim().
73 *
74 * @param int $maxLength
75 * @param string $message Already translated.
76 * @return \Closure
77 */
78 public static function text($maxLength, $message): Closure
79 {
80 return function ($attribute, $value) use ($maxLength, $message) {
81 if (!RequiredWhenRule::isFilled($value)) {
82 return null;
83 }
84
85 if (!is_scalar($value)) {
86 return $message;
87 }
88
89 $value = trim((string) $value);
90
91 if (sanitize_text_field($value) !== $value || strlen($value) > $maxLength) {
92 return $message;
93 }
94
95 return null;
96 };
97 }
98 }
99