aboutsummaryrefslogtreecommitdiffstats
path: root/core/namespace
diff options
context:
space:
mode:
Diffstat (limited to 'core/namespace')
-rw-r--r--core/namespace/Item.php30
-rw-r--r--core/namespace/ItemFactory.php14
-rw-r--r--core/namespace/ItemInterface.php2
-rw-r--r--core/namespace/Page/Item.php16
-rw-r--r--core/namespace/Post/Item.php16
5 files changed, 67 insertions, 11 deletions
diff --git a/core/namespace/Item.php b/core/namespace/Item.php
index 02de382..0a7d364 100644
--- a/core/namespace/Item.php
+++ b/core/namespace/Item.php
@@ -10,7 +10,7 @@ abstract class Item implements ItemInterface {
#===============================================================================
# Abstract item constructor
#===============================================================================
- public final function __construct($itemID, \Database $Database) {
+ public final function __construct($param, \Database $Database) {
$this->Database = $Database;
$this->Reflection = new ReflectionObject($this);
@@ -18,6 +18,17 @@ abstract class Item implements ItemInterface {
$attribute = "{$this->Reflection->getNamespaceName()}\\Attribute";
$exception = "{$this->Reflection->getNamespaceName()}\\Exception";
+ # If $param is an instance of Attribute,
+ # skip fetching attribute from database!
+ if($param instanceof Attribute) {
+ $this->Attribute = $param;
+ return;
+ }
+
+ # If this gets executed, then $param
+ # is not an instance of Attribute!
+ $itemID = $param;
+
#===============================================================================
# Checking if item in database exists
#===============================================================================
@@ -168,4 +179,21 @@ abstract class Item implements ItemInterface {
return 0;
}
+
+ #===============================================================================
+ # Return Attribute instance based on field comparison with value
+ #===============================================================================
+ public static function getByField($field, $value, \Database $Database): Attribute {
+ $exception = (new ReflectionClass(get_called_class()))->getNamespaceName().'\\Exception';
+ $attribute = (new ReflectionClass(get_called_class()))->getNamespaceName().'\\Attribute';
+ $Statement = $Database->prepare('SELECT * FROM '.$attribute::TABLE." WHERE {$field} = ?");
+
+ if($Statement->execute([$value])) {
+ if(!$Attribute = $Statement->fetchObject($attribute)) {
+ throw new $exception(sprintf("Item with value %s on field %s does not exist.", $value, $field));
+ }
+
+ return $Attribute;
+ }
+ }
}
diff --git a/core/namespace/ItemFactory.php b/core/namespace/ItemFactory.php
index d81ff9f..ba32cd5 100644
--- a/core/namespace/ItemFactory.php
+++ b/core/namespace/ItemFactory.php
@@ -18,6 +18,18 @@ abstract class ItemFactory extends Factory {
#===========================================================================
public static function buildBySlug($slug): Item {
$Item = (new ReflectionClass(get_called_class()))->getNamespaceName().'\\Item';
- return self::build($Item::getIDByField('slug', $slug, \Application::getDatabase()));
+ return self::buildByAttribute($Item::getByField('slug', $slug, \Application::getDatabase()));
+ }
+
+ #===========================================================================
+ # Build instance by Attribute
+ #===========================================================================
+ public static function buildByAttribute(Attribute $Attribute) {
+ if(!$Instance = parent::fetchInstance($Attribute->get('id'))) {
+ $Item = (new ReflectionClass(get_called_class()))->getNamespaceName().'\\Item';
+ $Instance = parent::storeInstance($Attribute->get('id'), new $Item($Attribute, \Application::getDatabase()));
+ }
+
+ return $Instance;
}
}
diff --git a/core/namespace/ItemInterface.php b/core/namespace/ItemInterface.php
index efee734..917db6a 100644
--- a/core/namespace/ItemInterface.php
+++ b/core/namespace/ItemInterface.php
@@ -1,4 +1,4 @@
<?php
interface ItemInterface {
- public function __construct($itemID, \Database $Database);
+ public function __construct($param, \Database $Database);
}
diff --git a/core/namespace/Page/Item.php b/core/namespace/Page/Item.php
index d559b81..d875e8a 100644
--- a/core/namespace/Page/Item.php
+++ b/core/namespace/Page/Item.php
@@ -27,16 +27,24 @@ class Item extends \Item {
}
#===============================================================================
- # Return unique page IDs for search results
+ # Return search results as Page\Attribute
#===============================================================================
- public static function getSearchResultIDs($search, \Database $Database): array {
- $Statement = $Database->prepare(sprintf("SELECT id FROM %s WHERE
+ public static function getSearchResults($search, \Database $Database): array {
+ $Statement = $Database->prepare(sprintf("SELECT * FROM %s WHERE
MATCH(name, body) AGAINST(? IN BOOLEAN MODE) LIMIT 20", Attribute::TABLE));
if($Statement->execute([$search])) {
- return $Statement->fetchAll($Database::FETCH_COLUMN);
+ return $Statement->fetchAll($Database::FETCH_CLASS, 'Page\Attribute');
}
return [];
}
+
+ #===============================================================================
+ # Return associated User\Attribute
+ #===============================================================================
+ public function getUserAttribute() {
+ $Statement = $this->Database->query(sprintf('SELECT * FROM user WHERE id = %d', $this->Attribute->get('user')));
+ return $Statement->fetchObject('User\Attribute');
+ }
}
diff --git a/core/namespace/Post/Item.php b/core/namespace/Post/Item.php
index 0f2c6a5..6109e20 100644
--- a/core/namespace/Post/Item.php
+++ b/core/namespace/Post/Item.php
@@ -27,23 +27,31 @@ class Item extends \Item {
}
#===============================================================================
- # Return unique post IDs for search results
+ # Return search results as Post\Attribute
#===============================================================================
- public static function getSearchResultIDs($search, array $date, \Database $Database): array {
+ public static function getSearchResults($search, array $date, \Database $Database): array {
$D = ($D = intval($date[0])) !== 0 ? $D : 'NULL';
$M = ($M = intval($date[1])) !== 0 ? $M : 'NULL';
$Y = ($Y = intval($date[2])) !== 0 ? $Y : 'NULL';
- $Statement = $Database->prepare(sprintf("SELECT id FROM %s WHERE
+ $Statement = $Database->prepare(sprintf("SELECT * FROM %s WHERE
({$Y} IS NULL OR YEAR(time_insert) = {$Y}) AND
({$M} IS NULL OR MONTH(time_insert) = {$M}) AND
({$D} IS NULL OR DAY(time_insert) = {$D}) AND
MATCH(name, body) AGAINST(? IN BOOLEAN MODE) LIMIT 20", Attribute::TABLE));
if($Statement->execute([$search])) {
- return $Statement->fetchAll($Database::FETCH_COLUMN);
+ return $Statement->fetchAll($Database::FETCH_CLASS, 'Post\Attribute');
}
return [];
}
+
+ #===============================================================================
+ # Return associated User\Attribute
+ #===============================================================================
+ public function getUserAttribute() {
+ $Statement = $this->Database->query(sprintf('SELECT * FROM user WHERE id = %d', $this->Attribute->get('user')));
+ return $Statement->fetchObject('User\Attribute');
+ }
}