Skip to content

Commit

Permalink
Version 0.29
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronef committed Dec 14, 2019
2 parents 50fd62c + 3bc0cbf commit df46b80
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion modx.ddtools.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* EvolutionCMS.libraries.ddTools
* @version 0.28 (2019-10-22)
* @version 0.29 (2019-12-14)
*
* @see README.md
*
Expand Down
80 changes: 62 additions & 18 deletions src/BaseClass/BaseClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class BaseClass {
/**
* setExistingProps
* @version 1.2 (2019-08-21)
* @version 1.3 (2019-12-14)
*
* @desc Sets existing object properties.
*
Expand All @@ -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');
}
}
}
Expand Down

0 comments on commit df46b80

Please sign in to comment.