Skip to content

Commit

Permalink
Version 0.61
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronef committed Sep 30, 2023
2 parents 47964ab + b7ab2b2 commit eae0343
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 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.61 (2023-10-01)
* \+ `\DDTools\ObjectTools::convertType` → Parameters → `$params->type` → Valid values → `'stringHtmlAttrs'`: The new available value. Allows conversion to an HTML attributes string (see README → Examples).


## Version 0.60 (2023-05-14)
* \+ `\ddTools::getTpl`: The new public method (see README).

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG_ru.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# (MODX)EvolutionCMS.libraries.ddTools changelog


## Версия 0.61 (2023-10-01)
* \+ `\DDTools\ObjectTools::convertType` → Параметры → `$params->type` → Допустимые значения → `'stringHtmlAttrs'`: Новое значение. Позволяет преобразовать в строку HTML-атрибутов (см. README → Примеры).


## Версия 0.60 (2023-05-14)
* \+ `\ddTools::getTpl`: Новый публичный метод (см. README).

Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ Arrays, [JSON](https://en.wikipedia.org/wiki/JSON) and [Query string](https://en
* `'stringJsonObject'`
* `'stringJsonArray'`
* `'stringQueryFormatted'`
* `'stringHtmlAttrs'` — HTML attributes string (e. g. `width='100' height='50'`), boolean values will be converted to `0` or `1` (e. g. `data-is-loaded='1'`), objects/arrays will be converted to JSON string (e. g. `data-user-data='{"firstName": "Elon", "lastName": "Musk"}'`)
* Default value: `'objectAuto'`


Expand Down Expand Up @@ -1369,6 +1370,36 @@ A simple syntax and easy to read.',
```


##### Convert an associative array to a string of HTML attributes

```php
\DDTools\ObjectTools::convertType([
'object' => [
'data-name' => 'KINO',
//Will be converted to 1
'data-is-active' => true,
//Will be converted to JSON array
'data-members' => [
'Viktor Tsoi',
'Yuri Kasparyan',
'Aleksei Rybin',
'Igor Tikhomirov',
'Aleksandr Titov',
'Georgy Guryanov',
'Oleg Valinsky',
],
],
'type' => 'stringHtmlAttrs'
]);
```

Returns:

```html
data-name='KINO' data-is-active='1' data-members='["Viktor Tsoi","Yuri Kasparyan","Aleksei Rybin","Igor Tikhomirov","Aleksandr Titov","Georgy Guryanov","Oleg Valinsky"]'
```


#### `\DDTools\ObjectTools::extend($params)`


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/evolutioncms-libraries-ddtools",
"type": "modxevo-library-ddtools",
"version": "0.60.0",
"version": "0.61.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.60 (2023-05-14)
* @version 0.61 (2023-10-01)
*
* @see README.md
*
Expand Down
39 changes: 38 additions & 1 deletion src/ObjectTools/ObjectTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static function getPropValue($params){

/**
* convertType
* @version 1.2.1 (2023-03-29)
* @version 1.3 (2023-10-01)
*
* @see README.md
*/
Expand Down Expand Up @@ -229,6 +229,43 @@ public static function convertType($params){
$params->type == 'stringqueryformated'
){
$result = http_build_query($result);
//stringHtmlAttrs
}elseif ($params->type == 'stringhtmlattrs'){
$resultObject = $result;
//Temporary use an array
$result = [];

foreach (
$resultObject as
$result_itemAttrName =>
$result_itemAttrValue
){
//Prepare value
//Boolean to 0|1
if (is_bool($result_itemAttrValue)){
$result_itemAttrValue = intval($result_itemAttrValue);
//Objects to JSON
}elseif(self::isObjectOrArray($result_itemAttrValue)){
$result_itemAttrValue = self::convertType([
'object' => $result_itemAttrValue,
'type' => 'stringJsonAuto',
]);
//Other to string
}else{
$result_itemAttrValue = strval($result_itemAttrValue);
}

$result[] =
$result_itemAttrName . '=\'' .
$result_itemAttrValue .
'\''
;
}

$result = implode(
' ',
$result
);
//stringJson
}elseif(
substr(
Expand Down

0 comments on commit eae0343

Please sign in to comment.