|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tqdev\PhpCrudApi\Middleware; |
| 4 | + |
| 5 | +use Psr\Http\Message\ResponseInterface; |
| 6 | +use Psr\Http\Message\ServerRequestInterface; |
| 7 | +use Psr\Http\Server\RequestHandlerInterface; |
| 8 | +use Tqdev\PhpCrudApi\Middleware\Base\Middleware; |
| 9 | +use Tqdev\PhpCrudApi\RequestUtils; |
| 10 | +use Tqdev\PhpCrudApi\ResponseFactory; |
| 11 | + |
| 12 | +class JsonMiddleware extends Middleware |
| 13 | +{ |
| 14 | + private function convertJsonRequestValue($value) /*: object */ |
| 15 | + { |
| 16 | + if (is_array($value) || is_object($value)) { |
| 17 | + $value = json_encode($value,JSON_UNESCAPED_UNICODE); |
| 18 | + } |
| 19 | + return $value; |
| 20 | + } |
| 21 | + |
| 22 | + private function convertJsonRequest($object, array $fieldNames) /*: object */ |
| 23 | + { |
| 24 | + if (is_array($object)) { |
| 25 | + foreach ($object as $i => $obj) { |
| 26 | + foreach ($obj as $k => $v) { |
| 27 | + if (in_array('all', $fieldNames) || in_array($k, $fieldNames)) { |
| 28 | + $object[$i]->$k = $this->convertJsonRequestValue($v); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + } else if (is_object($object)) { |
| 33 | + foreach ($object as $k => $v) { |
| 34 | + if (in_array('all', $fieldNames) || in_array($k, $fieldNames)) { |
| 35 | + $object->$k = $this->convertJsonRequestValue($v); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + return $object; |
| 40 | + } |
| 41 | + |
| 42 | + private function convertJsonResponseValue(string $value) /*: object */ |
| 43 | + { |
| 44 | + if (strlen($value) > 0 && in_array($value[0],['[','{'])) { |
| 45 | + $parsed = json_decode($value); |
| 46 | + if (json_last_error() == JSON_ERROR_NONE) { |
| 47 | + $value = $parsed; |
| 48 | + } |
| 49 | + } |
| 50 | + return $value; |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + private function convertJsonResponse($object, array $fieldNames) /*: object */ |
| 55 | + { |
| 56 | + if (is_array($object)) { |
| 57 | + foreach ($object as $k => $v) { |
| 58 | + $object[$k] = $this->convertJsonResponse($v, $fieldNames); |
| 59 | + } |
| 60 | + } else if (is_object($object)) { |
| 61 | + foreach ($object as $k => $v) { |
| 62 | + if (in_array('all', $fieldNames) || in_array($k, $fieldNames)) { |
| 63 | + $object->$k = $this->convertJsonResponse($v, $fieldNames); |
| 64 | + } |
| 65 | + } |
| 66 | + } else if (is_string($object)) { |
| 67 | + $object = $this->convertJsonResponseValue($object); |
| 68 | + } |
| 69 | + return $object; |
| 70 | + } |
| 71 | + |
| 72 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface |
| 73 | + { |
| 74 | + $operation = RequestUtils::getOperation($request); |
| 75 | + $controllerPath = RequestUtils::getPathSegment($request, 1); |
| 76 | + $tableName = RequestUtils::getPathSegment($request, 2); |
| 77 | + |
| 78 | + $controllerPaths = $this->getArrayProperty('controllers', 'all'); |
| 79 | + $tableNames = $this->getArrayProperty('tables', 'all'); |
| 80 | + $fieldNames = $this->getArrayProperty('fields', 'all'); |
| 81 | + if ( |
| 82 | + (in_array('all', $controllerPaths) || in_array($controllerPath, $controllerPaths)) && |
| 83 | + (in_array('all', $tableNames) || in_array($tableName, $tableNames)) |
| 84 | + ) { |
| 85 | + if (in_array($operation, ['create', 'update'])) { |
| 86 | + $records = $request->getParsedBody(); |
| 87 | + $records = $this->convertJsonRequest($records,$fieldNames); |
| 88 | + $request = $request->withParsedBody($records); |
| 89 | + } |
| 90 | + $response = $next->handle($request); |
| 91 | + if (in_array($operation, ['read', 'list'])) { |
| 92 | + $records = json_decode($response->getBody()->getContents()); |
| 93 | + $records = $this->convertJsonResponse($records, $fieldNames); |
| 94 | + $response = ResponseFactory::fromObject($response->getStatusCode(), $records); |
| 95 | + } |
| 96 | + } else { |
| 97 | + $response = $next->handle($request); |
| 98 | + } |
| 99 | + return $response; |
| 100 | + } |
| 101 | +} |
0 commit comments