1 | <?php |
||
29 | trait JsonHelper |
||
30 | { |
||
31 | /** |
||
32 | * Send something as JSON |
||
33 | * |
||
34 | * @param $response - The response |
||
35 | * @param $payload - The object to serialize |
||
36 | * @return - The JSON response |
||
|
|||
37 | */ |
||
38 | 6 | protected function sendJson(Response $response, $payload): Response |
|
43 | |||
44 | /** |
||
45 | * Sends a Content-Range header for pagination |
||
46 | * |
||
47 | * @param $response - The response |
||
48 | * @return - The JSON response |
||
49 | * @since 0.6.0 |
||
50 | */ |
||
51 | 6 | protected function sendItems(Response $response, iterable $items, ?Pagination $pagination = null, ?int $total = null): Response |
|
52 | { |
||
53 | 6 | if ($items instanceof \Minotaur\Db\CursorSubset) { |
|
54 | 1 | $total = $items->getTotal(); |
|
55 | 1 | $items = $items->toArray(); |
|
56 | } else { |
||
57 | 5 | $items = is_array($items) ? $items : iterator_to_array($items, false); |
|
58 | 5 | $total = $total ?? count($items); |
|
59 | } |
||
60 | 6 | $start = $pagination === null ? 0 : $pagination->getOffset(); |
|
61 | 6 | $max = $pagination === null ? 0 : $pagination->getMax(); |
|
62 | // make sure $end is no higher than $total and isn't negative |
||
63 | 6 | $end = max(min((PHP_INT_MAX - $max < $start ? PHP_INT_MAX : $start + $max), $total) - 1, 0); |
|
64 | 6 | return $this->sendJson( |
|
65 | 6 | $response->withHeader('Content-Range', "items $start-$end/$total"), |
|
66 | 6 | $items |
|
67 | ); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Send notice that an entity was created. |
||
72 | * |
||
73 | * @param $response - The response |
||
74 | * @param $type - The entity type |
||
75 | * @param array<string> $ids The entity ids |
||
76 | * @param array<string,mixed> $extra Any extra data to serialize |
||
77 | * @return - The JSON response |
||
78 | */ |
||
79 | protected function sendCreated(Response $response, string $type, array $ids, array $extra = []): Response |
||
84 | |||
85 | /** |
||
86 | * Send notice that objects were deleted. |
||
87 | * |
||
88 | * @param $response - The response |
||
89 | * @param $type - The entity type |
||
90 | * @param array<string> $ids The entity ids |
||
91 | * @param array<string,mixed> $extra Any extra data to serialize |
||
92 | * @return - The JSON response |
||
93 | */ |
||
94 | protected function sendDeleted(Response $response, string $type, array $ids, array $extra = []): Response |
||
98 | |||
99 | /** |
||
100 | * Send notice that objects were updated. |
||
101 | * |
||
102 | * @param $response - The response |
||
103 | * @param $type - The entity type |
||
104 | * @param array<string> $ids The entity ids |
||
105 | * @param array<string,mixed> $extra Any extra data to serialize |
||
106 | * @return - The JSON response |
||
107 | */ |
||
108 | protected function sendUpdated(Response $response, string $type, array $ids, array $extra = []): Response |
||
112 | |||
113 | /** |
||
114 | * Sends a generic notice that objects have been operated on |
||
115 | * |
||
116 | * @param $verb - The verb |
||
117 | * @param $response - The response |
||
118 | * @param $type - The entity type |
||
119 | * @param array<string> $ids The entity ids |
||
120 | * @param array<string,mixed> $extra Any extra data to serialize |
||
121 | * @return - The JSON response |
||
122 | */ |
||
123 | protected function sendVerb(string $verb, Response $response, string $type, array $ids, array $extra = []): Response |
||
133 | } |
||
134 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.