1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Caridea |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2017 Appertly |
19
|
|
|
* @copyright 2017-2018 LibreWorks contributors |
20
|
|
|
* @license Apache-2.0 |
21
|
|
|
*/ |
22
|
|
|
namespace Caridea\Http; |
23
|
|
|
|
24
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
25
|
|
|
use Caridea\Http\Pagination; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* A trait that can be used by any class that needs to output typical JSON. |
29
|
|
|
*/ |
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 |
40
|
|
|
{ |
41
|
9 |
|
$response->getBody()->write(json_encode($payload)); |
42
|
9 |
|
return $response->withHeader('Content-Type', 'application/json'); |
43
|
|
|
} |
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 |
78
|
|
|
{ |
79
|
1 |
|
return $this->sendVerb('created', $response, $type, $ids, $extra) |
80
|
1 |
|
->withStatus(201, "Created"); |
81
|
|
|
} |
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 |
93
|
|
|
{ |
94
|
1 |
|
return $this->sendVerb('deleted', $response, $type, $ids, $extra); |
95
|
|
|
} |
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 |
107
|
|
|
{ |
108
|
1 |
|
return $this->sendVerb('updated', $response, $type, $ids, $extra); |
109
|
|
|
} |
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 |
122
|
|
|
{ |
123
|
3 |
|
$send = array_merge([], $extra); |
124
|
3 |
|
$send['success'] = true; |
125
|
3 |
|
$send['message'] = "Objects $verb successfully"; |
126
|
3 |
|
$send['objects'] = array_map(function ($id) use ($type) { |
127
|
3 |
|
return ['type' => $type, 'id' => $id]; |
128
|
3 |
|
}, $ids); |
129
|
3 |
|
return $this->sendJson($response, $send); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|