summaryrefslogtreecommitdiffstats
path: root/core/namespace/Attribute.php
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2017-02-24 21:27:59 +0100
committerThomas Lange <code@nerdmind.de>2017-02-24 21:27:59 +0100
commit52b077a48c743ba4d08ac00520a0bf1ef6deef5f (patch)
treeb4205c194167e0e03e273957cdd0aab3be9fdf01 /core/namespace/Attribute.php
downloadblog-1.0.tar.gz
blog-1.0.tar.xz
blog-1.0.zip
Initial commit.v1.0
Diffstat (limited to 'core/namespace/Attribute.php')
-rw-r--r--core/namespace/Attribute.php74
1 files changed, 74 insertions, 0 deletions
diff --git a/core/namespace/Attribute.php b/core/namespace/Attribute.php
new file mode 100644
index 0000000..69998e0
--- /dev/null
+++ b/core/namespace/Attribute.php
@@ -0,0 +1,74 @@
+<?php
+abstract class Attribute implements AttributeInterface {
+
+ #===============================================================================
+ # Set attribute
+ #===============================================================================
+ public function set($attribute, $value) {
+ return $this->{$attribute} = $value;
+ }
+
+ #===============================================================================
+ # Get attribute
+ #===============================================================================
+ public function get($attribute) {
+ return $this->{$attribute} ?? NULL;
+ }
+
+ #===============================================================================
+ # Get array with not FALSE attributes
+ #===============================================================================
+ protected function getFilteredAttributes(): array {
+ return array_filter(get_object_vars($this), function($value) {
+ return $value !== FALSE;
+ });
+ }
+
+ #===============================================================================
+ # Insert database item
+ #===============================================================================
+ public function databaseINSERT(\Database $Database): bool {
+ $part[0] = '';
+ $part[1] = '';
+
+ $attributes = $this->getFilteredAttributes();
+
+ foreach($attributes as $column => $value) {
+ $part[0] .= "{$column},";
+ $part[1] .= '?,';
+ }
+
+ $part[0] = rtrim($part[0], ',');
+ $part[1] = rtrim($part[1], ',');
+
+ $Statement = $Database->prepare('INSERT INTO '.static::TABLE." ({$part[0]}) VALUES ({$part[1]})");
+ return $Statement->execute(array_values($attributes));
+ }
+
+ #===============================================================================
+ # Update database item
+ #===============================================================================
+ public function databaseUPDATE(\Database $Database): bool {
+ $part = '';
+
+ $attributes = $this->getFilteredAttributes();
+
+ foreach($attributes as $column => $value) {
+ $part .= "{$column} = ?,";
+ }
+
+ $part = rtrim($part, ',');
+
+ $Statement = $Database->prepare('UPDATE '.static::TABLE.' SET '.$part.' WHERE id = '.(int) $this->get('id'));
+ return $Statement->execute(array_values($attributes));
+ }
+
+ #===============================================================================
+ # Delete database item
+ #===============================================================================
+ public function databaseDELETE(\Database $Database): bool {
+ $Statement = $Database->prepare('DELETE FROM '.static::TABLE.' WHERE id = ?');
+ return $Statement->execute([$this->get('id')]);
+ }
+}
+?> \ No newline at end of file