2-customers_mapper.php 4.8 KB

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