appertly /
minotaur
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | /** |
||
| 4 | * Minotaur |
||
| 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 | * @license Apache-2.0 |
||
| 20 | */ |
||
| 21 | namespace Minotaur\Http; |
||
| 22 | |||
| 23 | use Psr\Http\Message\ResponseInterface as Response; |
||
| 24 | use Caridea\Http\Pagination; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * A trait that can be used by controllers who need to return typical JSON |
||
| 28 | */ |
||
| 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 |
||
|
0 ignored issues
–
show
|
|||
| 37 | */ |
||
| 38 | 6 | protected function sendJson(Response $response, $payload): Response |
|
| 39 | { |
||
| 40 | 6 | $response->getBody()->write(json_encode($payload)); |
|
| 41 | 6 | return $response->withHeader('Content-Type', 'application/json'); |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Sends a Content-Range header for pagination |
||
| 46 | * |
||
| 47 | * @param $response - The response |
||
| 48 | * @return - The JSON response |
||
|
0 ignored issues
–
show
The doc-type
- could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)
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. Loading history...
|
|||
| 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 |
||
|
0 ignored issues
–
show
The doc-type
- could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)
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. Loading history...
|
|||
| 78 | */ |
||
| 79 | protected function sendCreated(Response $response, string $type, array $ids, array $extra = []): Response |
||
| 80 | { |
||
| 81 | return $this->sendVerb('created', $response, $type, $ids, $extra) |
||
| 82 | ->withStatus(201, "Created"); |
||
| 83 | } |
||
| 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 |
||
|
0 ignored issues
–
show
The doc-type
- could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)
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. Loading history...
|
|||
| 93 | */ |
||
| 94 | protected function sendDeleted(Response $response, string $type, array $ids, array $extra = []): Response |
||
| 95 | { |
||
| 96 | return $this->sendVerb('deleted', $response, $type, $ids, $extra); |
||
| 97 | } |
||
| 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 |
||
|
0 ignored issues
–
show
The doc-type
- could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)
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. Loading history...
|
|||
| 107 | */ |
||
| 108 | protected function sendUpdated(Response $response, string $type, array $ids, array $extra = []): Response |
||
| 109 | { |
||
| 110 | return $this->sendVerb('updated', $response, $type, $ids, $extra); |
||
| 111 | } |
||
| 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 |
||
|
0 ignored issues
–
show
The doc-type
- could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)
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. Loading history...
|
|||
| 122 | */ |
||
| 123 | protected function sendVerb(string $verb, Response $response, string $type, array $ids, array $extra = []): Response |
||
| 124 | { |
||
| 125 | $send = array_merge([], $extra); |
||
| 126 | $send['success'] = true; |
||
| 127 | $send['message'] = "Objects $verb successfully"; |
||
| 128 | $send['objects'] = array_map(function ($id) use($type) { |
||
|
0 ignored issues
–
show
|
|||
| 129 | return ['type' => $type, 'id' => $id]; |
||
| 130 | }, $ids); |
||
| 131 | return $this->sendJson($response, $send); |
||
| 132 | } |
||
| 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.