From c30b22be356d667084611c61964d39114eba5bf4 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Mon, 19 Oct 2020 21:29:45 +0200 Subject: Section: Update documentation and remove global db usages --- ratatoeskr/sys/models.php | 217 +++++++++++++++++++++++----------------------- 1 file changed, 107 insertions(+), 110 deletions(-) (limited to 'ratatoeskr/sys') diff --git a/ratatoeskr/sys/models.php b/ratatoeskr/sys/models.php index 1d1f696..03ced65 100644 --- a/ratatoeskr/sys/models.php +++ b/ratatoeskr/sys/models.php @@ -1601,85 +1601,76 @@ class Plugin extends BySQLRowEnabled } } -/* - * Class: Section +/** * Representing a section */ class Section extends BySQLRowEnabled { + /** @var int */ private $id; - /* - * Variables: Public class variables - * - * $name - The name of the section. - * $title - The title of the section (a object). - * $template - Name of the template. - */ + /** @var string The name of the section. */ public $name; + + /** @var Multilingual The title of the section. */ public $title; + + /** @var string Name of the template. */ public $template; protected function populate_by_sqlrow($sqlrow) { - $this->id = $sqlrow["id"]; - $this->name = $sqlrow["name"]; - $this->title = Multilingual::by_id($sqlrow["title"]); - $this->template = $sqlrow["template"]; + $this->id = (int)$sqlrow["id"]; + $this->name = (string)$sqlrow["name"]; + $this->title = Multilingual::by_id($sqlrow["title"]); // FIXME: Right now, we can't pass a $db to Multilingual::by_id here, as this violates the populate_by_sqlrow function signature :( + $this->template = (string)$sqlrow["template"]; } - /* - * Function: test_name + /** * Tests, if a name is a valid section name. * - * Parameters: - * $name - The name to test. - * - * Returns: - * True, if the name is a valid section name, False otherwise. + * @param string|mixed $name The name to test. + * @return bool */ - public static function test_name($name) + public static function test_name($name): bool { - return preg_match("/^[a-zA-Z0-9\\-_]+$/", $name) != 0; + return preg_match("/^[a-zA-Z0-9\\-_]+$/", (string)$name) != 0; } - /* - * Function: get_id - */ - public function get_id() + public function get_id(): int { return $this->id; } - /* - * Constructor: create + /** * Creates a new section. * - * Parameters: - * $name - The name of the new section. - * - * Throws: - * , + * @param string|mixed $name The name of the new section. + * @param Database|null $db + * @return Section + * @throws AlreadyExistsError + * @throws InvalidDataError */ - public static function create($name) + public static function create($name, ?Database $db = null): self { - global $db_con; + $name = (string)$name; + $db = $db ?? Env::getGlobal()->database(); if (!self::test_name($name)) { throw new InvalidDataError("invalid_section_name"); } try { - self::by_name($name); + self::by_name($name, $db); } catch (DoesNotExistError $e) { $obj = new self(); $obj->name = $name; - $obj->title = Multilingual::create(); + $obj->title = Multilingual::create($db); $obj->template = ""; - qdb("INSERT INTO `PREFIX_sections` (`name`, `title`, `template`) VALUES (?, ?, '')", $name, $obj->title->get_id()); + $db->query("INSERT INTO `PREFIX_sections` (`name`, `title`, `template`) VALUES (?, ?, '')", $name, $obj->title->get_id()); - $obj->id = $db_con->lastInsertId(); + $obj->id = $db->lastInsertId(); return $obj; } @@ -1687,22 +1678,20 @@ class Section extends BySQLRowEnabled throw new AlreadyExistsError(); } - /* - * Constructor: by_id + /** * Gets section by ID. * - * Parameters: - * $id - The ID. - * - * Returns: - * A
object. - * - * Throws: - * + * @param int|mixed $id + * @param Database|null $db + * @return self + * @throws DoesNotExistError */ - public static function by_id($id) + public static function by_id($id, ?Database $db = null): self { - $stmt = qdb("SELECT `id`, `name`, `title`, `template` FROM `PREFIX_sections` WHERE `id` = ?", $id); + $id = (int)$id; + $db = $db ?? Env::getGlobal()->database(); + + $stmt = $db->query("SELECT `id`, `name`, `title`, `template` FROM `PREFIX_sections` WHERE `id` = ?", $id); $sqlrow = $stmt->fetch(); if ($sqlrow === false) { throw new DoesNotExistError(); @@ -1711,22 +1700,20 @@ class Section extends BySQLRowEnabled return self::by_sqlrow($sqlrow); } - /* - * Constructor: by_name + /** * Gets section by name. * - * Parameters: - * $name - The name. - * - * Returns: - * A
object. - * - * Throws: - * + * @param string|mixed $name + * @param Database|null $db + * @return self + * @throws DoesNotExistError */ - public static function by_name($name) + public static function by_name($name, ?Database $db = null): self { - $stmt = qdb("SELECT `id`, `name`, `title`, `template` FROM `PREFIX_sections` WHERE `name` = ?", $name); + $name = (string)$name; + $db = $db ?? Env::getGlobal()->database(); + + $stmt = $db->query("SELECT `id`, `name`, `title`, `template` FROM `PREFIX_sections` WHERE `name` = ?", $name); $sqlrow = $stmt->fetch(); if ($sqlrow === false) { throw new DoesNotExistError(); @@ -1735,55 +1722,58 @@ class Section extends BySQLRowEnabled return self::by_sqlrow($sqlrow); } - /* - * Constructor: all + /** * Gets all sections. * - * Returns: - * Array of Section objects. + * @param Database|null $db + * @return self[] */ - public static function all() + public static function all(?Database $db = null): array { + $db = $db ?? Env::getGlobal()->database(); + $rv = []; - $stmt = qdb("SELECT `id`, `name`, `title`, `template` FROM `PREFIX_sections` WHERE 1"); + $stmt = $db->query("SELECT `id`, `name`, `title`, `template` FROM `PREFIX_sections` WHERE 1"); while ($sqlrow = $stmt->fetch()) { $rv[] = self::by_sqlrow($sqlrow); } return $rv; } - /* - * Function: get_styles + /** * Get all styles associated with this section. * - * Returns: - * List of