3-groups_mapper.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace cx_newsletter;
  3. use \cx_appengine\string_builder;
  4. use \exception;
  5. class groups_mapper
  6. extends mapper
  7. implements mapper_interface {
  8. public function create(database_item $target) : database_item {
  9. if ($target->has_id()) {
  10. throw $this->get_has_id_exception();
  11. }
  12. if ($this->exists($target)) {
  13. throw new exception('Group '.$target->name.' already exists.');
  14. }
  15. $converter = new group_converter();
  16. $converter->load_object($target);
  17. $result = $this->get_database()->insert(
  18. $this->get_tables()->groups(),
  19. $converter->get_array()
  20. );
  21. if ($result === false) {
  22. throw $this->get_exception('create');
  23. }
  24. return $this->complete(new group($this->get_database()->insert_id));
  25. }
  26. private function exists(database_item $target) : bool {
  27. $filter = [
  28. 'name' => $target->name
  29. ];
  30. $result = $this->load_all($filter);
  31. if (empty($result)) {
  32. return false;
  33. }
  34. if (!$target->has_id()) {
  35. return true;
  36. }
  37. if ($result[0]->get_id() === $target->get_id()) {
  38. return false;
  39. }
  40. return true;
  41. }
  42. public function save(database_item $target) : database_item {
  43. if (!$target->has_id()) {
  44. throw $this->get_blank_id_exception();
  45. }
  46. if ($this->exists($target)) {
  47. throw new exception('Group '.$target->name.' already exists.');
  48. }
  49. $converter = new group_converter();
  50. $converter->load_object($target);
  51. $condition = [];
  52. $condition['id'] = $target->get_id();
  53. $result = $this->get_database()->update(
  54. $this->get_tables()->groups(),
  55. $converter->get_array(),
  56. $condition
  57. );
  58. if ($result === false) {
  59. throw $this->get_exception('save');
  60. }
  61. return $this->complete($target);
  62. }
  63. public function load_all(array $filters = [], ?int $limit = null) : array {
  64. $query = new string_builder();
  65. $query->push('select id, name ');
  66. $query->push('from '.$this->get_tables()->groups().' ');
  67. $query->push($this->parse_filters($filters));
  68. $query->push($this->parse_limit($limit));
  69. $result = $this->get_database()->get_results($query->get(), ARRAY_A);
  70. if ($result === null) {
  71. throw $this->get_exception('find');
  72. }
  73. $groups = [];
  74. $converter = new group_converter();
  75. foreach ($result as $row) {
  76. array_push($groups, $converter->load_array($row)->get_object());
  77. }
  78. return $groups;
  79. }
  80. public function complete(database_item $target) : database_item {
  81. if (!$target->has_id()) {
  82. throw $this->get_blank_id_exception();
  83. }
  84. $query = new string_builder();
  85. $query->push('select id, name ');
  86. $query->push('from '.$this->get_tables()->groups().' ');
  87. $query->push('where id=%d');
  88. $sql = $this->get_database()->prepare($query, $target->get_id());
  89. $row = $this->get_database()->get_row($sql, ARRAY_A);
  90. if ($row === null) {
  91. throw $this->get_exception('complete');
  92. }
  93. $converter = new group_converter();
  94. $converter->load_array($row);
  95. return $converter->get_object();
  96. }
  97. public function remove(database_item $target) : void {
  98. if (!$target->has_id()) {
  99. throw $this->get_blank_id_exception();
  100. }
  101. $result = $this->get_database()->delete(
  102. $this->get_tables()->groups(),
  103. [ 'id' => $target->get_id() ]
  104. );
  105. if ($result === false) {
  106. throw $this->get_exception('remove');
  107. }
  108. }
  109. public function count(array $filters = []) : int {
  110. $query = new string_builder();
  111. $query->push('select count(id) ');
  112. $query->push('from '.$this->get_tables()->groups().' ');
  113. $query->push($this->parse_filters($filters));
  114. $result = $this->get_database()->get_var($query->get());
  115. if ($result === null) {
  116. throw $this->get_exception('count');
  117. }
  118. return intval($result);
  119. }
  120. public function find(string $phrase, ?int $limit = null) : array {
  121. $columns = [
  122. 'name' => '%'.$phrase.'$'
  123. ];
  124. $filters = $this->parse_filters(
  125. $columns,
  126. filter_type::like,
  127. filter_glue::single
  128. );
  129. $query = new string_builder();
  130. $query->push('select id, name ');
  131. $query->push('from '.$this->get_tables()->groups().' ');
  132. $query->push($filters);
  133. $query->push($this->parse_limit($limit));
  134. $result = $this->get_database()->get_result($query->get(), ARRAY_A);
  135. if ($result === null) {
  136. throw $this->get_exception('find');
  137. }
  138. $groups = [];
  139. $converter = new groups_converter();
  140. foreach ($result as $row) {
  141. array_push($groups, $converter->load_array($row)->get_object());
  142. }
  143. return $groups;
  144. }
  145. }