diff --git a/README.md b/README.md index e6eb605..e201072 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,12 @@ # (MODX)EvolutionCMS.libraries.ddTools A library with various tools facilitating your work. + + +## # Requires +* PHP >= 5.4 +* [(MODX)EvolutionCMS](https://github.com/evolution-cms/evolution) >= 1.0.10 +* [PHP.libraries.phpThumb](http://phpthumb.sourceforge.net) 1.7.13-201406261000 (included) + ___ Visit the following [link](http://code.divandesign.biz/modx/ddtools) to read the documentation, instructions & changelog. \ No newline at end of file diff --git a/composer.json b/composer.json index 87c1462..d1f8f7e 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "dd/modxevo-library-ddtools", "type": "modxevo-library-ddtools", - "version": "0.25.0", + "version": "0.26.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 a1d824b..1dc2bc9 100644 --- a/modx.ddtools.class.php +++ b/modx.ddtools.class.php @@ -1,13 +1,11 @@ = 5.4. - * @uses (MODX)EvolutionCMS >= 1.0.10 {@link https://github.com/evolution-cms/evolution }. - * @uses phpThumb lib 1.7.13-201406261000 (included) {@link http://phpthumb.sourceforge.net }. + * @see README.md * - * @link http://code.divandesign.biz/modx/ddtools/0.24.1 + * @link http://code.divandesign.biz/modx/ddtools * * @copyright 2012–2019 DivanDesign {@link http://www.DivanDesign.biz } */ diff --git a/require.php b/require.php index a4085c4..a04d358 100644 --- a/require.php +++ b/require.php @@ -1,5 +1,5 @@ \ No newline at end of file diff --git a/src/BaseClass/BaseClass.php b/src/BaseClass/BaseClass.php new file mode 100644 index 0000000..1abef8c --- /dev/null +++ b/src/BaseClass/BaseClass.php @@ -0,0 +1,122 @@ + + $propValue + ){ + if (property_exists( + $this, + $propName + )){ + $setProp = function( + $propName, + $propValue + ){ + $this->{$propName} = $propValue; + }; + + //Access to private properties too + $setProp->call( + $this, + $propName, + $propValue + ); + } + } + } + + /** + * createChildInstance + * @version 1.1.1 (2019-08-22) + * + * @throws \Exception + * + * @param $params {array_associative|stdClass} — The object of params. @required + * @param $params->parentDir {string} — Directory of the parent file (e. g. __DIR__). @required + * @param $params->name {string} — Class name. @required + * @param $params->params {array_associative|stdClass} — Params to be passed to object constructor. Default: []. + * @param $params->capitalizeName {boolean} — Need to capitalize child name? Default: true. + * + * @return {object} + */ + public static final function createChildInstance($params){ + //Defaults + $params = (object) array_merge( + [ + 'params' => [], + 'capitalizeName' => true + ], + (array) $params + ); + + $thisClassName = get_called_class(); + + $thisNameSpace = substr( + $thisClassName, + 0, + strrpos( + $thisClassName, + '\\' + ) + ); + + //Current classname without namespace + $thisClassName = substr( + $thisClassName, + strrpos( + $thisClassName, + '\\' + ) + 1 + ); + + //Capitalize child name if needed + if ($params->capitalizeName){ + $params->name = ucfirst(strtolower($params->name)); + } + + $filePath = + $params->parentDir . + DIRECTORY_SEPARATOR . + $params->name . + DIRECTORY_SEPARATOR . + $thisClassName . + '.php' + ; + + if(is_file($filePath)){ + require_once($filePath); + + $objectClass = + '\\' . + $thisNameSpace . + '\\' . + $params->name . + '\\' . + $thisClassName + ; + + return new $objectClass($params->params); + }else{ + throw new \Exception( + $thisClassName . ' “' . $params->name . '” not found.', + 500 + ); + } + } +} \ No newline at end of file diff --git a/src/ObjectTools/ObjectTools.php b/src/ObjectTools/ObjectTools.php deleted file mode 100644 index cc52569..0000000 --- a/src/ObjectTools/ObjectTools.php +++ /dev/null @@ -1,116 +0,0 @@ -object {object} — The object. @required - * @param $params->props {array_associative|stdClass} — The object properties. @required - * - * @return {void} - */ - public static function setExistingProps($params){ - $params = (object) $params; - - $params->props = (object) $params->props; - - foreach ( - $params->props as - $paramName => - $paramValue - ){ - if (property_exists( - $params->object, - $paramName - )){ - $params->object->{$paramName} = $paramValue; - } - } - } - - /** - * createChildInstance - * @version 1.0.1 (2019-06-23) - * - * @desc Creates an instance of the needed child class (e. g. \ddSendFeedback\Sender\Telegram\Sender). - * - * @param $params {array_associative|stdClass} — The object of params. @required - * @param $params->parentDir {string} — Directory of the parent file (e. g. __DIR__). @required - * @param $params->parentFullClassName {string} — Parent class name including namespace (e. g. __CLASS__). @required - * @param $params->childName {string} — Child name. @required - * @param $params->childParams {array_associative|stdClass} — Params to be passed to object constructor. Default: []. - * @param $params->capitalizeChildName {boolean} — Need to capitalize child name? Default: false. - * - * @throws \Exception - * - * @return {object} - */ - public static function createChildInstance($params){ - //Defaults - $params = (object) array_merge( - [ - 'childParams' => [], - 'capitalizeChildName' => false - ], - (array) $params - ); - - //Delimiter between parent class name and namespace - $parentNamespaceDelimiter = strrpos( - $params->parentFullClassName, - '\\' - ); - //Parent classname without namespace - $parentClassName = substr( - $params->parentFullClassName, - $parentNamespaceDelimiter + 1 - ); - //Parent namespace - $parentNamespace = substr( - $params->parentFullClassName, - 0, - $parentNamespaceDelimiter - ); - - //Capitalize child name if needed - if ($params->capitalizeChildName){ - $params->childName = ucfirst(strtolower($params->childName)); - } - - $childFullPath = - $params->parentDir . - DIRECTORY_SEPARATOR . - $params->childName . - DIRECTORY_SEPARATOR . - $parentClassName . '.php' - ; - - //If child exist - if(is_file($childFullPath)){ - //Iclude child - require_once($childFullPath); - - $childClass = - $parentNamespace . - '\\' . - $params->childName . - '\\' . - $parentClassName - ; - - return new $childClass($params->childParams); - }else{ - throw new \Exception( - $parentClassName . ' “' . $params->childName . '” not found.', - 500 - ); - } - } -} \ No newline at end of file