diff options
author | vincent-peugnet <v.peugnet@free.fr> | 2020-04-27 13:35:29 +0200 |
---|---|---|
committer | vincent-peugnet <v.peugnet@free.fr> | 2020-04-28 20:21:34 +0200 |
commit | cba95c5eb19a33654a6f0995c6f9e0885b7afc20 (patch) | |
tree | a334a0fa3c074f44fe41a4114ea1853ac7f6c0e3 /app/class/Item.php | |
parent | c832f91ca41490f69f478045c86038f9ef4a5cb5 (diff) | |
download | wcms-cba95c5eb19a33654a6f0995c6f9e0885b7afc20.tar.gz wcms-cba95c5eb19a33654a6f0995c6f9e0885b7afc20.zip |
fix password max size
add error in hydrate
Diffstat (limited to 'app/class/Item.php')
-rw-r--r-- | app/class/Item.php | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/app/class/Item.php b/app/class/Item.php index 76fa78e..44e383a 100644 --- a/app/class/Item.php +++ b/app/class/Item.php @@ -5,24 +5,38 @@ namespace Wcms; use DateTime; use DateTimeImmutable; use DateTimeZone; +use InvalidArgumentException; +use RuntimeException; abstract class Item { - - public function hydrate($datas = []) + /** + * 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 + * @throws RuntimeException listing var settings errors + */ + public function hydrate($datas = [], bool $sendexception = false): bool { - $error = 0; - foreach ($datas as $key => $value) { - $method = 'set' . $key; - - if (method_exists($this, $method)) { - if ($this->$method($value) === false) { - $error++; + $seterrors = []; + if (is_array($datas) || is_object($datas)) { + foreach ($datas as $key => $value) { + $method = 'set' . $key; + if (method_exists($this, $method)) { + if ($this->$method($value) === false) { + $seterrors[] = $key; + } } } } - if ($error > 0) { + 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; |