1 | <?php |
||
30 | trait JsonHelper |
||
31 | { |
||
32 | /** |
||
33 | * Send something as JSON. |
||
34 | * |
||
35 | * @param \Psr\Http\Message\ResponseInterface $response The response |
||
36 | * @param mixed $payload The object to serialize |
||
37 | * @return \Psr\Http\Message\ResponseInterface The JSON response |
||
38 | */ |
||
39 | 9 | protected function sendJson(Response $response, $payload): Response |
|
44 | |||
45 | /** |
||
46 | * Sends a Content-Range header for pagination. |
||
47 | * |
||
48 | * @param \Psr\Http\Message\ResponseInterface $response The response |
||
49 | * @param iterable $items The items to serialize |
||
50 | * @param \Caridea\Http\Pagination|null $pagination The pagination object |
||
51 | * @param int|null $total The total number of records, if `$items` is a subset |
||
52 | * @return \Psr\Http\Message\ResponseInterface The JSON response |
||
53 | */ |
||
54 | 6 | protected function sendItems(Response $response, iterable $items, ?Pagination $pagination = null, ?int $total = null): Response |
|
55 | { |
||
56 | 6 | $items = is_array($items) ? $items : ($items instanceof \Traversable ? iterator_to_array($items, false) : []); |
|
57 | 6 | $total = $total ?? count($items); |
|
58 | 6 | $start = $pagination === null ? 0 : $pagination->getOffset(); |
|
59 | 6 | $max = $pagination === null ? 0 : $pagination->getMax(); |
|
60 | // make sure $end is no higher than $total and isn't negative |
||
61 | 6 | $end = max(min((PHP_INT_MAX - $max < $start ? PHP_INT_MAX : $start + $max), $total) - 1, 0); |
|
62 | 6 | return $this->sendJson( |
|
63 | 6 | $response->withHeader('Content-Range', "items $start-$end/$total"), |
|
64 | 6 | $items |
|
65 | ); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Send notice that an entity was created. |
||
70 | * |
||
71 | * @param \Psr\Http\Message\ResponseInterface $response The response |
||
72 | * @param string $type The entity type |
||
73 | * @param array<mixed> $ids The entity ids |
||
74 | * @param array<string,mixed> $extra Any extra data to serialize |
||
75 | * @return \Psr\Http\Message\ResponseInterface The JSON response |
||
76 | */ |
||
77 | 1 | protected function sendCreated(Response $response, string $type, array $ids, array $extra = []): Response |
|
82 | |||
83 | /** |
||
84 | * Send notice that objects were deleted. |
||
85 | * |
||
86 | * @param \Psr\Http\Message\ResponseInterface $response The response |
||
87 | * @param string $type The entity type |
||
88 | * @param array<mixed> $ids The entity ids |
||
89 | * @param array<string,mixed> $extra Any extra data to serialize |
||
90 | * @return \Psr\Http\Message\ResponseInterface The JSON response |
||
91 | */ |
||
92 | 1 | protected function sendDeleted(Response $response, string $type, array $ids, array $extra = []): Response |
|
96 | |||
97 | /** |
||
98 | * Send notice that objects were updated. |
||
99 | * |
||
100 | * @param \Psr\Http\Message\ResponseInterface $response The response |
||
101 | * @param string $type The entity type |
||
102 | * @param array<mixed> $ids The entity ids |
||
103 | * @param array<string,mixed> $extra Any extra data to serialize |
||
104 | * @return \Psr\Http\Message\ResponseInterface The JSON response |
||
105 | */ |
||
106 | 1 | protected function sendUpdated(Response $response, string $type, array $ids, array $extra = []): Response |
|
110 | |||
111 | /** |
||
112 | * Sends a generic notice that objects have been operated on. |
||
113 | * |
||
114 | * @param string $verb The verb |
||
115 | * @param \Psr\Http\Message\ResponseInterface $response The response |
||
116 | * @param string $type The entity type |
||
117 | * @param array<mixed> $ids The entity ids |
||
118 | * @param array<string,mixed> $extra Any extra data to serialize |
||
119 | * @return \Psr\Http\Message\ResponseInterface The JSON response |
||
120 | */ |
||
121 | 3 | protected function sendVerb(string $verb, Response $response, string $type, array $ids, array $extra = []): Response |
|
131 | } |
||
132 |