LogsFilterTrait.php
102 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Logging\RestApi; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | /** |
| 9 | * Validates and normalizes the logs filter object (`from`/`to`/`name`/`level`) |
| 10 | * shared by the logs listing and deletion endpoints, so both interpret the same |
| 11 | * filter shape identically. Host endpoints must also use |
| 12 | * {@see \MailPoet\API\REST\ListingRequestValidationTrait} for the date helpers |
| 13 | * and namespaced error codes. |
| 14 | */ |
| 15 | trait LogsFilterTrait { |
| 16 | /** |
| 17 | * @param mixed $filters |
| 18 | * @return array{from?: string, to?: string, name?: string[], level?: int[]} |
| 19 | */ |
| 20 | protected function validateAndNormalizeLogsFilter($filters): array { |
| 21 | if ($filters === null || $filters === []) { |
| 22 | return []; |
| 23 | } |
| 24 | if (!is_array($filters)) { |
| 25 | throw $this->listingValidationError(__('Filters must be an object.', 'mailpoet'), 'filter'); |
| 26 | } |
| 27 | $allowedKeys = ['from', 'to', 'name', 'level']; |
| 28 | $normalizedFilters = []; |
| 29 | foreach ($filters as $key => $value) { |
| 30 | if (!is_string($key) || !in_array($key, $allowedKeys, true)) { |
| 31 | throw $this->listingValidationError(__('Unsupported logs filter.', 'mailpoet'), 'filter'); |
| 32 | } |
| 33 | $normalizedFilters[$key] = $value; |
| 34 | } |
| 35 | |
| 36 | $criteria = []; |
| 37 | $names = $this->normalizeLogsNameFilter($normalizedFilters); |
| 38 | if ($names !== []) { |
| 39 | $criteria['name'] = $names; |
| 40 | } |
| 41 | $levels = $this->normalizeLogsLevelFilter($normalizedFilters); |
| 42 | if ($levels !== []) { |
| 43 | $criteria['level'] = $levels; |
| 44 | } |
| 45 | |
| 46 | $from = $this->validateDateFilter($normalizedFilters, 'from'); |
| 47 | $to = $this->validateDateFilter($normalizedFilters, 'to'); |
| 48 | $this->validateDateRange($from, $to); |
| 49 | if ($from) { |
| 50 | $criteria['from'] = $from->format('Y-m-d'); |
| 51 | } |
| 52 | if ($to) { |
| 53 | $criteria['to'] = $to->format('Y-m-d'); |
| 54 | } |
| 55 | |
| 56 | return $criteria; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @param array<string, mixed> $filters |
| 61 | * @return string[] |
| 62 | */ |
| 63 | private function normalizeLogsNameFilter(array $filters): array { |
| 64 | if (!array_key_exists('name', $filters) || $filters['name'] === '' || $filters['name'] === []) { |
| 65 | return []; |
| 66 | } |
| 67 | if (!is_array($filters['name'])) { |
| 68 | throw $this->listingValidationError(__('The name filter must be an array of strings.', 'mailpoet'), 'name'); |
| 69 | } |
| 70 | $names = []; |
| 71 | foreach ($filters['name'] as $value) { |
| 72 | if (!is_string($value)) { |
| 73 | throw $this->listingValidationError(__('The name filter must be an array of strings.', 'mailpoet'), 'name'); |
| 74 | } |
| 75 | $names[] = $value; |
| 76 | } |
| 77 | return array_values(array_unique($names)); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @param array<string, mixed> $filters |
| 82 | * @return int[] |
| 83 | */ |
| 84 | private function normalizeLogsLevelFilter(array $filters): array { |
| 85 | if (!array_key_exists('level', $filters) || $filters['level'] === '' || $filters['level'] === []) { |
| 86 | return []; |
| 87 | } |
| 88 | if (!is_array($filters['level'])) { |
| 89 | throw $this->listingValidationError(__('The level filter must be an array of integers.', 'mailpoet'), 'level'); |
| 90 | } |
| 91 | $levels = []; |
| 92 | foreach ($filters['level'] as $value) { |
| 93 | $integer = $this->getIntegerValue($value); |
| 94 | if ($integer === null) { |
| 95 | throw $this->listingValidationError(__('The level filter must be an array of integers.', 'mailpoet'), 'level'); |
| 96 | } |
| 97 | $levels[] = $integer; |
| 98 | } |
| 99 | return array_values(array_unique($levels)); |
| 100 | } |
| 101 | } |
| 102 |