aboutsummaryrefslogtreecommitdiff
path: root/app/class/Item.php
diff options
context:
space:
mode:
authorvincent-peugnet <v.peugnet@free.fr>2020-01-29 02:34:50 +0100
committervincent-peugnet <v.peugnet@free.fr>2020-01-29 02:34:50 +0100
commit64ac1605b6fe8e997134dd57cc30f30b99ebc076 (patch)
treed4fd89c68f0f0eb7b49ea92336f8f8ab5f778e45 /app/class/Item.php
parentb6173772bdef1eaf067573373ab500d67dfc454d (diff)
downloadwcms-64ac1605b6fe8e997134dd57cc30f30b99ebc076.tar.gz
wcms-64ac1605b6fe8e997134dd57cc30f30b99ebc076.zip
refactor : item abstract class
new Item abstract class with hydrate and dry functions to clean others
Diffstat (limited to 'app/class/Item.php')
-rw-r--r--app/class/Item.php50
1 files changed, 50 insertions, 0 deletions
diff --git a/app/class/Item.php b/app/class/Item.php
new file mode 100644
index 0000000..526e630
--- /dev/null
+++ b/app/class/Item.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Wcms;
+
+abstract class Item
+{
+
+
+ public function hydrate($datas = [])
+ {
+ foreach ($datas as $key => $value) {
+ $method = 'set' . $key;
+
+ if (method_exists($this, $method)) {
+ $this->$method($value);
+ }
+ }
+ }
+
+
+ public function dry()
+ {
+ $array = [];
+ foreach ($this as $var => $value) {
+ $array[$var] = $this->$var();
+ }
+ return $array;
+ }
+
+
+ /**
+ * Return any asked vars and their values of an object as associative array
+ *
+ * @param array $vars list of vars
+ * @return array Associative array `$var => $value`
+ */
+ public function drylist(array $vars) : array
+ {
+ $array = [];
+ foreach ($vars as $var) {
+ if (property_exists($this, $var))
+ $array[$var] = $this->$var;
+ }
+ return $array;
+ }
+
+}
+
+
+?> \ No newline at end of file