diff options
author | Freya Murphy <freya@freyacat.org> | 2024-07-08 17:22:30 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-07-08 17:22:30 -0400 |
commit | 8a2c577823b69117af8eda9b1a46bfbcae8153c6 (patch) | |
tree | e89dee0f77377a665c78d8a3cea6012888d9af73 /src/web/helpers | |
parent | fix2 (diff) | |
download | website-8a2c577823b69117af8eda9b1a46bfbcae8153c6.tar.gz website-8a2c577823b69117af8eda9b1a46bfbcae8153c6.tar.bz2 website-8a2c577823b69117af8eda9b1a46bfbcae8153c6.zip |
a few fixes, just a few....
Diffstat (limited to 'src/web/helpers')
-rw-r--r-- | src/web/helpers/aria.php | 2 | ||||
-rw-r--r-- | src/web/helpers/database.php | 80 | ||||
-rw-r--r-- | src/web/helpers/image.php | 4 | ||||
-rw-r--r-- | src/web/helpers/lang.php | 31 | ||||
-rw-r--r-- | src/web/helpers/markdown.php | 5 | ||||
-rw-r--r-- | src/web/helpers/sanitize.php | 2 |
6 files changed, 76 insertions, 48 deletions
diff --git a/src/web/helpers/aria.php b/src/web/helpers/aria.php index 8ebfcc5..9782299 100644 --- a/src/web/helpers/aria.php +++ b/src/web/helpers/aria.php @@ -1,6 +1,6 @@ <?php /* Copyright (c) 2024 Freya Murphy */ -function aria_section($id, $title = NULL): string { +function aria_section(string $id, ?string $title = NULL): string { $out = ''; if ($title) { $idh = $id . '_heading'; diff --git a/src/web/helpers/database.php b/src/web/helpers/database.php index 25cb5ba..132ed81 100644 --- a/src/web/helpers/database.php +++ b/src/web/helpers/database.php @@ -1,16 +1,24 @@ <?php /* Copyright (c) 2024 Freya Murphy */ +function __nullify(mixed $val): mixed { + if (!$val) { + return NULL; + } else { + return $val; + } +} + class DatabaseQuery { - private $conn; - private $query; + private \PDO $conn; + private string $query; - private $where; - private $set; + private bool $where; + private bool $set; - private $param; + private array $param; - function __construct($conn) { + function __construct(\PDO $conn) { $this->conn = $conn; $this->query = ''; @@ -23,7 +31,7 @@ class DatabaseQuery { /// ARBITRARY QUERY /// - public function query($query) { + public function query(string $query): DatabaseQuery { $this->query .= $query; return $this; } @@ -32,12 +40,12 @@ class DatabaseQuery { /// SELECT /// - public function select($select) { + public function select(string $select): DatabaseQuery { $this->query .= "SELECT $select\n"; return $this; } - public function from($from) { + public function from(string $from): DatabaseQuery { $this->query .= "FROM $from\n"; return $this; } @@ -46,7 +54,7 @@ class DatabaseQuery { /// INSERT /// - public function insert_into($insert, ...$columns) { + public function insert_into(string $insert, string ...$columns): DatabaseQuery { $this->query .= "INSERT INTO $insert\n ("; foreach ($columns as $idx => $column) { if ($idx !== 0) { @@ -58,7 +66,7 @@ class DatabaseQuery { return $this; } - public function values(...$values) { + public function values(mixed ...$values): DatabaseQuery { $this->query .= "VALUES ("; foreach ($values as $idx => $value) { if ($idx !== 0) { @@ -75,7 +83,7 @@ class DatabaseQuery { /// WHERE /// - public function where($cond) { + public function where(string $cond): DatabaseQuery { if (!$this->where) { $this->where = TRUE; $this->query .= "WHERE "; @@ -86,7 +94,10 @@ class DatabaseQuery { return $this; } - public function where_in($column, $array) { + /** + * @param array<mixed> $array + */ + public function where_in(string $column, array $array): DatabaseQuery { if (!$this->where) { $this->where = TRUE; $this->query .= "WHERE "; @@ -102,7 +113,10 @@ class DatabaseQuery { return $this; } - private function in($array) { + /** + * @param array<mixed> $array + */ + private function in(array $array): DatabaseQuery { $in = 'IN ('; foreach ($array as $idx => $item) { if ($idx != 0) { @@ -119,31 +133,31 @@ class DatabaseQuery { /// OPERATORS /// - public function like($item) { + public function like(mixed $item): DatabaseQuery { $this->query .= "LIKE ?\n"; array_push($this->param, $item); return $this; } - public function eq($item) { + public function eq(mixed $item): DatabaseQuery { $this->query .= "= ?\n"; array_push($this->param, $item); return $this; } - public function ne($item) { + public function ne(mixed $item): DatabaseQuery { $this->query .= "<> ?\n"; array_push($this->param, $item); return $this; } - public function lt($item) { + public function lt(mixed $item): DatabaseQuery { $this->query .= "< ?\n"; array_push($this->param, $item); return $this; } - public function le($item) { + public function le(mixed $item): DatabaseQuery { $this->query .= "<= ?\n"; array_push($this->param, $item); return $this; @@ -153,7 +167,7 @@ class DatabaseQuery { /// JOINS /// - public function join($table, $on, $type = 'LEFT') { + public function join(string $table, string $on, string $type = 'LEFT'): DatabaseQuery { $this->query .= "$type JOIN $table ON $on\n"; return $this; } @@ -162,19 +176,19 @@ class DatabaseQuery { /// LIMIT, OFFSET, ORDER /// - public function limit($limit) { + public function limit(int $limit): DatabaseQuery { $this->query .= "LIMIT ?\n"; array_push($this->param, $limit); return $this; } - public function offset($offset) { + public function offset(int $offset): DatabaseQuery { $this->query .= "OFFSET ?\n"; array_push($this->param, $offset); return $this; } - public function order_by($column, $order = 'ASC') { + public function order_by(string $column, string $order = 'ASC'): DatabaseQuery { $this->query .= "ORDER BY " . $column . ' ' . $order . ' '; return $this; } @@ -183,7 +197,7 @@ class DatabaseQuery { /// COLLECT /// - public function rows(...$params) { + public function rows(mixed ...$params): ?array { $args = $this->param; foreach ($params as $param) { array_push($args, $param); @@ -196,20 +210,20 @@ class DatabaseQuery { echo '<br> >> caused by <<<br>'; echo str_replace("\n", "<br>", $this->query); } - return $stmt->fetchAll(PDO::FETCH_ASSOC); + return __nullify($stmt->fetchAll(PDO::FETCH_ASSOC)); } - public function row(...$params) { + public function row(mixed ...$params): ?array { $args = $this->param; foreach ($params as $param) { array_push($args, $param); } $stmt = $this->conn->prepare($this->query); $stmt->execute($args); - return $stmt->fetch(PDO::FETCH_ASSOC); + return __nullify($stmt->fetch(PDO::FETCH_ASSOC)); } - public function execute(...$params) { + public function execute(mixed ...$params): bool { $args = $this->param; foreach ($params as $param) { array_push($args, $param); @@ -234,13 +248,13 @@ class DatabaseQuery { */ class DatabaseHelper { - private $conn; + private ?\PDO $conn; function __construct() { $this->conn = NULL; } - private function connect() { + private function connect(): \PDO { if ($this->conn === NULL) { $user = getenv("POSTGRES_USER"); $pass = getenv("POSTGRES_PASSWORD"); @@ -261,19 +275,19 @@ class DatabaseHelper { return $this->conn; } - public function select($select) { + public function select(string $select): DatabaseQuery { $conn = $this->connect(); $query = new DatabaseQuery($conn); return $query->select($select); } - public function insert_into($insert, ...$columns) { + public function insert_into(string $insert, string ...$columns): DatabaseQuery { $conn = $this->connect(); $query = new DatabaseQuery($conn); return $query->insert_into($insert, ...$columns); } - public function query($query_str) { + public function query(string $query_str): DatabaseQuery { $conn = $this->connect(); $query = new DatabaseQuery($conn); return $query->query($query_str); diff --git a/src/web/helpers/image.php b/src/web/helpers/image.php index c18154a..fd395b5 100644 --- a/src/web/helpers/image.php +++ b/src/web/helpers/image.php @@ -30,7 +30,7 @@ function __make_source( $media = ''; } $main = $GLOBALS['main_model']; - $path = $main->get_url('public/' . $name . '.' . $format); + $path = $main->get_url('public/' . $name . '.' . $format, TRUE); $mime = __get_mime($format); return sprintf('<source type="%s" srcset="%s" %s>', $mime, $path, $media); @@ -69,7 +69,7 @@ function image( $format = end($formats); $main = $GLOBALS['main_model']; - $path = $main->get_url('public/' . $name . '.' . $format); + $path = $main->get_url('public/' . $name . '.' . $format, TRUE); $out .= "<img src=\"$path\""; if ($alt) { $alt = lang($alt); diff --git a/src/web/helpers/lang.php b/src/web/helpers/lang.php index e8fa29e..b11cc7c 100644 --- a/src/web/helpers/lang.php +++ b/src/web/helpers/lang.php @@ -1,7 +1,13 @@ <?php /* Copyright (c) 2024 Freya Murphy */ $lang = array(); -function lang($key, $default = NULL, $sub = NULL) { +/** + * @param ?array<string,mixed> $sub + */ +function lang( + string $key, + ?string $default = NULL, + ?array $sub = NULL) { $lang = $GLOBALS['lang']; if(array_key_exists($key, $lang)) { if ($sub) { @@ -17,15 +23,20 @@ function lang($key, $default = NULL, $sub = NULL) { } } -function ilang($key, - $class = NULL, - $id = NULL, - $href = NULL, - $click = NULL, - $attrs = array(), - $sub = NULL, - $button = FALSE, - $container = 'span' +/** + * @param array<string,string> $attrs + * @param ?array<string,mixed> $sub + */ +function ilang( + string $key, + string $class = NULL, + string $id = NULL, + string $href = NULL, + string $click = NULL, + array $attrs = array(), + ?array $sub = NULL, + bool $button = FALSE, + string $container = 'span' ) { $text = ucfirst(lang($key . "_text", FALSE, sub: $sub)); $tip = lang($key . "_tip", FALSE, sub: $sub); diff --git a/src/web/helpers/markdown.php b/src/web/helpers/markdown.php index 39b430c..5279a1f 100644 --- a/src/web/helpers/markdown.php +++ b/src/web/helpers/markdown.php @@ -8,7 +8,10 @@ class MarkdownParser { $this->parsedown = new ParsedownExtra(); } - function parse($path) { + /** + * @return array<string,mixed> + */ + function parse(string $path): array { $content = file_get_contents($path); $data = array( 'meta' => array(), diff --git a/src/web/helpers/sanitize.php b/src/web/helpers/sanitize.php index 5d37852..495c249 100644 --- a/src/web/helpers/sanitize.php +++ b/src/web/helpers/sanitize.php @@ -1,6 +1,6 @@ <?php /* Copyright (c) 2024 Freya Murphy */ -function esc($data) { +function esc(string $data): string { $data = str_replace('&', '&', $data); $data = str_replace('<', '<', $data); $data = str_replace('>', '>', $data); |