PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.13
Yatra – Travel Booking & Tour Operator Software v3.0.13
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 / CustomerValidator.php

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

249 lines 8.7 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 * Customer Validator
11 *
12 * Comprehensive validation for customer data
13 */
14 class CustomerValidator
15 {
16 /**
17 * Validate customer creation data
18 */
19 public static function validateCreate(array $data): void
20 {
21 $errors = [];
22
23 // Required fields
24 if (empty($data['first_name'])) {
25 $errors['first_name'][] = __('First name is required', 'yatra');
26 } elseif (strlen($data['first_name']) < 2) {
27 $errors['first_name'][] = __('First name must be at least 2 characters', 'yatra');
28 }
29
30 if (empty($data['last_name'])) {
31 $errors['last_name'][] = __('Last name is required', 'yatra');
32 } elseif (strlen($data['last_name']) < 2) {
33 $errors['last_name'][] = __('Last name must be at least 2 characters', 'yatra');
34 }
35
36 if (empty($data['email'])) {
37 $errors['email'][] = __('Email is required', 'yatra');
38 } elseif (!is_email($data['email'])) {
39 $errors['email'][] = __('Invalid email format', 'yatra');
40 }
41
42 // Optional field validation
43 if (isset($data['phone']) && !empty($data['phone'])) {
44 if (!preg_match('/^[\+]?[0-9\s\-\(\)]{7,20}$/', $data['phone'])) {
45 $errors['phone'][] = __('Invalid phone number format', 'yatra');
46 }
47 }
48
49 if (isset($data['date_of_birth']) && !empty($data['date_of_birth'])) {
50 if (!self::isValidDate($data['date_of_birth'])) {
51 $errors['date_of_birth'][] = __('Invalid date of birth format', 'yatra');
52 } elseif (strtotime($data['date_of_birth']) > strtotime('-18 years')) {
53 $errors['date_of_birth'][] = __('Customer must be at least 18 years old', 'yatra');
54 }
55 }
56
57 if (isset($data['gender']) && !empty($data['gender'])) {
58 $validGenders = ['male', 'female', 'other', 'prefer_not_to_say'];
59 if (!in_array($data['gender'], $validGenders)) {
60 $errors['gender'][] = __('Invalid gender value', 'yatra');
61 }
62 }
63
64 if (isset($data['country_code']) && !empty($data['country_code'])) {
65 if (!preg_match('/^[A-Z]{2}$/', $data['country_code'])) {
66 $errors['country_code'][] = __('Country code must be 2 uppercase letters', 'yatra');
67 }
68 }
69
70 if (isset($data['emergency_contact_phone']) && !empty($data['emergency_contact_phone'])) {
71 if (!preg_match('/^[\+]?[0-9\s\-\(\)]{7,20}$/', $data['emergency_contact_phone'])) {
72 $errors['emergency_contact_phone'][] = __('Invalid emergency contact phone format', 'yatra');
73 }
74 }
75
76 if (!empty($errors)) {
77 throw new ValidationException('Customer validation failed', $errors);
78 }
79 }
80
81 /**
82 * Validate customer update data
83 */
84 public static function validateUpdate(array $data, int $customerId): void
85 {
86 $errors = [];
87
88 // ID validation
89 if ($customerId <= 0) {
90 $errors['id'][] = __('Invalid customer ID', 'yatra');
91 }
92
93 // Optional field validation
94 if (isset($data['first_name'])) {
95 if (empty($data['first_name'])) {
96 $errors['first_name'][] = __('First name cannot be empty', 'yatra');
97 } elseif (strlen($data['first_name']) < 2) {
98 $errors['first_name'][] = __('First name must be at least 2 characters', 'yatra');
99 }
100 }
101
102 if (isset($data['last_name'])) {
103 if (empty($data['last_name'])) {
104 $errors['last_name'][] = __('Last name cannot be empty', 'yatra');
105 } elseif (strlen($data['last_name']) < 2) {
106 $errors['last_name'][] = __('Last name must be at least 2 characters', 'yatra');
107 }
108 }
109
110 if (isset($data['email'])) {
111 if (empty($data['email'])) {
112 $errors['email'][] = __('Email cannot be empty', 'yatra');
113 } elseif (!is_email($data['email'])) {
114 $errors['email'][] = __('Invalid email format', 'yatra');
115 }
116 }
117
118 if (isset($data['phone']) && !empty($data['phone'])) {
119 if (!preg_match('/^[\+]?[0-9\s\-\(\)]{7,20}$/', $data['phone'])) {
120 $errors['phone'][] = __('Invalid phone number format', 'yatra');
121 }
122 }
123
124 if (isset($data['date_of_birth']) && !empty($data['date_of_birth'])) {
125 if (!self::isValidDate($data['date_of_birth'])) {
126 $errors['date_of_birth'][] = __('Invalid date of birth format', 'yatra');
127 } elseif (strtotime($data['date_of_birth']) > strtotime('-18 years')) {
128 $errors['date_of_birth'][] = __('Customer must be at least 18 years old', 'yatra');
129 }
130 }
131
132 if (isset($data['gender']) && !empty($data['gender'])) {
133 $validGenders = ['male', 'female', 'other', 'prefer_not_to_say'];
134 if (!in_array($data['gender'], $validGenders)) {
135 $errors['gender'][] = __('Invalid gender value', 'yatra');
136 }
137 }
138
139 if (isset($data['country_code']) && !empty($data['country_code'])) {
140 if (!preg_match('/^[A-Z]{2}$/', $data['country_code'])) {
141 $errors['country_code'][] = __('Country code must be 2 uppercase letters', 'yatra');
142 }
143 }
144
145 if (isset($data['emergency_contact_phone']) && !empty($data['emergency_contact_phone'])) {
146 if (!preg_match('/^[\+]?[0-9\s\-\(\)]{7,20}$/', $data['emergency_contact_phone'])) {
147 $errors['emergency_contact_phone'][] = __('Invalid emergency contact phone format', 'yatra');
148 }
149 }
150
151 if (!empty($errors)) {
152 throw new ValidationException('Customer validation failed', $errors);
153 }
154 }
155
156 /**
157 * Sanitize customer data
158 */
159 public static function sanitize(array $data): array
160 {
161 $sanitized = [];
162
163 // Text fields
164 if (isset($data['first_name'])) {
165 $sanitized['first_name'] = sanitize_text_field($data['first_name']);
166 }
167
168 if (isset($data['last_name'])) {
169 $sanitized['last_name'] = sanitize_text_field($data['last_name']);
170 }
171
172 if (isset($data['email'])) {
173 $sanitized['email'] = sanitize_email($data['email']);
174 }
175
176 if (isset($data['phone'])) {
177 $sanitized['phone'] = sanitize_text_field($data['phone']);
178 }
179
180 if (isset($data['address'])) {
181 $sanitized['address'] = sanitize_textarea_field($data['address']);
182 }
183
184 if (isset($data['city'])) {
185 $sanitized['city'] = sanitize_text_field($data['city']);
186 }
187
188 if (isset($data['state'])) {
189 $sanitized['state'] = sanitize_text_field($data['state']);
190 }
191
192 if (isset($data['country'])) {
193 $sanitized['country'] = sanitize_text_field($data['country']);
194 }
195
196 if (isset($data['postal_code'])) {
197 $sanitized['postal_code'] = sanitize_text_field($data['postal_code']);
198 }
199
200 if (isset($data['emergency_contact_name'])) {
201 $sanitized['emergency_contact_name'] = sanitize_text_field($data['emergency_contact_name']);
202 }
203
204 if (isset($data['emergency_contact_phone'])) {
205 $sanitized['emergency_contact_phone'] = sanitize_text_field($data['emergency_contact_phone']);
206 }
207
208 if (isset($data['notes'])) {
209 $sanitized['notes'] = wp_kses_post($data['notes']);
210 }
211
212 // Date fields
213 if (isset($data['date_of_birth'])) {
214 $sanitized['date_of_birth'] = sanitize_text_field($data['date_of_birth']);
215 }
216
217 // Enum fields
218 if (isset($data['gender'])) {
219 $validGenders = ['male', 'female', 'other', 'prefer_not_to_say'];
220 $sanitized['gender'] = in_array($data['gender'], $validGenders) ? $data['gender'] : null;
221 }
222
223 if (isset($data['country_code'])) {
224 $sanitized['country_code'] = strtoupper(sanitize_text_field($data['country_code']));
225 }
226
227 if (isset($data['status'])) {
228 $validStatuses = ['active', 'inactive', 'blocked'];
229 $sanitized['status'] = in_array($data['status'], $validStatuses) ? $data['status'] : 'active';
230 }
231
232 // Boolean fields
233 if (isset($data['newsletter_subscribed'])) {
234 $sanitized['newsletter_subscribed'] = (bool)$data['newsletter_subscribed'];
235 }
236
237 return $sanitized;
238 }
239
240 /**
241 * Check if date is valid
242 */
243 private static function isValidDate(string $date): bool
244 {
245 $d = \DateTime::createFromFormat('Y-m-d', $date);
246 return $d && $d->format('Y-m-d') === $date;
247 }
248 }
249