From 64ac1605b6fe8e997134dd57cc30f30b99ebc076 Mon Sep 17 00:00:00 2001 From: vincent-peugnet Date: Wed, 29 Jan 2020 02:34:50 +0100 Subject: refactor : item abstract class new Item abstract class with hydrate and dry functions to clean others --- app/class/Item.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 app/class/Item.php (limited to 'app/class/Item.php') 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 @@ + $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 -- cgit v1.2.3