| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace cx_newsletter;
- class customers_mapper
- extends mapper
- implements mapper_interface {
- public function create(database_item $target) : database_item {
- if ($target->has_id()) {
- throw $this->get_has_id_exception();
- }
- $converter = new customers_converter();
- $converter->load_object($target);
- $result = $this->get_database()->insert(
- $this->get_tables()->customers(),
- $converter->get_array()
- );
- if ($result === false) {
- throw $this->get_exception('create');
- }
- return $this->complete(new customer($this->get_database()->insert_id));
- }
- public function save(database_item $target) : database_item {
- if (!$target->has_id()) {
- throw $this->get_blank_id_exception();
- }
- $converter = new customers_converter();
- $converter->load_object($target);
- $condition = [];
- $condition['id'] = $target->get_id();
- $result = $this->get_database()->update(
- $this->get_tables()->customers(),
- $converter->get_array(),
- $condition
- );
- return $this->complete($target);
- }
- public function load_all(array $filters = [], ?int $limit = null) : array {
- $query = new \cx_appengine\string_builder();
- $query->push('select * ');
- $query->push('from '.$this->get_tables()->customers().' ');
- $query->push($this->parse_filters($filters));
- $query->push($this->parse_limit($limit));
- $result = $this->get_database()->get_results($query->get(), ARRAY_A);
-
- if ($result === null) {
- throw $this->get_exception();
- }
- $customers = [];
- $converter = new customers_converter();
- foreach ($result as $row) {
- array_push($customers, $converter->load_array($row)->get_object());
- }
- return $customers;
- }
- public function complete(database_item $target) : database_item {
- if (!$target->has_id()) {
- throw $this->get_blank_id_exception();
- }
- $query = new \cx_appengine\string_builder();
- $query->push('select * ');
- $query->push('from '.$this->get_tables()->customers().' ');
- $query->push('where id=%d');
- $sql = $this->get_database()->prepare($query, $target->get_id());
- $row = $this->get_database()->get_row($sql, ARRAY_A);
- if ($row === null) {
- throw $this->get_exception('complete');
- }
- $converter = new customers_converter();
- $converter->load_array($row);
- return $converter->get_object();
- }
- public function remove(database_item $target) : void {
- if (!$target->has_id()) {
- throw $this->get_blank_id_exception();
- }
- $this->get_database()->delete(
- $this->get_tables()->send_log(),
- [ 'customer' => $target->get_id()]
- );
- $result = $this->get_database()->delete(
- $this->get_tables()->customers(),
- [ 'id' => $target->get_id() ]
- );
- if ($result === false) {
- throw $this->get_exception('remove');
- }
- }
- public function count(array $filters = []) : int {
- $query = new \cx_appengine\string_builder();
- $query->push('select count(id) ');
- $query->push('from '.$this->get_tables()->customers().' ');
- $query->push($this->parse_filters($filters));
- $result = $this->get_database()->get_var($query->get());
- if ($result === null) {
- throw $this->get_exception('count');
- }
- return intval($result);
- }
- public function find(string $phrase, ?int $limit = null) : array {
- $columns = [
- 'name' => '%'.$phrase.'%',
- 'surname' => '%'.$phrase.'%',
- 'company' => '%'.$phrase.'%',
- 'comments' => '%'.$phrase.'%',
- 'email' => '%'.$phrase.'%',
- 'phone_number' => '%'.$phrase.'%',
- 'tester' => '%'.$phrase.'%',
- ];
- $filters = $this->parse_filters(
- $columns,
- filter_type::like,
- filter_glue::single
- );
- $query = new \cx_appengine\string_builder();
- $query->push('select * ');
- $query->push('from '.$this->get_tables()->customers().' ');
- $query->push($filters);
- $query->push($this->parse_limit($limit));
- $result = $this->get_database()->get_results($query->get(), ARRAY_A);
-
- if ($result === null) {
- throw $this->get_exception();
- }
- $customers = [];
- $converter = new customers_converter();
- foreach ($result as $row) {
- array_push($customers, $converter->load_array($row)->get_object());
- }
- return $customers;
- }
- }
|