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 \Traversable|array $items The items to serialize |
||
50 | * @param \Caridea\Http\Pagination $pagination The pagination object |
||
51 | * @param int $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, $items, Pagination $pagination = null, int $total = null): Response |
|
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 |