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/Dbitem.php | 34 ++++++-------- app/class/Element.php | 13 +----- app/class/Item.php | 50 ++++++++++++++++++++ app/class/Media.php | 14 +----- app/class/Medialist.php | 13 +----- app/class/Opt.php | 26 +---------- app/class/Page.php | 26 +---------- app/class/Quickcss.php | 14 +----- app/class/Record.php | 118 ------------------------------------------------ app/class/Route.php | 12 +---- app/class/User.php | 23 +--------- 11 files changed, 72 insertions(+), 271 deletions(-) create mode 100644 app/class/Item.php delete mode 100644 app/class/Record.php (limited to 'app/class') diff --git a/app/class/Dbitem.php b/app/class/Dbitem.php index fb272b1..34814d2 100644 --- a/app/class/Dbitem.php +++ b/app/class/Dbitem.php @@ -2,33 +2,25 @@ namespace Wcms; -class Dbitem +use DateTime; +use DateTimeImmutable; + +abstract class Dbitem extends 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) { + if($value instanceof DateTime || $value instanceof DateTimeImmutable) { + $array[$var] = $this->$var('string'); + } else { + $array[$var] = $this->$var(); } } - - } - - public function dry() - { - $array = []; - foreach (get_object_vars($this) as $var => $value) { - if (in_array($var, $this::VAR_DATE)) { - $array[$var] = $this->$var('string'); - } else { - $array[$var] = $this->$var(); - } - } return $array; } -} +} ?> \ No newline at end of file diff --git a/app/class/Element.php b/app/class/Element.php index 72eba8f..a08e219 100644 --- a/app/class/Element.php +++ b/app/class/Element.php @@ -4,7 +4,7 @@ namespace Wcms; use Exception; -class Element +class Element extends Item { protected $fullmatch; protected $type; @@ -29,17 +29,6 @@ class Element $this->analyse($pageid); } - public function hydrate($datas) - { - foreach ($datas as $key => $value) { - $method = 'set' . $key; - - if (method_exists($this, $method)) { - $this->$method($value); - } - } - } - private function analyse(string $pageid) { if(!empty($this->options)) { 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 diff --git a/app/class/Media.php b/app/class/Media.php index 0be2013..5d61bf1 100644 --- a/app/class/Media.php +++ b/app/class/Media.php @@ -2,7 +2,7 @@ namespace Wcms; -class Media +class Media extends Item { private $id; private $path; @@ -27,18 +27,6 @@ class Media $this->hydrate($donnees); } - public function hydrate(array $donnees) - { - foreach ($donnees as $key => $value) { - $method = 'set' . $key; - - if (method_exists($this, $method)) { - $this->$method($value); - } - } - } - - public function analyse() { $this->settype(); diff --git a/app/class/Medialist.php b/app/class/Medialist.php index 2c17fa0..ab1dea3 100644 --- a/app/class/Medialist.php +++ b/app/class/Medialist.php @@ -2,7 +2,7 @@ namespace Wcms; -class Medialist +class Medialist extends Item { /** @var string full regex match */ protected $fullmatch; @@ -46,17 +46,6 @@ class Medialist $this->hydrate($datas); } - public function hydrate($datas) - { - foreach ($datas as $key => $value) { - $method = 'set' . $key; - - if (method_exists($this, $method)) { - $this->$method($value); - } - } - } - public function readfilter() { parse_str($this->filter, $datas); diff --git a/app/class/Opt.php b/app/class/Opt.php index cbbac02..60dfb65 100644 --- a/app/class/Opt.php +++ b/app/class/Opt.php @@ -2,7 +2,7 @@ namespace Wcms; -class Opt +class Opt extends Item { protected $sortby = 'id'; protected $order = 1; @@ -26,32 +26,8 @@ class Opt $this->hydrate($donnees); } - public function hydrate(array $donnees) - { - foreach ($donnees as $key => $value) { - $method = 'set' . $key; - if (method_exists($this, $method)) { - $this->$method($value); - } - } - } - /** - * 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; - } public function resetall() diff --git a/app/class/Page.php b/app/class/Page.php index 170a057..82e61c1 100644 --- a/app/class/Page.php +++ b/app/class/Page.php @@ -6,7 +6,7 @@ use DateTime; use DateTimeImmutable; use DateTimeZone; -class Page +class Page extends Dbitem { protected $id; protected $title; @@ -63,17 +63,6 @@ class Page $this->hydrate($datas); } - public function hydrate($datas) - { - foreach ($datas as $key => $value) { - $method = 'set' . $key; - - if (method_exists($this, $method)) { - $this->$method($value); - } - } - } - public function reset() { $now = new DateTimeImmutable(null, timezone_open("Europe/Paris")); @@ -125,19 +114,6 @@ class Page return ['pagevarlist' => $classvarlist]; } - public function dry() - { - $array = []; - foreach (get_class_vars(__class__) as $var => $value) { - if (in_array($var, self::VAR_DATE)) { - $array[$var] = $this->$var('string'); - } else { - $array[$var] = $this->$var(); - } - } - return $array; - } - /** * Check if page have a thumbnail * @return bool true if the page have a thumbnail otherwise return false. diff --git a/app/class/Quickcss.php b/app/class/Quickcss.php index 0173763..b55a57b 100644 --- a/app/class/Quickcss.php +++ b/app/class/Quickcss.php @@ -2,7 +2,7 @@ namespace Wcms; -class Quickcss +class Quickcss extends Item { @@ -41,17 +41,7 @@ class Quickcss public function __construct($data) { - $this->hydrate($data); } - - public function hydrate($data) - { - foreach ($data as $key => $value) { - $method = 'set' . $key; - - if (method_exists($this, $method)) { - $this->$method($value); - } - } + $this->hydrate($data); } public function calc() diff --git a/app/class/Record.php b/app/class/Record.php deleted file mode 100644 index 381f10a..0000000 --- a/app/class/Record.php +++ /dev/null @@ -1,118 +0,0 @@ -hydrate($donnees); - } - - public function hydrate(array $donnees) - { - foreach ($donnees as $key => $value) { - $method = 'set' . $key; - - if (method_exists($this, $method)) { - $this->$method($value); - } - } - } - - - -// _________________________________________________ G E T ____________________________________________________ - - public function id() - { - return $this->id; - } - - public function path() - { - return $this->path; - } - - public function extension() - { - return $this->extension; - } - - public function size() - { - return $this->size; - } - - public function datetime() - { - return $this->datetime; - } - - public function number() - { - return $this->number; - } - -// ___________________________________________________ S E T __________________________________________________ - - public function setid($id) - { - if (strlen($id) < 100 and is_string($id)) { - $this->id = strip_tags(strtolower($id)); - } - } - - public function setpath($path) - { - if (strlen($path) < 40 and is_string($path)) { - $this->path = strip_tags(strtolower($path)); - } - } - - public function setextension($extension) - { - if (strlen($extension) < 7 and is_string($extension)) { - $this->extension = strip_tags(strtolower($extension)); - } - } - - public function setsize($size) - { - if (40 and is_int($size)) { - $this->size = strip_tags(strtolower($size)); - } - } - - public function setdatetime($datetime) - { - if (is_int($datetime)) { - $this->datetime = strip_tags(strtolower($datetime)); - } - } - - public function setnumber($number) - { - if (is_int($number)) { - $this->number = strip_tags(strtolower($number)); - } - } - - - - - -} - -?> \ No newline at end of file diff --git a/app/class/Route.php b/app/class/Route.php index 4f2a6de..32dfaf5 100644 --- a/app/class/Route.php +++ b/app/class/Route.php @@ -2,7 +2,7 @@ namespace Wcms; -class Route +class Route extends Item { protected $id = null; protected $aff = null; @@ -16,16 +16,6 @@ class Route $this->hydrate($vars); } - public function hydrate($vars) - { - foreach ($vars as $var => $value) { - $method = 'set' . $var; - if (method_exists($this, $method)) { - $this->$method($value); - } - } - } - public function toarray() { $array = []; diff --git a/app/class/User.php b/app/class/User.php index 8712c72..b735309 100644 --- a/app/class/User.php +++ b/app/class/User.php @@ -5,7 +5,7 @@ namespace Wcms; use DateTimeImmutable; use DateTimeZone; -class User +class User extends Item { protected $id; protected $level = 0; @@ -26,27 +26,6 @@ class User } } - 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 (get_class_vars(__class__) as $var => $value) { - $array[$var] = $this->$var(); - } - return $array; - } - - // _________________________ G E T _______________________ public function id() -- cgit v1.2.3