primaryAfter = $primaryAfter; $this->primaryBefore = $primaryBefore; $this->limit = $limit; $this->orderBy = $orderBy; $this->orderDirection = $orderDirection; $this->page = $page; } public function getAfter(): \DateTimeImmutable { return $this->primaryAfter; } public function getBefore(): \DateTimeImmutable { return $this->primaryBefore; } public function getLimit(): int { return $this->limit; } public function getOrderBy(): string { return $this->orderBy; } public function getOrderDirection(): string { return $this->orderDirection; } public function getPage(): int { return $this->page; } /** * @param Request $request * @return Query * @throws UnexpectedValueException */ public static function fromRequest(Request $request) { $query = $request->getParam('query'); if (!is_array($query)) { throw new UnexpectedValueException('Invalid query parameters'); } $primary = $query['primary'] ?? null; if (!is_array($primary)) { throw new UnexpectedValueException('Invalid query parameters'); } $primaryAfter = $primary['after'] ?? null; $primaryBefore = $primary['before'] ?? null; if ( !is_string($primaryAfter) || !is_string($primaryBefore) ) { throw new UnexpectedValueException('Invalid query parameters'); } $limit = $query['limit'] ?? 25; $orderBy = $query['order_by'] ?? ''; $orderDirection = isset($query['order']) && strtolower($query['order']) === 'asc' ? 'asc' : 'desc'; $page = $query['page'] ?? 1; return new self( new \DateTimeImmutable($primaryAfter), new \DateTimeImmutable($primaryBefore), $limit, $orderBy, $orderDirection, $page ); } public static function getRequestSchema(): Schema { return Builder::object( [ 'primary' => Builder::object( [ 'after' => Builder::string()->formatDateTime()->required(), 'before' => Builder::string()->formatDateTime()->required(), ] ), 'limit' => Builder::integer()->minimum(1)->maximum(100), 'order_by' => Builder::string(), 'order' => Builder::string(), 'page' => Builder::integer()->minimum(1), ] ); } }