diff --git a/CHANGELOG.md b/CHANGELOG.md index 8eab023..f3c41fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ # (MODX)EvolutionCMS.libraries.ddTools changelog +## Version 0.48 (2021-03-25) +* \+ `\DDTools\Snippet`: + * \+ `$paramsTypes`: The new property. Overwrite in child classes if you want to convert some parameters types. + * \+ `prepareParams`: The parameter types will be converted respectively with `$this->paramsTypes`. + + ## Version 0.47 (2021-03-20) * \+ `\DDTools\ObjectTools::convertType`: * \+ Added [HJSON](https://hjson.github.io/) support (closes #7). diff --git a/CHANGELOG_ru.md b/CHANGELOG_ru.md index f1244b0..b1ddb4f 100644 --- a/CHANGELOG_ru.md +++ b/CHANGELOG_ru.md @@ -1,6 +1,12 @@ # (MODX)EvolutionCMS.libraries.ddTools changelog +## Версия 0.48 (2021-03-25) +* \+ `\DDTools\Snippet`: + * \+ `$paramsTypes`: Новое свойство. Перекройте в дочерних классах если вы хотите сконвертировать типы параметров. + * \+ `prepareParams`: Типы параметров будут сконвертированы соответственно с `$this->paramsTypes`. + + ## Версия 0.47 (2021-03-20) * \+ `\DDTools\ObjectTools::convertType`: * \+ Добавлена поддержка [HJSON](https://hjson.github.io/) (closes #7). diff --git a/README.md b/README.md index 0200286..9d87791 100644 --- a/README.md +++ b/README.md @@ -487,6 +487,26 @@ Abstract class for snippets. * Desctription: Key is parameter name, value is value. * Valid values: `mixed` +* `\DDTools\Snippet::$paramsTypes` + * Desctription: Overwrite in child classes if you want to convert some parameters types. + Parameters types will be converted respectively with this field in `\DDTools\Snippet::prepareParams`. + * Valid values: `arrayAssociative` + * Visibility: `protected` + +* `\DDTools\Snippet::$paramsTypes[$paramName]` + * Desctription: The parameter type. + Values are case insensitive (the following names are equal: `'stringjsonauto'`, `'stringJsonAuto'`, `'STRINGJSONAUTO'`, etc). + * Valid values: + * `'integer'` + * `'boolean'` + * `'objectAuto'` + * `'objectStdClass'` + * `'objectArray'` + * `'stringJsonAuto'` + * `'stringJsonObject'` + * `'stringJsonArray'` + * Visibility: `protected` + * `\DDTools\Snippet::$renamedParamsCompliance` * Desctription: Overwrite in child classes if you want to rename some parameters with backward compatibility (see `$params->compliance` of `\ddTools::verifyRenamedParams`). * Valid values: `arrayAssociative` diff --git a/composer.json b/composer.json index b681d2a..9a9bb6c 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "dd/evolutioncms-libraries-ddtools", "type": "modxevo-library-ddtools", - "version": "0.47.0", + "version": "0.48.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 daca371..65f0968 100644 --- a/modx.ddtools.class.php +++ b/modx.ddtools.class.php @@ -1,7 +1,7 @@ params = (object) $this->params; + $params = \DDTools\ObjectTools::convertType([ 'object' => $params, 'type' => 'objectStdClass' @@ -102,10 +110,52 @@ protected function prepareParams($params = []){ ]); } + if (!empty($this->paramsTypes)){ + foreach ( + $this->paramsTypes as + $paramName => + $paramType + ){ + $paramType = strtolower($paramType); + + if ($paramType == 'integer'){ + $params->{$paramName} = intval($params->{$paramName}); + }elseif ($paramType == 'boolean'){ + $params->{$paramName} = boolval($params->{$paramName}); + }else{ + //Convert defaults + if ( + \DDTools\ObjectTools::isPropExists([ + 'object' => $this->params, + 'propName' => $paramName + ]) + ){ + $this->params->{$paramName} = \DDTools\ObjectTools::convertType([ + 'object' => $this->params->{$paramName}, + 'type' => $paramType + ]); + } + + //Convert given + if ( + \DDTools\ObjectTools::isPropExists([ + 'object' => $params, + 'propName' => $paramName + ]) + ){ + $params->{$paramName} = \DDTools\ObjectTools::convertType([ + 'object' => $params->{$paramName}, + 'type' => $paramType + ]); + } + } + } + } + $this->params = \DDTools\ObjectTools::extend([ 'objects' => [ //Defaults - (object) $this->params, + $this->params, //Given parameters $params ]