AuthenticationData.php
2 years ago
DonateControllerData.php
2 years ago
DonateFormRouteData.php
2 years ago
DonateRouteData.php
2 years ago
DonationConfirmationReceiptViewRouteData.php
2 years ago
DonationFormGoalData.php
2 years ago
DonationFormPreviewRouteData.php
2 years ago
DonationFormQueryData.php
2 years ago
DonationFormViewRouteData.php
2 years ago
LegacyPurchaseFormData.php
2 years ago
UserData.php
2 years ago
ValidationRouteData.php
2 years ago
ValidationRouteData.php
111 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\DataTransferObjects; |
| 4 | |
| 5 | use Give\DonationForms\Exceptions\DonationFormFieldErrorsException; |
| 6 | use Give\DonationForms\Models\DonationForm; |
| 7 | use Give\Framework\FieldsAPI\Actions\CreateValidatorFromFormFields; |
| 8 | use Give\Framework\Http\Response\Types\JsonResponse; |
| 9 | use Give\Framework\Support\Contracts\Arrayable; |
| 10 | use WP_Error; |
| 11 | |
| 12 | /** |
| 13 | * @since 3.0.0 |
| 14 | */ |
| 15 | class ValidationRouteData implements Arrayable |
| 16 | { |
| 17 | /** |
| 18 | * @var array |
| 19 | */ |
| 20 | private $requestData; |
| 21 | /** |
| 22 | * @var int |
| 23 | */ |
| 24 | public $formId; |
| 25 | |
| 26 | /** |
| 27 | * Convert data from request into DTO |
| 28 | * |
| 29 | * @since 3.0.0 |
| 30 | */ |
| 31 | public static function fromRequest(array $requestData): self |
| 32 | { |
| 33 | $self = new self(); |
| 34 | $self->formId = (int)$requestData['formId']; |
| 35 | $self->requestData = $requestData; |
| 36 | |
| 37 | return $self; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * This method loops over the form schema to |
| 42 | * compares the request against the individual fields, |
| 43 | * their types and validation rules. |
| 44 | * |
| 45 | * @since 3.0.0 |
| 46 | * |
| 47 | * @throws DonationFormFieldErrorsException |
| 48 | */ |
| 49 | public function validate(): JsonResponse |
| 50 | { |
| 51 | $request = $this->getRequestData(); |
| 52 | |
| 53 | /** @var DonationForm $form */ |
| 54 | $form = DonationForm::find($this->formId); |
| 55 | |
| 56 | if (!$form) { |
| 57 | $this->throwDonationFormFieldErrorsException(['formId' => 'Invalid Form ID, Form not found']); |
| 58 | } |
| 59 | |
| 60 | $formFields = array_filter($form->schema()->getFields(), static function ($field) use ($request) { |
| 61 | return array_key_exists($field->getName(), $request); |
| 62 | }); |
| 63 | |
| 64 | $validator = (new CreateValidatorFromFormFields())($formFields, $request); |
| 65 | |
| 66 | if ($validator->fails()) { |
| 67 | $this->throwDonationFormFieldErrorsException($validator->errors()); |
| 68 | } |
| 69 | |
| 70 | return new JsonResponse(['valid' => true]); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @since 3.0.0 |
| 75 | */ |
| 76 | public function getRequestData(): array |
| 77 | { |
| 78 | return $this->requestData; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * This loops over an array of errors in the specific FieldAPI format, |
| 83 | * and converts them into a WP_Error object that is attached to the |
| 84 | * exception and delivered back to the client via JSON. |
| 85 | * |
| 86 | * @since 3.0.0 |
| 87 | * |
| 88 | * @param array<string, string> $errors |
| 89 | * |
| 90 | * @throws DonationFormFieldErrorsException |
| 91 | */ |
| 92 | private function throwDonationFormFieldErrorsException(array $errors) |
| 93 | { |
| 94 | $wpError = new WP_Error(); |
| 95 | |
| 96 | foreach ($errors as $id => $error) { |
| 97 | $wpError->add($id, $error); |
| 98 | } |
| 99 | |
| 100 | throw new DonationFormFieldErrorsException($wpError); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @since 3.0.0 |
| 105 | */ |
| 106 | public function toArray(): array |
| 107 | { |
| 108 | return get_object_vars($this); |
| 109 | } |
| 110 | } |
| 111 |