From fbf9c49322483df6d9fc04264c2e9080e7090098 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Sat, 14 Dec 2019 04:39:51 +0300 Subject: [PATCH 1/2] + `\DDTools\BaseClass::setExistingProps`: Can set properties of all parent and child classes. --- src/BaseClass/BaseClass.php | 80 ++++++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 18 deletions(-) diff --git a/src/BaseClass/BaseClass.php b/src/BaseClass/BaseClass.php index 1abef8c..fd7da0a 100644 --- a/src/BaseClass/BaseClass.php +++ b/src/BaseClass/BaseClass.php @@ -4,7 +4,7 @@ class BaseClass { /** * setExistingProps - * @version 1.2 (2019-08-21) + * @version 1.3 (2019-12-14) * * @desc Sets existing object properties. * @@ -20,23 +20,67 @@ public function setExistingProps($props){ $propName => $propValue ){ - if (property_exists( - $this, - $propName - )){ - $setProp = function( - $propName, - $propValue - ){ - $this->{$propName} = $propValue; - }; - - //Access to private properties too - $setProp->call( - $this, - $propName, - $propValue - ); + $this->setProp([ + 'object' => $this, + 'propName' => $propName, + 'propValue' => $propValue + ]); + } + } + + /** + * setProp + * @version 1.0 (2019-09-23) + * + * @throws \ReflectionException + * @throws \Exception + * + * @param $params {array_associative|stdClass} — The object of params. @required + * @param $params->object {object} — Объект для модификации. @required + * @param $params->propName {string} — Имя поля. @required + * @param $params->propValue {mixed} — Значение. @required + * @param $params->class {string|object|null} — Класс или объект. Default: null. + * + * @return {void} + */ + private function setProp($params){ + //Defaults + $params = (object) array_merge( + [ + 'class' => null + ], + (array) $params + ); + + if ($params->class === null){ + $params->class = get_class($params->object); + } + + $classReflection = new \ReflectionClass($params->class); + + if ($classReflection->hasProperty($params->propName)){ + $reflectionProperty = $classReflection->getProperty($params->propName); + + if (!$reflectionProperty->isPublic()){ + $reflectionProperty->setAccessible(true); + } + + $reflectionProperty->setValue( + $params->object, + $params->propValue + ); + }else{ + $parent = $classReflection->getParentClass(); + + if ($parent !== false){ + $this->setProp([ + 'object' => $params->object, + 'propName' => $params->propName, + 'propValue' => $params->propValue, + 'class' => $parent->getName() + ]); + }else{ + throw new \Exception('Property “' . $params->propName . '” not found'); } } } From 3bc0cbfe72ed340b3330804ce494368ee54bfc89 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Sat, 14 Dec 2019 04:42:52 +0300 Subject: [PATCH 2/2] Prerelease --- CHANGELOG.md | 4 ++++ composer.json | 2 +- modx.ddtools.class.php | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f98dae0..95349e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # (MODX)EvolutionCMS.libraries.ddTools changelog +## Version 0.29 (2019-12-14) +* \+ `\DDTools\BaseClass::setExistingProps`: Can set properties of all parent and child classes. + + ## Version 0.28 (2019-10-22) * \+ `\DDTools\FilesTools::createDir` (and `\ddTools::createDir` as alias): Makes directory using `$modx->config['new_folder_permissions']`. Nested directories will be created too. Doesn't throw an exception if the folder already exists. diff --git a/composer.json b/composer.json index 5e7d074..7e6d6c5 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "dd/modxevo-library-ddtools", "type": "modxevo-library-ddtools", - "version": "0.28.0", + "version": "0.29.0", "description": "A library with various tools facilitating your work.", "keywords": [ "modx", diff --git a/modx.ddtools.class.php b/modx.ddtools.class.php index d0b7006..58c5ed3 100644 --- a/modx.ddtools.class.php +++ b/modx.ddtools.class.php @@ -1,7 +1,7 @@