| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace cx_newsletter;
- use \cx_appengine\string_builder;
- use \exception;
- class groups_mapper
- extends mapper
- implements mapper_interface {
- public function create(database_item $target) : database_item {
- if ($target->has_id()) {
- throw $this->get_has_id_exception();
- }
- if ($this->exists($target)) {
- throw new exception('Group '.$target->name.' already exists.');
- }
- $converter = new group_converter();
- $converter->load_object($target);
- $result = $this->get_database()->insert(
- $this->get_tables()->groups(),
- $converter->get_array()
- );
- if ($result === false) {
- throw $this->get_exception('create');
- }
- return $this->complete(new group($this->get_database()->insert_id));
- }
- private function exists(database_item $target) : bool {
- $filter = [
- 'name' => $target->name
- ];
- $result = $this->load_all($filter);
- if (empty($result)) {
- return false;
- }
- if (!$target->has_id()) {
- return true;
- }
- if ($result[0]->get_id() === $target->get_id()) {
- return false;
- }
- return true;
- }
- public function save(database_item $target) : database_item {
- if (!$target->has_id()) {
- throw $this->get_blank_id_exception();
- }
- if ($this->exists($target)) {
- throw new exception('Group '.$target->name.' already exists.');
- }
- $converter = new group_converter();
- $converter->load_object($target);
- $condition = [];
- $condition['id'] = $target->get_id();
- $result = $this->get_database()->update(
- $this->get_tables()->groups(),
- $converter->get_array(),
- $condition
- );
- if ($result === false) {
- throw $this->get_exception('save');
- }
-
- return $this->complete($target);
- }
- public function load_all(array $filters = [], ?int $limit = null) : array {
- $query = new string_builder();
- $query->push('select id, name ');
- $query->push('from '.$this->get_tables()->groups().' ');
- $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('find');
- }
- $groups = [];
- $converter = new group_converter();
- foreach ($result as $row) {
- array_push($groups, $converter->load_array($row)->get_object());
- }
- return $groups;
- }
- public function complete(database_item $target) : database_item {
- if (!$target->has_id()) {
- throw $this->get_blank_id_exception();
- }
- $query = new string_builder();
- $query->push('select id, name ');
- $query->push('from '.$this->get_tables()->groups().' ');
- $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 group_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();
- }
-
- $result = $this->get_database()->delete(
- $this->get_tables()->groups(),
- [ 'id' => $target->get_id() ]
- );
- if ($result === false) {
- throw $this->get_exception('remove');
- }
- }
- public function count(array $filters = []) : int {
- $query = new string_builder();
- $query->push('select count(id) ');
- $query->push('from '.$this->get_tables()->groups().' ');
- $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.'$'
- ];
- $filters = $this->parse_filters(
- $columns,
- filter_type::like,
- filter_glue::single
- );
- $query = new string_builder();
- $query->push('select id, name ');
- $query->push('from '.$this->get_tables()->groups().' ');
- $query->push($filters);
- $query->push($this->parse_limit($limit));
- $result = $this->get_database()->get_result($query->get(), ARRAY_A);
- if ($result === null) {
- throw $this->get_exception('find');
- }
- $groups = [];
- $converter = new groups_converter();
- foreach ($result as $row) {
- array_push($groups, $converter->load_array($row)->get_object());
- }
- return $groups;
- }
- }
|