Query.php
165 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet\Analytics\Entities; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\REST\Request; |
| 9 | use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException; |
| 10 | use MailPoet\Validator\Builder; |
| 11 | use MailPoet\Validator\Schema; |
| 12 | |
| 13 | class Query { |
| 14 | |
| 15 | /** @var \DateTimeImmutable */ |
| 16 | private $primaryAfter; |
| 17 | |
| 18 | /** @var \DateTimeImmutable */ |
| 19 | private $primaryBefore; |
| 20 | |
| 21 | /** @var int */ |
| 22 | private $limit; |
| 23 | |
| 24 | /** @var string */ |
| 25 | private $orderBy; |
| 26 | |
| 27 | /** @var string */ |
| 28 | private $orderDirection; |
| 29 | |
| 30 | /** @var int */ |
| 31 | private $page; |
| 32 | |
| 33 | /** @var array */ |
| 34 | private $filter; |
| 35 | |
| 36 | /** @var string | null */ |
| 37 | private $search; |
| 38 | |
| 39 | /** @var int | null */ |
| 40 | private $versionId; |
| 41 | |
| 42 | public function __construct( |
| 43 | \DateTimeImmutable $primaryAfter, |
| 44 | \DateTimeImmutable $primaryBefore, |
| 45 | int $limit = 25, |
| 46 | string $orderBy = '', |
| 47 | string $orderDirection = 'asc', |
| 48 | int $page = 1, |
| 49 | array $filter = [], |
| 50 | ?string $search = null, |
| 51 | ?int $versionId = null |
| 52 | ) { |
| 53 | $this->primaryAfter = $primaryAfter; |
| 54 | $this->primaryBefore = $primaryBefore; |
| 55 | $this->limit = $limit; |
| 56 | $this->orderBy = $orderBy; |
| 57 | $this->orderDirection = $orderDirection; |
| 58 | $this->page = $page; |
| 59 | $this->filter = $filter; |
| 60 | $this->search = $search; |
| 61 | $this->versionId = $versionId; |
| 62 | } |
| 63 | |
| 64 | public function getAfter(): \DateTimeImmutable { |
| 65 | return $this->primaryAfter; |
| 66 | } |
| 67 | |
| 68 | public function getBefore(): \DateTimeImmutable { |
| 69 | return $this->primaryBefore; |
| 70 | } |
| 71 | |
| 72 | public function getLimit(): int { |
| 73 | return $this->limit; |
| 74 | } |
| 75 | |
| 76 | public function getOrderBy(): string { |
| 77 | return $this->orderBy; |
| 78 | } |
| 79 | |
| 80 | public function getOrderDirection(): string { |
| 81 | return $this->orderDirection; |
| 82 | } |
| 83 | |
| 84 | public function getPage(): int { |
| 85 | return $this->page; |
| 86 | } |
| 87 | |
| 88 | public function getFilter(): array { |
| 89 | return $this->filter; |
| 90 | } |
| 91 | |
| 92 | public function getSearch(): ?string { |
| 93 | return $this->search; |
| 94 | } |
| 95 | |
| 96 | public function getVersionId(): ?int { |
| 97 | return $this->versionId; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param Request $request |
| 102 | * @return Query |
| 103 | * @throws UnexpectedValueException |
| 104 | */ |
| 105 | public static function fromRequest(Request $request) { |
| 106 | $query = $request->getParam('query'); |
| 107 | if (!is_array($query)) { |
| 108 | throw new UnexpectedValueException('Invalid query parameters'); |
| 109 | } |
| 110 | $primary = $query['primary'] ?? null; |
| 111 | if (!is_array($primary)) { |
| 112 | throw new UnexpectedValueException('Invalid query parameters'); |
| 113 | } |
| 114 | $primaryAfter = $primary['after'] ?? null; |
| 115 | $primaryBefore = $primary['before'] ?? null; |
| 116 | if ( |
| 117 | !is_string($primaryAfter) || |
| 118 | !is_string($primaryBefore) |
| 119 | ) { |
| 120 | throw new UnexpectedValueException('Invalid query parameters'); |
| 121 | } |
| 122 | |
| 123 | $limit = is_int($query['limit'] ?? null) ? $query['limit'] : 25; |
| 124 | $orderBy = is_string($query['order_by'] ?? null) ? $query['order_by'] : ''; |
| 125 | $order = $query['order'] ?? null; |
| 126 | $orderDirection = is_string($order) && strtolower($order) === 'asc' ? 'asc' : 'desc'; |
| 127 | $page = is_int($query['page'] ?? null) ? $query['page'] : 1; |
| 128 | $filter = is_array($query['filter'] ?? null) ? $query['filter'] : []; |
| 129 | $search = isset($query['search']) && is_string($query['search']) ? $query['search'] : null; |
| 130 | $versionId = is_int($query['version_id'] ?? null) ? $query['version_id'] : null; |
| 131 | |
| 132 | return new self( |
| 133 | new \DateTimeImmutable($primaryAfter), |
| 134 | new \DateTimeImmutable($primaryBefore), |
| 135 | $limit, |
| 136 | $orderBy, |
| 137 | $orderDirection, |
| 138 | $page, |
| 139 | $filter, |
| 140 | $search, |
| 141 | $versionId |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | public static function getRequestSchema(): Schema { |
| 146 | return Builder::object( |
| 147 | [ |
| 148 | 'primary' => Builder::object( |
| 149 | [ |
| 150 | 'after' => Builder::string()->formatDateTime()->required(), |
| 151 | 'before' => Builder::string()->formatDateTime()->required(), |
| 152 | ] |
| 153 | ), |
| 154 | 'limit' => Builder::integer()->minimum(1)->maximum(100), |
| 155 | 'order_by' => Builder::string(), |
| 156 | 'order' => Builder::string(), |
| 157 | 'page' => Builder::integer()->minimum(1), |
| 158 | 'filter' => Builder::object([]), |
| 159 | 'search' => Builder::string()->nullable(), |
| 160 | 'version_id' => Builder::integer()->minimum(1)->nullable(), |
| 161 | ] |
| 162 | ); |
| 163 | } |
| 164 | } |
| 165 |