123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace Tqdev\PhpCrudApi\Controller;
-
- use Tqdev\PhpCrudApi\Record\ErrorCode;
- use Tqdev\PhpCrudApi\Record\RecordService;
- use Tqdev\PhpCrudApi\Request;
- use Tqdev\PhpCrudApi\Response;
- use Tqdev\PhpCrudApi\Middleware\Router\Router;
-
- class RecordController
- {
- private $service;
- private $responder;
-
- public function __construct(Router $router, Responder $responder, RecordService $service)
- {
- $router->register('GET', '/records/*', array($this, '_list'));
- $router->register('POST', '/records/*', array($this, 'create'));
- $router->register('GET', '/records/*/*', array($this, 'read'));
- $router->register('PUT', '/records/*/*', array($this, 'update'));
- $router->register('DELETE', '/records/*/*', array($this, 'delete'));
- $router->register('PATCH', '/records/*/*', array($this, 'increment'));
- $this->service = $service;
- $this->responder = $responder;
- }
-
- public function _list(Request $request): Response
- {
- $table = $request->getPathSegment(2);
- $params = $request->getParams();
- if (!$this->service->exists($table)) {
- return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
- }
- return $this->responder->success($this->service->_list($table, $params));
- }
-
- public function read(Request $request): Response
- {
- $table = $request->getPathSegment(2);
- $id = $request->getPathSegment(3);
- $params = $request->getParams();
- if (!$this->service->exists($table)) {
- return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
- }
- if (strpos($id, ',') !== false) {
- $ids = explode(',', $id);
- $result = [];
- for ($i = 0; $i < count($ids); $i++) {
- array_push($result, $this->service->read($table, $ids[$i], $params));
- }
- return $this->responder->success($result);
- } else {
- $response = $this->service->read($table, $id, $params);
- if ($response === null) {
- return $this->responder->error(ErrorCode::RECORD_NOT_FOUND, $id);
- }
- return $this->responder->success($response);
- }
- }
-
- public function create(Request $request): Response
- {
- $table = $request->getPathSegment(2);
- $record = $request->getBody();
- if ($record === null) {
- return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
- }
- $params = $request->getParams();
- if (!$this->service->exists($table)) {
- return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
- }
- if (is_array($record)) {
- $result = array();
- foreach ($record as $r) {
- $result[] = $this->service->create($table, $r, $params);
- }
- return $this->responder->success($result);
- } else {
- return $this->responder->success($this->service->create($table, $record, $params));
- }
- }
-
- public function update(Request $request): Response
- {
- $table = $request->getPathSegment(2);
- $id = $request->getPathSegment(3);
- $record = $request->getBody();
- if ($record === null) {
- return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
- }
- $params = $request->getParams();
- if (!$this->service->exists($table)) {
- return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
- }
- $ids = explode(',', $id);
- if (is_array($record)) {
- if (count($ids) != count($record)) {
- return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
- }
- $result = array();
- for ($i = 0; $i < count($ids); $i++) {
- $result[] = $this->service->update($table, $ids[$i], $record[$i], $params);
- }
- return $this->responder->success($result);
- } else {
- if (count($ids) != 1) {
- return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
- }
- return $this->responder->success($this->service->update($table, $id, $record, $params));
- }
- }
-
- public function delete(Request $request): Response
- {
- $table = $request->getPathSegment(2);
- $id = $request->getPathSegment(3);
- $params = $request->getParams();
- if (!$this->service->exists($table)) {
- return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
- }
- $ids = explode(',', $id);
- if (count($ids) > 1) {
- $result = array();
- for ($i = 0; $i < count($ids); $i++) {
- $result[] = $this->service->delete($table, $ids[$i], $params);
- }
- return $this->responder->success($result);
- } else {
- return $this->responder->success($this->service->delete($table, $id, $params));
- }
- }
-
- }
|