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; } }