api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RecordController.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Controller;
  3. use Psr\Http\Message\ResponseInterface;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  6. use Tqdev\PhpCrudApi\Record\ErrorCode;
  7. use Tqdev\PhpCrudApi\Record\RecordService;
  8. use Tqdev\PhpCrudApi\RequestUtils;
  9. class RecordController
  10. {
  11. private $service;
  12. private $responder;
  13. public function __construct(Router $router, Responder $responder, RecordService $service)
  14. {
  15. $router->register('GET', '/records/*', array($this, '_list'));
  16. $router->register('POST', '/records/*', array($this, 'create'));
  17. $router->register('GET', '/records/*/*', array($this, 'read'));
  18. $router->register('PUT', '/records/*/*', array($this, 'update'));
  19. $router->register('DELETE', '/records/*/*', array($this, 'delete'));
  20. $router->register('PATCH', '/records/*/*', array($this, 'increment'));
  21. $this->service = $service;
  22. $this->responder = $responder;
  23. }
  24. public function _list(ServerRequestInterface $request): ResponseInterface
  25. {
  26. $table = RequestUtils::getPathSegment($request, 2);
  27. $params = RequestUtils::getParams($request);
  28. if (!$this->service->hasTable($table)) {
  29. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  30. }
  31. return $this->responder->success($this->service->_list($table, $params));
  32. }
  33. public function read(ServerRequestInterface $request): ResponseInterface
  34. {
  35. $table = RequestUtils::getPathSegment($request, 2);
  36. if (!$this->service->hasTable($table)) {
  37. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  38. }
  39. if ($this->service->getType($table) != 'table') {
  40. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  41. }
  42. $id = RequestUtils::getPathSegment($request, 3);
  43. $params = RequestUtils::getParams($request);
  44. if (strpos($id, ',') !== false) {
  45. $ids = explode(',', $id);
  46. $result = [];
  47. for ($i = 0; $i < count($ids); $i++) {
  48. array_push($result, $this->service->read($table, $ids[$i], $params));
  49. }
  50. return $this->responder->success($result);
  51. } else {
  52. $response = $this->service->read($table, $id, $params);
  53. if ($response === null) {
  54. return $this->responder->error(ErrorCode::RECORD_NOT_FOUND, $id);
  55. }
  56. return $this->responder->success($response);
  57. }
  58. }
  59. public function create(ServerRequestInterface $request): ResponseInterface
  60. {
  61. $table = RequestUtils::getPathSegment($request, 2);
  62. if (!$this->service->hasTable($table)) {
  63. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  64. }
  65. if ($this->service->getType($table) != 'table') {
  66. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  67. }
  68. $record = $request->getParsedBody();
  69. if ($record === null) {
  70. return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
  71. }
  72. $params = RequestUtils::getParams($request);
  73. if (is_array($record)) {
  74. $result = array();
  75. foreach ($record as $r) {
  76. $result[] = $this->service->create($table, $r, $params);
  77. }
  78. return $this->responder->success($result);
  79. } else {
  80. return $this->responder->success($this->service->create($table, $record, $params));
  81. }
  82. }
  83. public function update(ServerRequestInterface $request): ResponseInterface
  84. {
  85. $table = RequestUtils::getPathSegment($request, 2);
  86. if (!$this->service->hasTable($table)) {
  87. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  88. }
  89. if ($this->service->getType($table) != 'table') {
  90. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  91. }
  92. $id = RequestUtils::getPathSegment($request, 3);
  93. $params = RequestUtils::getParams($request);
  94. $record = $request->getParsedBody();
  95. if ($record === null) {
  96. return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
  97. }
  98. $ids = explode(',', $id);
  99. if (is_array($record)) {
  100. if (count($ids) != count($record)) {
  101. return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
  102. }
  103. $result = array();
  104. for ($i = 0; $i < count($ids); $i++) {
  105. $result[] = $this->service->update($table, $ids[$i], $record[$i], $params);
  106. }
  107. return $this->responder->success($result);
  108. } else {
  109. if (count($ids) != 1) {
  110. return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
  111. }
  112. return $this->responder->success($this->service->update($table, $id, $record, $params));
  113. }
  114. }
  115. public function delete(ServerRequestInterface $request): ResponseInterface
  116. {
  117. $table = RequestUtils::getPathSegment($request, 2);
  118. if (!$this->service->hasTable($table)) {
  119. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  120. }
  121. if ($this->service->getType($table) != 'table') {
  122. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  123. }
  124. $id = RequestUtils::getPathSegment($request, 3);
  125. $params = RequestUtils::getParams($request);
  126. $ids = explode(',', $id);
  127. if (count($ids) > 1) {
  128. $result = array();
  129. for ($i = 0; $i < count($ids); $i++) {
  130. $result[] = $this->service->delete($table, $ids[$i], $params);
  131. }
  132. return $this->responder->success($result);
  133. } else {
  134. return $this->responder->success($this->service->delete($table, $id, $params));
  135. }
  136. }
  137. public function increment(ServerRequestInterface $request): ResponseInterface
  138. {
  139. $table = RequestUtils::getPathSegment($request, 2);
  140. if (!$this->service->hasTable($table)) {
  141. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  142. }
  143. if ($this->service->getType($table) != 'table') {
  144. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  145. }
  146. $id = RequestUtils::getPathSegment($request, 3);
  147. $record = $request->getParsedBody();
  148. if ($record === null) {
  149. return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
  150. }
  151. $params = RequestUtils::getParams($request);
  152. $ids = explode(',', $id);
  153. if (is_array($record)) {
  154. if (count($ids) != count($record)) {
  155. return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
  156. }
  157. $result = array();
  158. for ($i = 0; $i < count($ids); $i++) {
  159. $result[] = $this->service->increment($table, $ids[$i], $record[$i], $params);
  160. }
  161. return $this->responder->success($result);
  162. } else {
  163. if (count($ids) != 1) {
  164. return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
  165. }
  166. return $this->responder->success($this->service->increment($table, $id, $record, $params));
  167. }
  168. }
  169. }