PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.5
Yatra – Travel Booking & Tour Operator Software v3.0.5
3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 2.0.11 All 82 releases
yatra / app / Validators / DestinationValidator.php

DestinationValidator.php in Yatra – Travel Booking & Tour Operator Software 3.0.5, at app/Validators/DestinationValidator.php

290 lines 10.8 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 Yatra\Validators;
6
7 use Yatra\Exceptions\ValidationException;
8
9 /**
10 * Destination Validator
11 *
12 * Comprehensive validation for destination data
13 */
14 class DestinationValidator
15 {
16 /**
17 * Validate destination creation data
18 */
19 public static function validateCreate(array $data): void
20 {
21 $errors = [];
22
23 // Required fields
24 if (empty($data['name'])) {
25 $errors['name'][] = __('Destination name is required', 'yatra');
26 } elseif (strlen($data['name']) < 2) {
27 $errors['name'][] = __('Destination name must be at least 2 characters', 'yatra');
28 }
29
30 if (empty($data['status'])) {
31 $errors['status'][] = __('Status is required', 'yatra');
32 } elseif (!in_array($data['status'], ['draft', 'publish', 'private', 'trash'])) {
33 $errors['status'][] = __('Invalid status value', 'yatra');
34 }
35
36 // Optional field validation
37 if (isset($data['description']) && strlen($data['description']) > 10000) {
38 $errors['description'][] = __('Description cannot exceed 10000 characters', 'yatra');
39 }
40
41 if (isset($data['short_description']) && strlen($data['short_description']) > 500) {
42 $errors['short_description'][] = __('Short description cannot exceed 500 characters', 'yatra');
43 }
44
45 if (isset($data['slug']) && !empty($data['slug'])) {
46 if (!preg_match('/^[\pL\pN-]+$/u', $data['slug'])) {
47 $errors['slug'][] = __('Slug can only contain letters, numbers, and hyphens', 'yatra');
48 }
49 }
50
51 if (isset($data['country_code']) && !empty($data['country_code'])) {
52 if (!preg_match('/^[A-Z]{2}$/', $data['country_code'])) {
53 $errors['country_code'][] = __('Country code must be 2 uppercase letters', 'yatra');
54 }
55 }
56
57 if (isset($data['latitude']) && !empty($data['latitude'])) {
58 if (!is_numeric($data['latitude']) || (float)$data['latitude'] < -90 || (float)$data['latitude'] > 90) {
59 $errors['latitude'][] = __('Latitude must be between -90 and 90', 'yatra');
60 }
61 }
62
63 if (isset($data['longitude']) && !empty($data['longitude'])) {
64 if (!is_numeric($data['longitude']) || (float)$data['longitude'] < -180 || (float)$data['longitude'] > 180) {
65 $errors['longitude'][] = __('Longitude must be between -180 and 180', 'yatra');
66 }
67 }
68
69 if (isset($data['altitude']) && !empty($data['altitude'])) {
70 if (!is_numeric($data['altitude'])) {
71 $errors['altitude'][] = __('Altitude must be a valid number', 'yatra');
72 }
73 }
74
75 if (isset($data['time_zone']) && !empty($data['time_zone'])) {
76 if (!in_array($data['time_zone'], timezone_identifiers_list())) {
77 $errors['time_zone'][] = __('Invalid timezone', 'yatra');
78 }
79 }
80
81 if (isset($data['best_time_to_visit']) && !empty($data['best_time_to_visit'])) {
82 $validMonths = ['january', 'february', 'march', 'april', 'may', 'june',
83 'july', 'august', 'september', 'october', 'november', 'december'];
84 if (is_array($data['best_time_to_visit'])) {
85 foreach ($data['best_time_to_visit'] as $month) {
86 if (!in_array(strtolower($month), $validMonths)) {
87 $errors['best_time_to_visit'][] = __('Invalid month in best time to visit', 'yatra');
88 break;
89 }
90 }
91 }
92 }
93
94 if (!empty($errors)) {
95 throw new ValidationException('Destination validation failed', $errors);
96 }
97 }
98
99 /**
100 * Validate destination update data
101 */
102 public static function validateUpdate(array $data, int $destinationId): void
103 {
104 $errors = [];
105
106 // ID validation
107 if ($destinationId <= 0) {
108 $errors['id'][] = __('Invalid destination ID', 'yatra');
109 }
110
111 // Optional field validation
112 if (isset($data['name'])) {
113 if (empty($data['name'])) {
114 $errors['name'][] = __('Destination name cannot be empty', 'yatra');
115 } elseif (strlen($data['name']) < 2) {
116 $errors['name'][] = __('Destination name must be at least 2 characters', 'yatra');
117 }
118 }
119
120 if (isset($data['status']) && !in_array($data['status'], ['draft', 'publish', 'private', 'trash'])) {
121 $errors['status'][] = __('Invalid status value', 'yatra');
122 }
123
124 if (isset($data['description']) && strlen($data['description']) > 10000) {
125 $errors['description'][] = __('Description cannot exceed 10000 characters', 'yatra');
126 }
127
128 if (isset($data['short_description']) && strlen($data['short_description']) > 500) {
129 $errors['short_description'][] = __('Short description cannot exceed 500 characters', 'yatra');
130 }
131
132 if (isset($data['slug']) && !empty($data['slug']) && !preg_match('/^[\pL\pN-]+$/u', $data['slug'])) {
133 $errors['slug'][] = __('Slug can only contain letters, numbers, and hyphens', 'yatra');
134 }
135
136 if (isset($data['country_code']) && !empty($data['country_code']) && !preg_match('/^[A-Z]{2}$/', $data['country_code'])) {
137 $errors['country_code'][] = __('Country code must be 2 uppercase letters', 'yatra');
138 }
139
140 if (isset($data['latitude']) && !empty($data['latitude'])) {
141 if (!is_numeric($data['latitude']) || (float)$data['latitude'] < -90 || (float)$data['latitude'] > 90) {
142 $errors['latitude'][] = __('Latitude must be between -90 and 90', 'yatra');
143 }
144 }
145
146 if (isset($data['longitude']) && !empty($data['longitude'])) {
147 if (!is_numeric($data['longitude']) || (float)$data['longitude'] < -180 || (float)$data['longitude'] > 180) {
148 $errors['longitude'][] = __('Longitude must be between -180 and 180', 'yatra');
149 }
150 }
151
152 if (isset($data['altitude']) && !empty($data['altitude']) && !is_numeric($data['altitude'])) {
153 $errors['altitude'][] = __('Altitude must be a valid number', 'yatra');
154 }
155
156 if (isset($data['time_zone']) && !empty($data['time_zone']) && !in_array($data['time_zone'], timezone_identifiers_list())) {
157 $errors['time_zone'][] = __('Invalid timezone', 'yatra');
158 }
159
160 if (isset($data['best_time_to_visit']) && !empty($data['best_time_to_visit'])) {
161 $validMonths = ['january', 'february', 'march', 'april', 'may', 'june',
162 'july', 'august', 'september', 'october', 'november', 'december'];
163 if (is_array($data['best_time_to_visit'])) {
164 foreach ($data['best_time_to_visit'] as $month) {
165 if (!in_array(strtolower($month), $validMonths)) {
166 $errors['best_time_to_visit'][] = __('Invalid month in best time to visit', 'yatra');
167 break;
168 }
169 }
170 }
171 }
172
173 if (!empty($errors)) {
174 throw new ValidationException('Destination validation failed', $errors);
175 }
176 }
177
178 /**
179 * Sanitize destination data
180 */
181 public static function sanitize(array $data): array
182 {
183 $sanitized = [];
184
185 // Text fields
186 if (isset($data['name'])) {
187 $sanitized['name'] = sanitize_text_field($data['name']);
188 }
189
190 if (isset($data['slug'])) {
191 $sanitized['slug'] = \Yatra\Helpers\SlugHelper::generate($data['slug']);
192 }
193
194 if (isset($data['description'])) {
195 $sanitized['description'] = wp_kses_post($data['description']);
196 }
197
198 if (isset($data['short_description'])) {
199 $sanitized['short_description'] = wp_kses_post($data['short_description']);
200 }
201
202 if (isset($data['country'])) {
203 $sanitized['country'] = sanitize_text_field($data['country']);
204 }
205
206 if (isset($data['state'])) {
207 $sanitized['state'] = sanitize_text_field($data['state']);
208 }
209
210 if (isset($data['city'])) {
211 $sanitized['city'] = sanitize_text_field($data['city']);
212 }
213
214 if (isset($data['address'])) {
215 $sanitized['address'] = sanitize_textarea_field($data['address']);
216 }
217
218 if (isset($data['postal_code'])) {
219 $sanitized['postal_code'] = sanitize_text_field($data['postal_code']);
220 }
221
222 if (isset($data['time_zone'])) {
223 $sanitized['time_zone'] = sanitize_text_field($data['time_zone']);
224 }
225
226 if (isset($data['currency'])) {
227 $sanitized['currency'] = sanitize_text_field($data['currency']);
228 }
229
230 if (isset($data['language'])) {
231 $sanitized['language'] = sanitize_text_field($data['language']);
232 }
233
234 // Numeric fields
235 if (isset($data['latitude'])) {
236 $sanitized['latitude'] = (float)$data['latitude'];
237 }
238
239 if (isset($data['longitude'])) {
240 $sanitized['longitude'] = (float)$data['longitude'];
241 }
242
243 if (isset($data['altitude'])) {
244 $sanitized['altitude'] = (float)$data['altitude'];
245 }
246
247 if (isset($data['sort_order'])) {
248 $sanitized['sort_order'] = (int)$data['sort_order'];
249 }
250
251 // Enum fields
252 if (isset($data['status'])) {
253 $validStatuses = ['draft', 'publish', 'private', 'trash'];
254 $sanitized['status'] = in_array($data['status'], $validStatuses) ? $data['status'] : 'draft';
255 }
256
257 if (isset($data['country_code'])) {
258 $sanitized['country_code'] = strtoupper(sanitize_text_field($data['country_code']));
259 }
260
261 // Boolean fields
262 if (isset($data['is_featured'])) {
263 $sanitized['is_featured'] = (bool)$data['is_featured'];
264 }
265
266 // JSON/Array fields
267 if (isset($data['best_time_to_visit'])) {
268 if (is_array($data['best_time_to_visit'])) {
269 $sanitized['best_time_to_visit'] = json_encode(array_map('sanitize_text_field', $data['best_time_to_visit']));
270 } else {
271 $sanitized['best_time_to_visit'] = $data['best_time_to_visit'];
272 }
273 }
274
275 if (isset($data['attractions'])) {
276 $sanitized['attractions'] = is_array($data['attractions']) ? json_encode($data['attractions']) : $data['attractions'];
277 }
278
279 if (isset($data['activities'])) {
280 $sanitized['activities'] = is_array($data['activities']) ? json_encode($data['activities']) : $data['activities'];
281 }
282
283 if (isset($data['climate_info'])) {
284 $sanitized['climate_info'] = is_array($data['climate_info']) ? json_encode($data['climate_info']) : $data['climate_info'];
285 }
286
287 return $sanitized;
288 }
289 }
290