Skip to content

Commit

Permalink
Version 0.59
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronef committed Mar 29, 2023
2 parents 0f57e86 + 62f1f11 commit c10961a
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 8 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.59 (2023-03-30)
* \+ `\ddTools::parseText`: In addition to accessing nested properties via placeholders like `'[+prop.subProp+]'`, you can get whole object as JSON via placeholders like `'[+prop+]'` (see README → Examples).


## Version 0.58.1 (2023-03-29)
* \* `\DDTools\ObjectTools::convertType` → Parameters → `$params->type` → Valid values → `'stringQueryFormatted'`: A mistake has been fixed. The incorrect value `'stringQueryFormated'` is also supported for backward compatibility.

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.59 (2023-03-30)
* \+ `\ddTools::parseText`: Помимо доступа свойствам вложенных объектов через плейсхоледры вида `'[+prop.subProp+]'`, вы также можете получить весь объект в JSON через плейсхолдеры вида `'[+prop+]'` (см. README → Примеры).


## Версия 0.58.1 (2023-03-29)
* \* `\DDTools\ObjectTools::convertType` → Параметры → `$params->type` → Допустимые значения → `'stringQueryFormatted'`: Исправлена ошибка в написании. Некорректное значение `'stringQueryFormated'` также поддерживается для обратной совместимости.

Expand Down
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,82 @@ extract(\ddTools::verifyRenamedParams([
```


### `\ddTools::parseText($params)`


#### Basic example

```php
\ddTools::parseText([
'text' => '
<article>
<h1>[+title+]</h1>
[+text+]
<p>[+authorFirstName+] [+authorLastName+], [+date+].</p>
</article>
',
'data' => [
'title' => 'Bethink Yourselves!',
'text' => '<p>Question your loyalty to your country and government and strive for a more just and peaceful society.</p>',
'authorFirstName' => 'Leo',
'authorLastName' => 'Tolstoy',
'date' => '1904'
]
]);
```

Returns:

```html
<article>
<h1>Bethink Yourselves!</h1>
<p>Question your loyalty to your country and government and strive for a more just and peaceful society.</p>
<p>Leo Tolstoy, 1904.</p>
</article>
```


#### Nested objects in `$params->data`

```php
\ddTools::parseText([
//Data can have a complex nested structure
'data' => [
'title' => 'Bethink Yourselves!',
'text' => '<p>Question your actions and consider the morality behind them.</p>',
//Note that this is not a string, but that's okay
'meta' => [
//Moreover, any depth is supported
'author' => [
'firstName' => 'Leo',
'lastName' => 'Tolstoy',
],
'date' => '1904'
]
],
//For nested data you can use placeholders like '[+meta.date+]' for getting a property
//Or like '[+meta+]' to get whole object as JSON
'text' => '
<article data-meta=\'[+meta+]\'>
<h1>[+title+]</h1>
[+text+]
<p>[+meta.author.firstName+] [+meta.author.lastName+], [+meta.date+].</p>
</article>
'
]);
```

Returns:

```html
<article data-meta='{"author":{"firstName":"Leo","lastName":"Tolstoy"},"date":"1904"}'>
<h1>Bethink Yourselves!</h1>
<p>Question your actions and consider the morality behind them.</p>
<p>Leo Tolstoy, 1904.</p>
</article>
```


### `\DDTools\ObjectTools`


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.58.1",
"version": "0.59.0",
"description": "A library with various tools facilitating your work.",
"keywords": [
"modx",
Expand Down
39 changes: 32 additions & 7 deletions modx.ddtools.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* EvolutionCMS.libraries.ddTools
* @version 0.58.1 (2023-03-29)
* @version 0.59 (2023-03-30)
*
* @see README.md
*
Expand Down Expand Up @@ -893,7 +893,7 @@ public static function logEvent($params){

/**
* parseText
* @version 1.6.2 (2021-12-09)
* @version 1.7 (2023-03-29)
*
* @see README.md
*/
Expand Down Expand Up @@ -940,11 +940,6 @@ public static function parseText($params = []){
]);
}

//Unfold for nested objects and arrays support (e. g. ['some' => ['a' => 'one', 'b' => 'two'] ] → '[+some.a+]', '[+some.b+]'; ['some' => ['one', 'two'] ] → '[+some.0+]', '[some.1]')
$params->data = \DDTools\ObjectTools::unfold([
'object' => $params->data
]);


$result = $params->text;

Expand All @@ -953,6 +948,36 @@ public static function parseText($params = []){
$key =>
$value
){
if (
is_object($value) ||
is_array($value)
){
//Unfold for nested objects and arrays support (e. g. ['some' => ['a' => 'one', 'b' => 'two'] ] → '[+some.a+]', '[+some.b+]'; ['some' => ['one', 'two'] ] → '[+some.0+]', '[some.1]')
$unfoldedValue = \DDTools\ObjectTools::unfold([
'object' => [
$key => $value
]
]);

foreach (
$unfoldedValue as
$unfoldedValue_itemKey =>
$unfoldedValue_itemValue
){
$result = str_replace(
$params->placeholderPrefix . $unfoldedValue_itemKey . $params->placeholderSuffix,
$unfoldedValue_itemValue,
$result
);
}

//Also add object value as JSON
$value = \DDTools\ObjectTools::convertType([
'object' => $value,
'type' => 'stringJsonAuto'
]);
}

$result = str_replace(
$params->placeholderPrefix . $key . $params->placeholderSuffix,
$value,
Expand Down

0 comments on commit c10961a

Please sign in to comment.