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 / Requests / CustomerRequest.php

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

109 lines 3.5 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\Requests;
4
5 use FluentCart\App\App;
6 use FluentCart\App\Models\Customer;
7 use FluentCart\App\Services\Renderer\CheckoutFieldsSchema;
8 use FluentCart\Framework\Foundation\RequestGuard;
9 use FluentCart\Framework\Support\Arr;
10
11 class CustomerRequest extends RequestGuard
12 {
13
14 public function beforeValidation()
15 {
16 $data = $this->all();
17 $data['notes'] = Arr::get($data, 'notes', '');
18 return $data;
19 }
20
21 /**
22 * @return array
23 */
24 public function rules()
25 {
26 $validationRules = [];
27
28 $customerId = intval(Arr::get($this->all(), 'id', 0));
29
30 if (CheckoutFieldsSchema::isFullNameRequired()) {
31 $validationRules['full_name'] = 'required|sanitizeText|maxLength:255';
32 } else {
33 $validationRules['first_name'] = 'required|sanitizeText|maxLength:255';
34 $validationRules['last_name'] = 'nullable|sanitizeText|maxLength:255';
35 }
36
37 return array_merge($validationRules, [
38 'city' => 'nullable|sanitizeText',
39 'email' => ['required', 'sanitizeText', 'email', 'maxLength:255', 'exist' =>function ($attribute, $value) use ($customerId) {
40
41 if (empty($value)) {
42 return null;
43 }
44 $value = sanitize_email($value);
45 $customer = Customer::query()
46 ->when($customerId, function ($query) use ($customerId) {
47 $query->where('id', '!=', $customerId);
48 })
49 ->where('email', $value)->first();
50 if ($customer) {
51 return __('Email already exists.', 'fluent-cart');
52 }
53 return null;
54 }],
55 ]);
56 }
57
58 /**
59 * @return array
60 */
61 public function messages()
62 {
63 return [
64 'email.required' => esc_html__('Email is required.', 'fluent-cart'),
65 'email.email' => esc_html__('Email must be a valid email address.', 'fluent-cart'),
66 'full_name.required' => esc_html__('Full Name is required.', 'fluent-cart'),
67 'first_name.required' => esc_html__('First Name is required.', 'fluent-cart'),
68 ];
69 }
70
71 /**
72 * @return array
73 */
74 public function sanitize()
75 {
76 return [
77 'user_id' => 'intval',
78 'email' => function ($value) {
79 if (empty($value)) {
80 return '';
81 }
82
83 return sanitize_email($value);
84 },
85 'full_name' => 'sanitize_text_field',
86 'first_name' => 'sanitize_text_field',
87 'last_name' => 'sanitize_text_field',
88 'status' => 'sanitize_text_field',
89 'aov' => 'sanitize_text_field',
90 'notes' => 'sanitize_text_field',
91 'country' => 'sanitize_text_field',
92 'city' => 'sanitize_text_field',
93 'state' => 'sanitize_text_field',
94 'postcode' => 'sanitize_text_field',
95 'username' => 'sanitize_text_field',
96 'user_nicename' => 'sanitize_text_field',
97 'display_name' => 'sanitize_text_field',
98 'user_url' => function ($value) {
99 if (empty($value)) {
100 return '';
101 }
102
103 return sanitize_url($value);
104 },
105 'wp_user' => 'sanitize_text_field',
106 ];
107 }
108 }
109