AbstractSegmentsListingEndpoint.php
1 month ago
DynamicSegmentsBulkActionEndpoint.php
2 months ago
DynamicSegmentsListingEndpoint.php
1 month ago
SegmentRequestValidationTrait.php
2 months ago
SegmentsBulkActionEndpoint.php
2 months ago
SegmentsEndpoint.php
2 months ago
SegmentsListingEndpoint.php
1 month ago
index.php
2 months ago
SegmentRequestValidationTrait.php
122 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Segments\RestApi\Endpoints; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\REST\ApiException; |
| 9 | |
| 10 | trait SegmentRequestValidationTrait { |
| 11 | protected function validateGroup(?string $group): string { |
| 12 | $group = $group ?: 'all'; |
| 13 | if (!in_array($group, ['all', 'trash'], true)) { |
| 14 | throw new ApiException( |
| 15 | __('Unsupported group. Allowed values are: all, trash.', 'mailpoet'), |
| 16 | 400, |
| 17 | 'mailpoet_segments_invalid_group' |
| 18 | ); |
| 19 | } |
| 20 | return $group; |
| 21 | } |
| 22 | |
| 23 | protected function validateOrder(?string $order, string $default): string { |
| 24 | $order = strtolower($order ?: $default); |
| 25 | if (!in_array($order, ['asc', 'desc'], true)) { |
| 26 | throw new ApiException( |
| 27 | __('Unsupported sort order. Allowed values are: asc, desc.', 'mailpoet'), |
| 28 | 400, |
| 29 | 'mailpoet_segments_invalid_order' |
| 30 | ); |
| 31 | } |
| 32 | return $order; |
| 33 | } |
| 34 | |
| 35 | protected function validatePage($page): int { |
| 36 | if ($page === null || $page === '') { |
| 37 | return 1; |
| 38 | } |
| 39 | if (!is_numeric($page) || (string)(int)$page !== (string)$page || (int)$page < 1 || (int)$page > 100000) { |
| 40 | throw new ApiException( |
| 41 | __('Page must be a positive integer.', 'mailpoet'), |
| 42 | 400, |
| 43 | 'mailpoet_segments_invalid_page' |
| 44 | ); |
| 45 | } |
| 46 | return (int)$page; |
| 47 | } |
| 48 | |
| 49 | protected function validatePerPage($perPage, int $default): int { |
| 50 | if ($perPage === null || $perPage === '') { |
| 51 | return $default; |
| 52 | } |
| 53 | if (!is_numeric($perPage) || (string)(int)$perPage !== (string)$perPage || (int)$perPage < 1 || (int)$perPage > 100) { |
| 54 | throw new ApiException( |
| 55 | sprintf( |
| 56 | // translators: %d is maximum items per page. |
| 57 | __('Per page must be a positive integer no greater than %d.', 'mailpoet'), |
| 58 | 100 |
| 59 | ), |
| 60 | 400, |
| 61 | 'mailpoet_segments_invalid_per_page' |
| 62 | ); |
| 63 | } |
| 64 | return (int)$perPage; |
| 65 | } |
| 66 | |
| 67 | protected function validateOffset($offset): int { |
| 68 | if ($offset === null || $offset === '') { |
| 69 | return 0; |
| 70 | } |
| 71 | if ( |
| 72 | !is_numeric($offset) |
| 73 | || (string)(int)$offset !== (string)$offset |
| 74 | || (int)$offset < 0 |
| 75 | || (int)$offset > 100000 |
| 76 | ) { |
| 77 | throw new ApiException( |
| 78 | __('Offset must be a non-negative integer no greater than 100000.', 'mailpoet'), |
| 79 | 400, |
| 80 | 'mailpoet_segments_invalid_offset' |
| 81 | ); |
| 82 | } |
| 83 | return (int)$offset; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param mixed $ids |
| 88 | * @return int[] |
| 89 | */ |
| 90 | protected function validateIds($ids): array { |
| 91 | if (!is_array($ids) || $ids === []) { |
| 92 | throw new ApiException( |
| 93 | __('At least one segment id is required.', 'mailpoet'), |
| 94 | 400, |
| 95 | 'mailpoet_segments_ids_required' |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | $validIds = []; |
| 100 | foreach ($ids as $id) { |
| 101 | if (!is_int($id) && !(is_string($id) && ctype_digit($id))) { |
| 102 | throw new ApiException( |
| 103 | __('Segment ids must be positive integers.', 'mailpoet'), |
| 104 | 400, |
| 105 | 'mailpoet_segments_invalid_ids' |
| 106 | ); |
| 107 | } |
| 108 | $id = (int)$id; |
| 109 | if ($id < 1) { |
| 110 | throw new ApiException( |
| 111 | __('Segment ids must be positive integers.', 'mailpoet'), |
| 112 | 400, |
| 113 | 'mailpoet_segments_invalid_ids' |
| 114 | ); |
| 115 | } |
| 116 | $validIds[] = $id; |
| 117 | } |
| 118 | |
| 119 | return array_values(array_unique($validIds)); |
| 120 | } |
| 121 | } |
| 122 |