diff options
author | vincent-peugnet <v.peugnet@free.fr> | 2020-04-28 14:20:10 +0200 |
---|---|---|
committer | vincent-peugnet <v.peugnet@free.fr> | 2020-04-28 20:21:34 +0200 |
commit | 98514a4e0037aa0879e45bbad660aeda8837c624 (patch) | |
tree | a574e7cf6a53150e1d960fc9b4520a49567b04bd /app/class/Item.php | |
parent | cba95c5eb19a33654a6f0995c6f9e0885b7afc20 (diff) | |
download | wcms-98514a4e0037aa0879e45bbad660aeda8837c624.tar.gz wcms-98514a4e0037aa0879e45bbad660aeda8837c624.zip |
add session as controller var
Diffstat (limited to 'app/class/Item.php')
-rw-r--r-- | app/class/Item.php | 71 |
1 files changed, 39 insertions, 32 deletions
diff --git a/app/class/Item.php b/app/class/Item.php index 44e383a..091516c 100644 --- a/app/class/Item.php +++ b/app/class/Item.php @@ -14,11 +14,39 @@ abstract class Item /** * Hydrate Object with corresponding `set__VAR__` * @param array|object $datas associative array using key as var name or object - * @param bool $sendexception throw exception if error setting variable * @return bool true if no error, otherwise false + */ + public function hydrate($datas = []): bool + { + $errors = $this->hydratejob($datas); + if (in_array(false, $errors)) { + return false; + } else { + return true; + } + } + + /** + * Hydrate Object with corresponding `set__VAR__` + * @param array|object $datas associative array using key as var name or object * @throws RuntimeException listing var settings errors */ - public function hydrate($datas = [], bool $sendexception = false): bool + public function hydrateexception($datas = []) + { + $errors = $this->hydratejob($datas); + if (!empty($errors)) { + $errors = implode(', ', $errors); + $class = get_class($this); + throw new RuntimeException("objects vars : $errors can't be set in $class object"); + } + } + + /** + * Concrete action of hydrate + * @param mixed $datas + * @return array associative array where key are methods and value is bool + */ + public function hydratejob($datas = []): array { $seterrors = []; if (is_array($datas) || is_object($datas)) { @@ -31,26 +59,23 @@ abstract class Item } } } - if (!empty($seterrors)) { - if ($sendexception) { - $errors = implode(', ', $seterrors); - $class = get_class($this); - throw new RuntimeException("objects vars : $errors can't be set in $class object"); - } - return false; - } else { - return true; - } + return $seterrors; } - public function dry() + /** + * Export whole object + * @return array Associative array + */ + public function dry(): array { $array = []; $array = $this->obj2array($this, $array); return $array; } - + /** + * Reccursive transform obj vars to array + */ public function obj2array($obj, &$arr) { if (!is_object($obj) && !is_array($obj)) { @@ -68,24 +93,6 @@ abstract class Item return $arr; } - public function dryold() - { - $array = []; - foreach (get_object_vars($this) as $var => $value) { - if (is_object($value) && is_subclass_of($value, get_class($this))) { - $array[$var] = $value->dry(); - } else { - if (method_exists($this, $var)) { - $array[$var] = $this->$var(); - } else { - $array[$var] = $value; - } - } - } - return get_object_vars($this); - // return $array; - } - /** * Return any asked vars and their values of an object as associative array |