Skip to content

Commit

Permalink
Version 0.49
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronef committed Apr 25, 2021
2 parents a1ea1f5 + 8e5bd0d commit 42d316e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 32 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# (MODX)EvolutionCMS.libraries.ddTools changelog


## Version 0.49 (2021-04-25)
* \* `\ddTools::parseText`:
* \+ Parameters → `$params->data`: Can also be set as JSON, HJSON or Query string.
* \* `\DDTools\ObjectTools::extend` is used instead of `array_merge`.
* \+ README → Documentation → Parameters description → `\ddTools::parseText($params)`.


## Version 0.48.2 (2021-03-31)
* \* `\DDTools\ObjectTools::extend`: Added deep object cloning to prevent references.

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


## Версия 0.49 (2021-04-25)
* \* `\ddTools::parseText`:
* \+ Параметры → `$params->data`: Также может быть задан как JSON, HJSON или Query string.
* \* `\DDTools\ObjectTools::extend` исползуется вместо `array_merge`.
* \+ README → Документация → Описание параметров → `\ddTools::parseText($params)`.


## Версия 0.48.2 (2021-03-31)
* \* `\DDTools\ObjectTools::extend`: Добавлено глубокое клонирование объектов для предотвращения ссылок.

Expand Down
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,73 @@ _ddTools version must be 0.14 or higher to use this method. If you use it, the c
### Parameters description


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

Replaces placeholders in a text with required values.
Like `$modx->parseChunk`, but takes a text and has some features.

* `$params`
* Desctription: Parameters, the pass-by-name style is used.
* Valid values:
* `stdClass`
* `arrayAssociative`
* **Required**

* `$params->text`
* Desctription: String to parse.
* Valid values: `string`
* **Required**

* `$params->data`
* Desctription:
The array of additional data has to be replaced in `$params->text`.
Nested objects and arrays are supported too:
* `{"someOne": "1", "someTwo": "test" }` => `[+someOne+], [+someTwo+]`.
* `{"some": {"a": "one", "b": "two"} }` => `[+some.a+]`, `[+some.b+]`.
* `{"some": ["one", "two"] }` => `[+some.0+]`, `[+some.1+]`.
* Valid values:
* `arrayAssociative`
* `stdClass`
* `stringJsonObject` — as [JSON](https://en.wikipedia.org/wiki/JSON)
* `stringHjsonObject` — as [HJSON](https://hjson.github.io/)
* `stringQueryFormated` — as [Query string](https://en.wikipedia.org/wiki/Query_string)
* Default value: `[]`

* `$params->data->{$key}`
* Valid values: Key is placeholder name, value is value.
* `string`
* `array`
* `object`
* **Required**

* `$params->placeholderPrefix`
* Desctription: Placeholders prefix.
* Valid values: `string`
* Default value: `'[+'`

* `$params->placeholderSuffix`
* Desctription: Placeholders suffix.
* Valid values: `string`
* Default value: `'+]'`

* `$params->removeEmptyPlaceholders`
* Desctription: Do you need to remove empty placeholders?
* Valid values: `boolean`
* Default value: `false`

* `$params->mergeAll`
* Desctription: Additional parsing of document fields, settings, chunks.
* Valid values: `boolean`
* Default value: `true`


##### Returns

* `$result`
* Desctription: Parsed text.
* Valid values: `string`


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

The method checks an array for deprecated parameters and writes warning messages into the MODX event log.
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.48.2",
"version": "0.49.0",
"description": "A library with various tools facilitating your work.",
"keywords": [
"modx",
Expand Down
54 changes: 23 additions & 31 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.48.2 (2021-03-31)
* @version 0.49 (2021-04-25)
*
* @see README.md
*
Expand Down Expand Up @@ -897,20 +897,9 @@ public static function logEvent($params){

/**
* parseText
* @version 1.5.2 (2019-06-22)
* @version 1.6 (2021-04-25)
*
* @desc Similar to $modx->parseChunk, but takes a text.
*
* @param $params {stdClass|arrayAssociative} — Parameters, the pass-by-name style is used. @required
* @param $params->text {string} — String to parse. @required
* @param $params->data {stdClass|arrayAssociative} — Array of values. Nested arrays are supported too: “['stringPlaceholder' = > 'one', 'arrayPlaceholder' => ['a' => 'one', 'b' => 'two']]” => “[+stringPlaceholder+]”, “[+arrayPlaceholder.a+]”, “[+arrayPlaceholder.b+]”. Default: [].
* @param $params->data->{$key} {string|stdClass|arrayAssociative} — Key — placeholder name, value — value.
* @param $params->placeholderPrefix {string} — Placeholders prefix. Default: '[+'.
* @param $params->placeholderSuffix {string} — Placeholders suffix. Default: '+]'.
* @param $params->removeEmptyPlaceholders {boolean} — Do you need to remove empty placeholders? Default: false.
* @param $params->mergeAll {boolean} — Additional parsing the document fields, settings, chunks. Default: true.
*
* @return {string}
* @see README.md
*/
public static function parseText($params = []){
//For backward compatibility
Expand All @@ -928,25 +917,28 @@ public static function parseText($params = []){
]);
}

//Defaults
$params = (object) array_merge(
[
'text' => '',
'data' => [],
'placeholderPrefix' => '[+',
'placeholderSuffix' => '+]',
'removeEmptyPlaceholders' => false,
'mergeAll' => true
],
(array) $params
);
$params = \DDTools\ObjectTools::extend([
'objects' => [
//Defaults
(object) [
'text' => '',
'data' => [],
'placeholderPrefix' => '[+',
'placeholderSuffix' => '+]',
'removeEmptyPlaceholders' => false,
'mergeAll' => true
],
$params
]
]);

$result = $params->text;
$params->data = \DDTools\ObjectTools::convertType([
'object' => $params->data,
'type' => 'objectArray'
]);

//Convert stdClass to array
if (!is_array($params->data)){
$params->data = (array) $params->data;
}

$result = $params->text;

//Если значения для парсинга переданы
if (!empty($params->data)){
Expand Down

0 comments on commit 42d316e

Please sign in to comment.