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.

RecordService.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Record;
  3. use Tqdev\PhpCrudApi\Column\ReflectionService;
  4. use Tqdev\PhpCrudApi\Database\GenericDB;
  5. use Tqdev\PhpCrudApi\Record\Document\ListDocument;
  6. class RecordService
  7. {
  8. private $db;
  9. private $reflection;
  10. private $columns;
  11. private $joiner;
  12. private $filters;
  13. private $ordering;
  14. private $pagination;
  15. public function __construct(GenericDB $db, ReflectionService $reflection)
  16. {
  17. $this->db = $db;
  18. $this->reflection = $reflection;
  19. $this->columns = new ColumnIncluder();
  20. $this->joiner = new RelationJoiner($reflection, $this->columns);
  21. $this->filters = new FilterInfo();
  22. $this->ordering = new OrderingInfo();
  23. $this->pagination = new PaginationInfo();
  24. }
  25. private function sanitizeRecord(string $tableName, /* object */ $record, string $id)
  26. {
  27. $keyset = array_keys((array) $record);
  28. foreach ($keyset as $key) {
  29. if (!$this->reflection->getTable($tableName)->hasColumn($key)) {
  30. unset($record->$key);
  31. }
  32. }
  33. if ($id != '') {
  34. $pk = $this->reflection->getTable($tableName)->getPk();
  35. foreach ($this->reflection->getTable($tableName)->getColumnNames() as $key) {
  36. $field = $this->reflection->getTable($tableName)->getColumn($key);
  37. if ($field->getName() == $pk->getName()) {
  38. unset($record->$key);
  39. }
  40. }
  41. }
  42. }
  43. public function hasTable(string $table): bool
  44. {
  45. return $this->reflection->hasTable($table);
  46. }
  47. public function getType(string $table): string
  48. {
  49. return $this->reflection->getType($table);
  50. }
  51. public function create(string $tableName, /* object */ $record, array $params) /*: ?int*/
  52. {
  53. $this->sanitizeRecord($tableName, $record, '');
  54. $table = $this->reflection->getTable($tableName);
  55. $columnValues = $this->columns->getValues($table, true, $record, $params);
  56. return $this->db->createSingle($table, $columnValues);
  57. }
  58. public function read(string $tableName, string $id, array $params) /*: ?object*/
  59. {
  60. $table = $this->reflection->getTable($tableName);
  61. $this->joiner->addMandatoryColumns($table, $params);
  62. $columnNames = $this->columns->getNames($table, true, $params);
  63. $record = $this->db->selectSingle($table, $columnNames, $id);
  64. if ($record == null) {
  65. return null;
  66. }
  67. $records = array($record);
  68. $this->joiner->addJoins($table, $records, $params, $this->db);
  69. return $records[0];
  70. }
  71. public function update(string $tableName, string $id, /* object */ $record, array $params) /*: ?int*/
  72. {
  73. $this->sanitizeRecord($tableName, $record, $id);
  74. $table = $this->reflection->getTable($tableName);
  75. $columnValues = $this->columns->getValues($table, true, $record, $params);
  76. return $this->db->updateSingle($table, $columnValues, $id);
  77. }
  78. public function delete(string $tableName, string $id, array $params) /*: ?int*/
  79. {
  80. $table = $this->reflection->getTable($tableName);
  81. return $this->db->deleteSingle($table, $id);
  82. }
  83. public function increment(string $tableName, string $id, /* object */ $record, array $params) /*: ?int*/
  84. {
  85. $this->sanitizeRecord($tableName, $record, $id);
  86. $table = $this->reflection->getTable($tableName);
  87. $columnValues = $this->columns->getValues($table, true, $record, $params);
  88. return $this->db->incrementSingle($table, $columnValues, $id);
  89. }
  90. public function _list(string $tableName, array $params): ListDocument
  91. {
  92. $table = $this->reflection->getTable($tableName);
  93. $this->joiner->addMandatoryColumns($table, $params);
  94. $columnNames = $this->columns->getNames($table, true, $params);
  95. $condition = $this->filters->getCombinedConditions($table, $params);
  96. $columnOrdering = $this->ordering->getColumnOrdering($table, $params);
  97. if (!$this->pagination->hasPage($params)) {
  98. $offset = 0;
  99. $limit = $this->pagination->getPageLimit($params);
  100. $count = 0;
  101. } else {
  102. $offset = $this->pagination->getPageOffset($params);
  103. $limit = $this->pagination->getPageLimit($params);
  104. $count = $this->db->selectCount($table, $condition);
  105. }
  106. $records = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, $offset, $limit);
  107. $this->joiner->addJoins($table, $records, $params, $this->db);
  108. return new ListDocument($records, $count);
  109. }
  110. }