API.php
6 months ago
AbstractListingEndpoint.php
3 weeks ago
ApiException.php
2 months ago
Endpoint.php
3 months ago
EndpointContainer.php
3 years ago
ErrorResponse.php
3 years ago
Exception.php
3 years ago
ListingRequestValidationTrait.php
1 month ago
Request.php
3 years ago
Response.php
1 year ago
index.php
3 years ago
ListingRequestValidationTrait.php
140 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\API\REST; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use DateTimeImmutable; |
| 9 | |
| 10 | /** |
| 11 | * Shared request validation for DataViews-backed listing endpoints |
| 12 | * (see {@see AbstractListingEndpoint}). Provides the reusable building blocks — |
| 13 | * sort field/order, pagination integers, and `yyyy-MM-dd` date filters — so each |
| 14 | * listing only declares its own allowed sort fields and filter keys. |
| 15 | * |
| 16 | * Error codes are namespaced per listing via {@see getListingValidationErrorPrefix()} |
| 17 | * so a 400 still says which listing rejected the request |
| 18 | * (e.g. `mailpoet_logs_invalid_orderby`). |
| 19 | */ |
| 20 | trait ListingRequestValidationTrait { |
| 21 | /** Lowercase listing slug used to build error codes, e.g. `logs` or `forms`. */ |
| 22 | abstract protected function getListingValidationErrorPrefix(): string; |
| 23 | |
| 24 | private function listingValidationError(string $message, string $suffix): ApiException { |
| 25 | return new ApiException( |
| 26 | $message, |
| 27 | 400, |
| 28 | 'mailpoet_' . $this->getListingValidationErrorPrefix() . '_invalid_' . $suffix |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param mixed $sortField |
| 34 | * @param string[] $allowedFields |
| 35 | */ |
| 36 | protected function validateSortField($sortField, array $allowedFields): void { |
| 37 | if ($sortField === null || $sortField === '') { |
| 38 | return; |
| 39 | } |
| 40 | if (!is_string($sortField) || !in_array($sortField, $allowedFields, true)) { |
| 41 | throw $this->listingValidationError( |
| 42 | sprintf( |
| 43 | // translators: %s is a comma-separated list of allowed sort fields. |
| 44 | __('Unsupported sort field. Allowed values are: %s.', 'mailpoet'), |
| 45 | implode(', ', $allowedFields) |
| 46 | ), |
| 47 | 'orderby' |
| 48 | ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** @param mixed $sortOrder */ |
| 53 | protected function validateSortOrder($sortOrder): void { |
| 54 | if ($sortOrder === null || $sortOrder === '') { |
| 55 | return; |
| 56 | } |
| 57 | if (!is_string($sortOrder) || !in_array(strtolower($sortOrder), ['asc', 'desc'], true)) { |
| 58 | throw $this->listingValidationError( |
| 59 | sprintf( |
| 60 | // translators: %s is a comma-separated list of allowed sort orders. |
| 61 | __('Unsupported sort order. Allowed values are: %s.', 'mailpoet'), |
| 62 | implode(', ', ['asc', 'desc']) |
| 63 | ), |
| 64 | 'order' |
| 65 | ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | protected function validatePagination(Request $request): void { |
| 70 | $this->validatePositiveInteger($request->getParam('page'), 'page', 1, self::MAX_PAGE); |
| 71 | $this->validatePositiveInteger($request->getParam('per_page'), 'per_page', 1, self::MAX_PER_PAGE); |
| 72 | $this->validatePositiveInteger($request->getParam('limit'), 'limit', 1, self::MAX_PER_PAGE); |
| 73 | $this->validatePositiveInteger($request->getParam('offset'), 'offset', 0, self::MAX_PAGE); |
| 74 | } |
| 75 | |
| 76 | /** @param mixed $value */ |
| 77 | private function validatePositiveInteger($value, string $name, int $min, int $max): void { |
| 78 | if ($value === null || $value === '') { |
| 79 | return; |
| 80 | } |
| 81 | $integer = $this->getIntegerValue($value); |
| 82 | if ($integer === null || $integer < $min || $integer > $max) { |
| 83 | throw $this->listingValidationError( |
| 84 | sprintf( |
| 85 | // translators: %1$s is a request parameter name, %2$d is the maximum accepted value. |
| 86 | __('%1$s must be an integer no greater than %2$d.', 'mailpoet'), |
| 87 | $name, |
| 88 | $max |
| 89 | ), |
| 90 | $name |
| 91 | ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** @param mixed $value */ |
| 96 | private function getIntegerValue($value): ?int { |
| 97 | if (is_int($value)) { |
| 98 | return $value; |
| 99 | } |
| 100 | if (is_string($value) && ctype_digit($value)) { |
| 101 | return (int)$value; |
| 102 | } |
| 103 | return null; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @param array<string, mixed> $filters |
| 108 | */ |
| 109 | protected function validateDateFilter(array $filters, string $field): ?DateTimeImmutable { |
| 110 | if (!array_key_exists($field, $filters) || $filters[$field] === '') { |
| 111 | return null; |
| 112 | } |
| 113 | if (!is_string($filters[$field])) { |
| 114 | throw $this->listingValidationError( |
| 115 | __('Date filters must use the YYYY-MM-DD format.', 'mailpoet'), |
| 116 | $field |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | $date = DateTimeImmutable::createFromFormat('!Y-m-d', $filters[$field]); |
| 121 | $errors = DateTimeImmutable::getLastErrors(); |
| 122 | if (!$date || (is_array($errors) && ($errors['warning_count'] > 0 || $errors['error_count'] > 0)) || $date->format('Y-m-d') !== $filters[$field]) { |
| 123 | throw $this->listingValidationError( |
| 124 | __('Date filters must use the YYYY-MM-DD format.', 'mailpoet'), |
| 125 | $field |
| 126 | ); |
| 127 | } |
| 128 | return $date; |
| 129 | } |
| 130 | |
| 131 | protected function validateDateRange(?DateTimeImmutable $from, ?DateTimeImmutable $to): void { |
| 132 | if ($from && $to && $from > $to) { |
| 133 | throw $this->listingValidationError( |
| 134 | __('The from date must be before or equal to the to date.', 'mailpoet'), |
| 135 | 'date_range' |
| 136 | ); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 |