QueryWithCompare.php
125 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 QueryWithCompare extends Query { |
| 14 | |
| 15 | /** @var \DateTimeImmutable */ |
| 16 | private $secondaryAfter; |
| 17 | |
| 18 | /** @var \DateTimeImmutable */ |
| 19 | private $secondaryBefore; |
| 20 | |
| 21 | public function __construct( |
| 22 | \DateTimeImmutable $primaryAfter, |
| 23 | \DateTimeImmutable $primaryBefore, |
| 24 | \DateTimeImmutable $secondaryAfter, |
| 25 | \DateTimeImmutable $secondaryBefore, |
| 26 | int $limit = 25, |
| 27 | string $orderBy = '', |
| 28 | string $orderDirection = 'asc', |
| 29 | int $page = 0, |
| 30 | array $filter = [], |
| 31 | ?string $search = null, |
| 32 | ?int $versionId = null |
| 33 | ) { |
| 34 | parent::__construct($primaryAfter, $primaryBefore, $limit, $orderBy, $orderDirection, $page, $filter, $search, $versionId); |
| 35 | $this->secondaryAfter = $secondaryAfter; |
| 36 | $this->secondaryBefore = $secondaryBefore; |
| 37 | } |
| 38 | |
| 39 | public function getCompareWithAfter(): \DateTimeImmutable { |
| 40 | return $this->secondaryAfter; |
| 41 | } |
| 42 | |
| 43 | public function getCompareWithBefore(): \DateTimeImmutable { |
| 44 | return $this->secondaryBefore; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param Request $request |
| 49 | * @return QueryWithCompare |
| 50 | * @throws UnexpectedValueException |
| 51 | */ |
| 52 | public static function fromRequest(Request $request) { |
| 53 | |
| 54 | $query = $request->getParam('query'); |
| 55 | if (!is_array($query)) { |
| 56 | throw new UnexpectedValueException('Invalid query parameters'); |
| 57 | } |
| 58 | $primary = $query['primary'] ?? null; |
| 59 | $secondary = $query['secondary'] ?? null; |
| 60 | if (!is_array($primary) || !is_array($secondary)) { |
| 61 | throw new UnexpectedValueException('Invalid query parameters'); |
| 62 | } |
| 63 | $primaryAfter = $primary['after'] ?? null; |
| 64 | $primaryBefore = $primary['before'] ?? null; |
| 65 | $secondaryAfter = $secondary['after'] ?? null; |
| 66 | $secondaryBefore = $secondary['before'] ?? null; |
| 67 | if ( |
| 68 | !is_string($primaryAfter) || |
| 69 | !is_string($primaryBefore) || |
| 70 | !is_string($secondaryAfter) || |
| 71 | !is_string($secondaryBefore) |
| 72 | ) { |
| 73 | throw new UnexpectedValueException('Invalid query parameters'); |
| 74 | } |
| 75 | |
| 76 | $limit = is_int($query['limit'] ?? null) ? $query['limit'] : 25; |
| 77 | $orderBy = is_string($query['orderBy'] ?? null) ? $query['orderBy'] : ''; |
| 78 | $orderDirection = is_string($query['orderDirection'] ?? null) ? $query['orderDirection'] : 'asc'; |
| 79 | $page = is_int($query['page'] ?? null) ? $query['page'] : 0; |
| 80 | $filter = is_array($query['filter'] ?? null) ? $query['filter'] : []; |
| 81 | $search = isset($query['search']) && is_string($query['search']) ? $query['search'] : null; |
| 82 | $versionId = is_int($query['version_id'] ?? null) ? $query['version_id'] : null; |
| 83 | |
| 84 | return new self( |
| 85 | new \DateTimeImmutable($primaryAfter), |
| 86 | new \DateTimeImmutable($primaryBefore), |
| 87 | new \DateTimeImmutable($secondaryAfter), |
| 88 | new \DateTimeImmutable($secondaryBefore), |
| 89 | $limit, |
| 90 | $orderBy, |
| 91 | $orderDirection, |
| 92 | $page, |
| 93 | $filter, |
| 94 | $search, |
| 95 | $versionId |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | public static function getRequestSchema(): Schema { |
| 100 | return Builder::object( |
| 101 | [ |
| 102 | 'primary' => Builder::object( |
| 103 | [ |
| 104 | 'after' => Builder::string()->formatDateTime()->required(), |
| 105 | 'before' => Builder::string()->formatDateTime()->required(), |
| 106 | ] |
| 107 | ), |
| 108 | 'secondary' => Builder::object( |
| 109 | [ |
| 110 | 'after' => Builder::string()->formatDateTime()->required(), |
| 111 | 'before' => Builder::string()->formatDateTime()->required(), |
| 112 | ] |
| 113 | ), |
| 114 | 'limit' => Builder::integer()->minimum(1)->maximum(100), |
| 115 | 'orderBy' => Builder::string(), |
| 116 | 'orderDirection' => Builder::string(), |
| 117 | 'page' => Builder::integer()->minimum(1), |
| 118 | 'filter' => Builder::object(), |
| 119 | 'search' => Builder::string()->nullable(), |
| 120 | 'version_id' => Builder::integer()->minimum(1)->nullable(), |
| 121 | ] |
| 122 | ); |
| 123 | } |
| 124 | } |
| 125 |