aboutsummaryrefslogtreecommitdiffstats
path: root/core/namespace/ORM
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2021-06-25 22:13:59 +0200
committerThomas Lange <code@nerdmind.de>2021-06-25 22:13:59 +0200
commita86bdc6b5bab78b4026f44161413f4e482df42e4 (patch)
tree76ffd745780671371272e97b3eeac85e154e236e /core/namespace/ORM
parent366d0de19c012821ef587d5fdb9557c339b43303 (diff)
downloadblog-a86bdc6b5bab78b4026f44161413f4e482df42e4.tar.gz
blog-a86bdc6b5bab78b4026f44161413f4e482df42e4.tar.xz
blog-a86bdc6b5bab78b4026f44161413f4e482df42e4.zip
Reorganize namespaces
Diffstat (limited to 'core/namespace/ORM')
-rw-r--r--core/namespace/ORM/Entities/Page.php14
-rw-r--r--core/namespace/ORM/Entities/Post.php14
-rw-r--r--core/namespace/ORM/Entities/User.php16
-rw-r--r--core/namespace/ORM/Entity.php49
-rw-r--r--core/namespace/ORM/EntityInterface.php10
-rw-r--r--core/namespace/ORM/Repositories/Page.php19
-rw-r--r--core/namespace/ORM/Repositories/Post.php19
-rw-r--r--core/namespace/ORM/Repositories/User.php8
-rw-r--r--core/namespace/ORM/Repository.php326
9 files changed, 475 insertions, 0 deletions
diff --git a/core/namespace/ORM/Entities/Page.php b/core/namespace/ORM/Entities/Page.php
new file mode 100644
index 0000000..94e5ff5
--- /dev/null
+++ b/core/namespace/ORM/Entities/Page.php
@@ -0,0 +1,14 @@
+<?php
+namespace ORM\Entities;
+use ORM\Entity;
+
+class Page extends Entity {
+ protected $id = FALSE;
+ protected $user = FALSE;
+ protected $slug = FALSE;
+ protected $name = FALSE;
+ protected $body = FALSE;
+ protected $argv = FALSE;
+ protected $time_insert = FALSE;
+ protected $time_update = FALSE;
+}
diff --git a/core/namespace/ORM/Entities/Post.php b/core/namespace/ORM/Entities/Post.php
new file mode 100644
index 0000000..e6bff36
--- /dev/null
+++ b/core/namespace/ORM/Entities/Post.php
@@ -0,0 +1,14 @@
+<?php
+namespace ORM\Entities;
+use ORM\Entity;
+
+class Post extends Entity {
+ protected $id = FALSE;
+ protected $user = FALSE;
+ protected $slug = FALSE;
+ protected $name = FALSE;
+ protected $body = FALSE;
+ protected $argv = FALSE;
+ protected $time_insert = FALSE;
+ protected $time_update = FALSE;
+}
diff --git a/core/namespace/ORM/Entities/User.php b/core/namespace/ORM/Entities/User.php
new file mode 100644
index 0000000..f0c9a1f
--- /dev/null
+++ b/core/namespace/ORM/Entities/User.php
@@ -0,0 +1,16 @@
+<?php
+namespace ORM\Entities;
+use ORM\Entity;
+
+class User extends Entity {
+ protected $id = FALSE;
+ protected $slug = FALSE;
+ protected $username = FALSE;
+ protected $password = FALSE;
+ protected $fullname = FALSE;
+ protected $mailaddr = FALSE;
+ protected $body = FALSE;
+ protected $argv = FALSE;
+ protected $time_insert = FALSE;
+ protected $time_update = FALSE;
+}
diff --git a/core/namespace/ORM/Entity.php b/core/namespace/ORM/Entity.php
new file mode 100644
index 0000000..9cc7755
--- /dev/null
+++ b/core/namespace/ORM/Entity.php
@@ -0,0 +1,49 @@
+<?php
+namespace ORM;
+
+abstract class Entity implements EntityInterface {
+ protected $id;
+ protected $time_insert;
+ protected $time_update;
+
+ #===============================================================================
+ # Get attribute
+ #===============================================================================
+ public function get(string $attribute) {
+ return $this->{$attribute} ?? NULL;
+ }
+
+ #===============================================================================
+ # Set attribute
+ #===============================================================================
+ public function set(string $attribute, $value) {
+ return $this->{$attribute} = $value;
+ }
+
+ #===============================================================================
+ # Return ID
+ #===============================================================================
+ final public function getID(): int {
+ return $this->id;
+ }
+
+ #===============================================================================
+ # Get all attributes
+ #===============================================================================
+ public function getAll(array $exclude = []): array {
+ $attributes = get_object_vars($this);
+
+ return array_filter($attributes, function($attribute) use($exclude) {
+ return !in_array($attribute, $exclude);
+ }, ARRAY_FILTER_USE_KEY);
+ }
+
+ #===============================================================================
+ # Get array with all non-false attributes
+ #===============================================================================
+ public function getFilteredAttributes(): array {
+ return array_filter(get_object_vars($this), function($value) {
+ return $value !== FALSE;
+ });
+ }
+}
diff --git a/core/namespace/ORM/EntityInterface.php b/core/namespace/ORM/EntityInterface.php
new file mode 100644
index 0000000..68c9588
--- /dev/null
+++ b/core/namespace/ORM/EntityInterface.php
@@ -0,0 +1,10 @@
+<?php
+namespace ORM;
+
+interface EntityInterface {
+ public function get(string $attribute);
+ public function set(string $attribute, $value);
+
+ public function getID(): int;
+ public function getAll(array $exclude = []): array;
+}
diff --git a/core/namespace/ORM/Repositories/Page.php b/core/namespace/ORM/Repositories/Page.php
new file mode 100644
index 0000000..595b420
--- /dev/null
+++ b/core/namespace/ORM/Repositories/Page.php
@@ -0,0 +1,19 @@
+<?php
+namespace ORM\Repositories;
+use ORM\Repository;
+use ORM\Entities\User;
+
+class Page extends Repository {
+ public static function getTableName(): string { return 'page'; }
+ public static function getClassName(): string { return 'ORM\Entities\Page'; }
+
+ public function getCountByUser(User $User): int {
+ $query = 'SELECT COUNT(id) FROM %s WHERE user = ?';
+ $query = sprintf($query, static::getTableName());
+
+ $Statement = $this->Database->prepare($query);
+ $Statement->execute([$User->getID()]);
+
+ return $Statement->fetchColumn();
+ }
+}
diff --git a/core/namespace/ORM/Repositories/Post.php b/core/namespace/ORM/Repositories/Post.php
new file mode 100644
index 0000000..8eac12f
--- /dev/null
+++ b/core/namespace/ORM/Repositories/Post.php
@@ -0,0 +1,19 @@
+<?php
+namespace ORM\Repositories;
+use ORM\Repository;
+use ORM\Entities\User;
+
+class Post extends Repository {
+ public static function getTableName(): string { return 'post'; }
+ public static function getClassName(): string { return 'ORM\Entities\Post'; }
+
+ public function getCountByUser(User $User): int {
+ $query = 'SELECT COUNT(id) FROM %s WHERE user = ?';
+ $query = sprintf($query, static::getTableName());
+
+ $Statement = $this->Database->prepare($query);
+ $Statement->execute([$User->getID()]);
+
+ return $Statement->fetchColumn();
+ }
+}
diff --git a/core/namespace/ORM/Repositories/User.php b/core/namespace/ORM/Repositories/User.php
new file mode 100644
index 0000000..629d9c1
--- /dev/null
+++ b/core/namespace/ORM/Repositories/User.php
@@ -0,0 +1,8 @@
+<?php
+namespace ORM\Repositories;
+use ORM\Repository;
+
+class User extends Repository {
+ public static function getTableName(): string { return 'user'; }
+ public static function getClassName(): string { return 'ORM\Entities\User'; }
+}
diff --git a/core/namespace/ORM/Repository.php b/core/namespace/ORM/Repository.php
new file mode 100644
index 0000000..507605c
--- /dev/null
+++ b/core/namespace/ORM/Repository.php
@@ -0,0 +1,326 @@
+<?php
+namespace ORM;
+use Database;
+
+abstract class Repository {
+ protected $Database;
+ protected $entities = [];
+
+ abstract public static function getTableName(): string;
+ abstract public static function getClassName(): string;
+
+ public function __construct(Database $Database) {
+ $this->Database = $Database;
+ }
+
+ #===============================================================================
+ # Adds an entity to the runtime cache
+ #===============================================================================
+ protected function storeInstance(int $identifier, EntityInterface $Entity) {
+ return $this->entities[$identifier] = $Entity;
+ }
+
+ #===============================================================================
+ # Adds an array of entities to the runtime cache
+ #===============================================================================
+ protected function storeMultipleInstances(array $entities) {
+ foreach($entities as $Entity) {
+ $this->storeInstance($Entity->getID(), $Entity);
+ }
+
+ return $entities;
+ }
+
+ #===============================================================================
+ # Gets an entity from the runtime cache
+ #===============================================================================
+ protected function fetchInstance(int $identifier) {
+ return $this->entities[$identifier] ?? FALSE;
+ }
+
+ #===============================================================================
+ # Removes an entity from the runtime cache
+ #===============================================================================
+ protected function removeInstance(int $identifier) {
+ if(isset($this->cache[$identifier])) {
+ unset($this->cache[$identifier]);
+ }
+ }
+
+ #===========================================================================
+ # Insert entity
+ #===========================================================================
+ public function insert(EntityInterface $Entity): bool {
+ $attributes = $Entity->getFilteredAttributes();
+
+ foreach($attributes as $field => $value) {
+ $fields[] = $field;
+ $values[] = '?';
+ }
+
+ $fields = implode(', ', $fields ?? []);
+ $values = implode(', ', $values ?? []);
+
+ $query = 'INSERT INTO %s (%s) VALUES(%s)';
+ $query = sprintf($query, static::getTableName(), $fields, $values);
+
+ $Statement = $this->Database->prepare($query);
+ return $Statement->execute(array_values($attributes));
+ }
+
+ #===========================================================================
+ # Update entity
+ #===========================================================================
+ public function update(EntityInterface $Entity): bool {
+ $attributes = $Entity->getFilteredAttributes();
+
+ foreach($attributes as $field => $value) {
+ $params[] = "$field = ?";
+ }
+
+ $params = implode(', ', $params ?? []);
+
+ $query = 'UPDATE %s SET %s WHERE id = '.intval($Entity->getID());
+ $query = sprintf($query, static::getTableName(), $params);
+
+ $Statement = $this->Database->prepare($query);
+ return $Statement->execute(array_values($attributes));
+ }
+
+ #===========================================================================
+ # Delete entity
+ #===========================================================================
+ public function delete(EntityInterface $Entity): bool {
+ $query = 'DELETE FROM %s WHERE id = ?';
+ $query = sprintf($query, static::getTableName());
+
+ $Statement = $this->Database->prepare($query);
+ return $Statement->execute([$Entity->getID()]);
+ }
+
+ #===========================================================================
+ # Find entity based on primary key
+ #===========================================================================
+ public function find(int $id): ?EntityInterface {
+ if($Entity = $this->fetchInstance($id)) {
+ return $Entity;
+ }
+
+ return $this->findBy('id', $id);
+ }
+
+ #===============================================================================
+ # Find entity based on specific field comparison
+ #===============================================================================
+ public function findBy(string $field, $value): ?EntityInterface {
+ $query = 'SELECT * FROM %s WHERE %s = ?';
+ $query = sprintf($query, static::getTableName(), $field);
+
+ $Statement = $this->Database->prepare($query);
+ $Statement->execute([$value]);
+
+ if($Entity = $Statement->fetchObject(static::getClassName())) {
+ $this->storeInstance($Entity->getID(), $Entity);
+ return $Entity;
+ }
+
+ return NULL;
+ }
+
+ #===============================================================================
+ # Find previous entity
+ #===============================================================================
+ public function findPrev(EntityInterface $Entity): ?EntityInterface {
+ $query = 'SELECT * FROM %s WHERE time_insert < ? ORDER BY time_insert DESC LIMIT 1';
+ $query = sprintf($query, static::getTableName());
+
+ $Statement = $this->Database->prepare($query);
+ $Statement->execute([$Entity->get('time_insert')]);
+
+ if($Entity = $Statement->fetchObject(static::getClassName())) {
+ $this->storeInstance($Entity->getID(), $Entity);
+ return $Entity;
+ }
+
+ return NULL;
+ }
+
+ #===============================================================================
+ # Find next entity
+ #===============================================================================
+ public function findNext(EntityInterface $Entity): ?EntityInterface {
+ $query = 'SELECT * FROM %s WHERE time_insert > ? ORDER BY time_insert ASC LIMIT 1';
+ $query = sprintf($query, static::getTableName());
+
+ $Statement = $this->Database->prepare($query);
+ $Statement->execute([$Entity->get('time_insert')]);
+
+ if($Entity = $Statement->fetchObject(static::getClassName())) {
+ $this->storeInstance($Entity->getID(), $Entity);
+ return $Entity;
+ }
+
+ return NULL;
+ }
+
+ #===========================================================================
+ # Find last (which means the newest) entity
+ #===========================================================================
+ public function getLast(): ?EntityInterface {
+ $query = 'SELECT * FROM %s ORDER BY time_insert DESC LIMIT 1';
+ $query = sprintf($query, static::getTableName());
+
+ $Statement = $this->Database->query($query);
+
+ if($Entity = $Statement->fetchObject(static::getClassName())) {
+ $this->storeInstance($Entity->getID(), $Entity);
+ return $Entity;
+ }
+
+ return NULL;
+ }
+
+ #===========================================================================
+ # Get entity count
+ #===========================================================================
+ public function getCount(): int {
+ $query = 'SELECT COUNT(id) FROM %s';
+ $query = sprintf($query, static::getTableName());
+
+ return $this->Database->query($query)->fetchColumn();
+ }
+
+ #===========================================================================
+ # Get paginated entity list
+ #===========================================================================
+ public function getPaginated(string $order, int $limit, int $offset = 0): array {
+ return $this->getAll([], $order, "$offset,$limit");
+ }
+
+ #===========================================================================
+ # Get all entities
+ #===========================================================================
+ public function getAll(array $filter = [], string $order = null, string $limit = null): array {
+ $select = 'SELECT * FROM '.static::getTableName();
+ $wheres = [];
+ $params = [];
+
+ if(!empty($filter)) {
+ foreach($filter as $column => $value) {
+ $wheres[] = "$column = ?";
+ $params[] = $value;
+ }
+
+ $where = 'WHERE '.implode(' AND ', $wheres);
+ }
+
+ if($order) {
+ $order = "ORDER BY $order";
+ }
+
+ if($limit) {
+ $limit = "LIMIT $limit";
+ }
+
+ $query = "$select %s %s %s";
+ $query = sprintf($query, $where ?? '', $order ?? '', $limit ?? '');
+
+ $Statement = $this->Database->prepare($query);
+ $Statement->execute($params);
+
+ if($entities = $Statement->fetchAll($this->Database::FETCH_CLASS, static::getClassName())) {
+ $this->storeMultipleInstances($entities);
+ return $entities;
+ }
+
+ return [];
+ }
+
+ #===============================================================================
+ # Get entities based on search query
+ #===============================================================================
+ public function search(string $search, array $filter = []): array {
+ if($search === '*') {
+ return $this->getAll([], NULL, 20);
+ }
+
+ if(strlen($filter['year'] ?? '') !== 0) {
+ $extend[] = 'YEAR(time_insert) = ? AND';
+ $params[] = $filter['year'];
+ }
+
+ if(strlen($filter['month'] ?? '') !== 0) {
+ $extend[] = 'MONTH(time_insert) = ? AND';
+ $params[] = $filter['month'];
+ }
+
+ if(strlen($filter['day'] ?? '') !== 0) {
+ $extend[] = 'DAY(time_insert) = ? AND';
+ $params[] = $filter['day'];
+ }
+
+ $dateparts = implode(' ', $extend ?? []);
+
+ $query = 'SELECT * FROM %s WHERE %s MATCH(name, body)
+ AGAINST(? IN BOOLEAN MODE) LIMIT 20';
+ $query = sprintf($query, static::getTableName(), $dateparts);
+
+ $Statement = $this->Database->prepare($query);
+ $Statement->execute(array_merge($params ?? [], [$search]));
+
+ if($entities = $Statement->fetchAll($this->Database::FETCH_CLASS, static::getClassName())) {
+ $this->storeMultipleInstances($entities);
+ return $entities;
+ }
+
+ return [];
+ }
+
+ #===============================================================================
+ # Get a list of distinct days
+ #===============================================================================
+ public function getDistinctDays(): array {
+ $query = 'SELECT DISTINCT DAY(time_insert) AS d FROM %s ORDER BY d';
+ $query = sprintf($query, static::getTableName());
+
+ $Statement = $this->Database->query($query);
+
+ if($result = $Statement->fetchAll($this->Database::FETCH_COLUMN)) {
+ return $result;
+ }
+
+ return [];
+ }
+
+ #===============================================================================
+ # Get a list of distinct months
+ #===============================================================================
+ public function getDistinctMonths(): array {
+ $query = 'SELECT DISTINCT MONTH(time_insert) AS m FROM %s ORDER BY m';
+ $query = sprintf($query, static::getTableName());
+
+ $Statement = $this->Database->query($query);
+
+ if($result = $Statement->fetchAll($this->Database::FETCH_COLUMN)) {
+ return $result;
+ }
+
+ return [];
+ }
+
+ #===============================================================================
+ # Get a list of distinct years
+ #===============================================================================
+ public function getDistinctYears(): array {
+ $query = 'SELECT DISTINCT YEAR(time_insert) AS y FROM %s ORDER BY y';
+ $query = sprintf($query, static::getTableName());
+
+ $Statement = $this->Database->query($query);
+
+ if($result = $Statement->fetchAll($this->Database::FETCH_COLUMN)) {
+ return $result;
+ }
+
+ return [];
+ }
+}