PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.4
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.4
6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / Validation / Rules / DateRule.php
kirki / libraries / framework / Validation / Rules Last commit date
ArrayRule.php 1 month ago BooleanRule.php 1 month ago ConditionalRule.php 1 month ago DateRule.php 1 month ago ExistsRule.php 1 month ago FileRule.php 1 month ago InRule.php 1 month ago NotInRule.php 1 month ago NullableRule.php 1 month ago NumericRule.php 1 month ago ObjectRule.php 1 month ago ProhibitedIfRule.php 1 month ago ProhibitedRule.php 1 month ago ProhibitedUnlessRule.php 1 month ago RequiredIfRule.php 1 month ago RequiredRule.php 1 month ago RequiredUnlessRule.php 1 month ago SameAsRule.php 1 month ago SometimesRule.php 1 month ago StringRule.php 1 month ago UniqueRule.php 1 month ago
DateRule.php
166 lines
1 <?php
2
3 /**
4 * Date rule class.
5 *
6 * @package Framework
7 * @subpackage Validation
8 * @since 1.0.0
9 */
10 namespace Kirki\Framework\Validation\Rules;
11
12 use Kirki\Framework\Constants\DateTimeFormats;
13 use Kirki\Framework\Exceptions\InvalidDateFormatException;
14 use Kirki\Framework\Supports\Facades\Date;
15 use Kirki\Framework\Validation\ValidationRule;
16 use function Kirki\Framework\deep_get;
17 \defined('ABSPATH') || exit;
18 /**
19 * Validates that the given value is a valid date.
20 *
21 * @method $this after(string $reference)
22 * @method $this format(string $format)
23 */
24 class DateRule extends ValidationRule
25 {
26 /**
27 * The rule name.
28 *
29 * @var string
30 *
31 * @since 1.0.0
32 */
33 protected string $rule = 'date';
34 /**
35 * The supported constraints.
36 *
37 * @var array
38 *
39 * @since 1.0.0
40 */
41 protected array $constraints = ['after', 'format', 'datetime'];
42 /**
43 * The date format.
44 *
45 * @var string
46 *
47 * @since 1.0.0
48 */
49 public const DATE_FORMAT = DateTimeFormats::DB_DATE;
50 /**
51 * The datetime format.
52 *
53 * @var string
54 *
55 * @since 1.0.0
56 */
57 public const DATETIME_FORMAT = DateTimeFormats::DB_DATETIME;
58 /**
59 * Validate the rule.
60 *
61 * @return bool
62 *
63 * @since 1.0.0
64 */
65 public function validate() : bool
66 {
67 if (!Date::is_valid_date($this->value)) {
68 return $this->fails($this->default_messages['default']);
69 }
70 return $this->validate_constraints(function ($passed, $constraint) {
71 if (!$passed) {
72 $this->fails($this->default_messages[$constraint], [$constraint => (string) $this->get($constraint)]);
73 }
74 });
75 }
76 /**
77 * Validate the after constraint.
78 *
79 * @param string $value The value to validate.
80 *
81 * @return bool
82 *
83 * @since 1.0.0
84 */
85 protected function validate_after($value)
86 {
87 $reference = $this->resolve_reference_date($this->get('after'));
88 if (!Date::is_valid_date($reference)) {
89 return \false;
90 }
91 return Date::parse($value)->is_after(Date::parse($reference));
92 }
93 /**
94 * Validate the format constraint.
95 *
96 * @param string $value The value to validate.
97 *
98 * @return bool
99 *
100 * @since 1.0.0
101 */
102 protected function validate_format($value)
103 {
104 $format = (string) $this->get('format');
105 if (!\is_string($value)) {
106 return \false;
107 }
108 try {
109 $date = Date::create_from_format($format, $value);
110 } catch (InvalidDateFormatException $exception) {
111 return \false;
112 }
113 return $date->format($format) === $value;
114 }
115 /**
116 * Validate the datetime constraint.
117 *
118 * @param string $value The value to validate.
119 *
120 * @return bool
121 *
122 * @since 1.0.0
123 */
124 protected function validate_datetime($value)
125 {
126 $format = $this->get('datetime');
127 if ($format === \true) {
128 $format = static::DATETIME_FORMAT;
129 }
130 try {
131 $date = Date::create_from_format($format, $value);
132 } catch (InvalidDateFormatException $exception) {
133 return \false;
134 }
135 return $date->format($format) === $value;
136 }
137 /**
138 * Resolve the after argument as another data field first, falling back to a literal date.
139 *
140 * @param string $reference The reference field name or literal date.
141 *
142 * @return mixed
143 *
144 * @since 1.0.0
145 */
146 protected function resolve_reference_date($reference)
147 {
148 $field_value = deep_get($this->data, (string) $reference);
149 if ($field_value !== null) {
150 return $field_value;
151 }
152 return $reference;
153 }
154 /**
155 * Get the error messages.
156 *
157 * @return array
158 *
159 * @since 1.0.0
160 */
161 public function messages()
162 {
163 return $this->process_messages($this->messages);
164 }
165 }
166