Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
EventFilter
93.33% covered (success)
93.33%
14 / 15
66.67% covered (warning)
66.67%
2 / 3
10.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 all
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toQueryString
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
8.03
1<?php
2
3declare(strict_types=1);
4
5namespace LambdaTwelve\OneRecord\Client;
6
7use DateTimeImmutable;
8use DateTimeZone;
9
10/**
11 * The query parameters of GET /logistics-objects/{id}/logistics-events, as
12 * the specification names them. Timestamps are sent in the spec's basic
13 * ISO form (YYYYMMDDThhmmssZ).
14 */
15final readonly class EventFilter
16{
17    public const string SORT_CREATED_ASC = 'ASC-creationDate';
18    public const string SORT_CREATED_DESC = 'DESC-creationDate';
19    public const string SORT_EVENT_ASC = 'ASC-eventDate';
20    public const string SORT_EVENT_DESC = 'DESC-eventDate';
21
22    /**
23     * @param list<string> $eventCodes codes or code IRIs; any match passes
24     */
25    public function __construct(
26        public array $eventCodes = [],
27        public ?DateTimeImmutable $createdAfter = null,
28        public ?DateTimeImmutable $createdBefore = null,
29        public ?DateTimeImmutable $occurredAfter = null,
30        public ?DateTimeImmutable $occurredBefore = null,
31        public ?string $sort = null,
32        public ?int $limit = null,
33        public ?int $skip = null,
34    ) {}
35
36    public static function all(): self
37    {
38        return new self();
39    }
40
41    public function toQueryString(): string
42    {
43        $parameters = [];
44        if ($this->eventCodes !== []) {
45            $parameters['event-code'] = implode(',', $this->eventCodes);
46        }
47        foreach (['created-after' => $this->createdAfter, 'created-before' => $this->createdBefore, 'occurred-after' => $this->occurredAfter, 'occurred-before' => $this->occurredBefore] as $name => $value) {
48            if ($value !== null) {
49                $parameters[$name] = $value->setTimezone(new DateTimeZone('UTC'))->format('Ymd\THis\Z');
50            }
51        }
52        if ($this->sort !== null) {
53            $parameters['sort'] = $this->sort;
54        }
55        if ($this->limit !== null) {
56            $parameters['limit'] = (string) $this->limit;
57        }
58        if ($this->skip !== null) {
59            $parameters['skip'] = (string) $this->skip;
60        }
61
62        return $parameters === [] ? '' : '?' . http_build_query($parameters, '', '&', PHP_QUERY_RFC3986);
63    }
64}