From 8a2c577823b69117af8eda9b1a46bfbcae8153c6 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Mon, 8 Jul 2024 17:22:30 -0400 Subject: a few fixes, just a few.... --- src/web/helpers/aria.php | 2 +- src/web/helpers/database.php | 80 ++++++++++++++++++++++++++------------------ src/web/helpers/image.php | 4 +-- src/web/helpers/lang.php | 31 +++++++++++------ src/web/helpers/markdown.php | 5 ++- src/web/helpers/sanitize.php | 2 +- 6 files changed, 76 insertions(+), 48 deletions(-) (limited to 'src/web/helpers') 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 @@ 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 $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 $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 '
>> caused by <<
'; echo str_replace("\n", "
", $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('', $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 .= " $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 $attrs + * @param ?array $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 + */ + 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 @@ ', '>', $data); -- cgit v1.2.3-freya