Skip to content

Commit

Permalink
Version 0.52
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronef committed Nov 16, 2021
2 parents a7ce175 + b7ccde0 commit a38b907
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 112 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.52 (2021-11-16)
* \+ `\DDTools\ObjectTools::unfold`: The new method.
Converts a multidimensional array/object into an one-dimensional one joining the keys with `$params->keySeparator`.
For example, it can be helpful while using placeholders like `[+size.width+]`.
See more info and examples in README.md.


## Version 0.51 (2021-11-08)
* \+ `\DDTools\ObjectTools::convertType`: Added the ability to return `stringQueryFormated`.

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.52 (2021-11-16)
* \+ `\DDTools\ObjectTools::unfold`: Новый метод.
Преобразует многомерный массив в одномерный, при этом ключи результирующего массива сливаются через `$params->keySeparator`.
Например, это может быть полезно при необходимости использования «вложенных плэйсхолдеров» (`[+size.width+]`).
См. больше информации и примеров в README.md.


## Версия 0.51 (2021-11-08)
* \+ `\DDTools\ObjectTools::convertType`: Добавлена возможность конвертировать в `stringQueryFormated`.

Expand Down
127 changes: 127 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,45 @@ Merge the contents of two or more objects or arrays together into the first one.
* Default value: `true`


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

Converts a multidimensional array/object into an one-dimensional one joining the keys with `$params->keySeparator`.
For example, it can be helpful while using placeholders like `[+size.width+]`.

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

* `$params->object`
* Desctription: An object/array to convert.
* Valid values:
* `stdClass`
* `arrayAssociative`
* **Required**

* `$params->keySeparator`
* Desctription: Separator between nested keys in the result object/array.
* Valid values: `string`
* Default value: `'.'`

* `$params->keyPrefix`
* Desctription: Prefix of the keys of an object/array (it's an internal varible, but can be used if required).
* Valid values: `string`
* Default value: `''`


###### Returns

* `$result`
* Desctription: Unfolded object/array. Type of results depends on `$params->object`.
* Valid values:
* `stdClass`
* `array`


#### `\DDTools\BaseClass`

Simple class with some small methods facilitating your work.
Expand Down Expand Up @@ -975,6 +1014,94 @@ stdClass::__set_state(array(
```


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


##### Unfold an object

```php
var_export(\DDTools\ObjectTools::unfold([
'object' => (object) [
'name' => 'Elon Musk',
'address' => (object) [
'line1' => '3500 Deer Creek Road',
'city' => 'Palo Alto',
'state' => 'California',
'country' => 'United States'
]
]
]));
```

Returns:

```php
stdClass::__set_state(array (
'name' => 'Elon Musk',
'address.line1' => '3500 Deer Creek Road',
'address.city' => 'Palo Alto',
'address.state' => 'California',
'address.country' => 'United States'
))
```


##### Unfold an array

```php
var_export(\DDTools\ObjectTools::unfold([
'object' => [
'a' => 'a val',
'b' => [
'b1' => 'b1 val',
'b2' => [
'b21' => 'b21 val',
'b22' => 'b22 val'
]
],
'c' => 'c val'
]
]));
```

Returns:

```php
array (
'a' => 'a val',
'b.b1' => 'b1 val',
'b.b2.b21' => 'b21 val',
'b.b2.b22' => 'b22 val',
'c' => 'c val'
)
```


##### Use custom key separator

```php
var_export(\DDTools\ObjectTools::unfold([
'object' => [
'name' => 'Elon Musk',
'parents' => [
'mother' => 'Maye Musk',
'father' => 'Errol Musk'
]
],
'keySeparator' => '_'
]));
```

Returns:

```php
stdClass::__set_state(array (
'name' => 'Elon Musk',
'parents_mother' => 'Maye Musk',
'parents_father' => 'Errol Musk'
))


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

Checks if the object, class or array has a property / element using the same syntax.
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.51.0",
"version": "0.52.0",
"description": "A library with various tools facilitating your work.",
"keywords": [
"modx",
Expand Down
178 changes: 67 additions & 111 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.51 (2021-11-08)
* @version 0.52 (2021-11-16)
*
* @see README.md
*
Expand Down Expand Up @@ -286,66 +286,6 @@ public static function explodeAssoc(
return $result;
}

/**
* unfoldArray
* @version 1.0.5 (2019-06-22)
*
* @desc Converts a multidimensional array into an one-dimensional one joining the keys with '.'. It can be helpful while using placeholders like [+size.width+].
* @example [
* 'a' => 'a val',
* 'b' => [
* 'b1' => 'b1 val',
* 'b2' => [
* 'b21' => 'b2.1 val',
* 'b22' => 'b2.2 val'
* ]
* ],
* 'c' => 'c val'
* ] turns into [
* 'a' => 'a val',
* 'b.b1' => 'b1 val',
* 'b.b2.b21' => 'b2.1 val',
* 'b.b2.b22' => 'b2.2 val',
* 'c' => 'c val'
* ].
*
* @param $array {array} — An array to convert. @required
* @param $keyPrefix {string} — Prefix of the keys of an array (it's an internal varible, can be used if required). Default: ''.
*
* @return {array} — Unfolded array.
*/
public static function unfoldArray(
$array,
$keyPrefix = ''
){
$result = [];

//Перебираем массив
foreach (
$array as
$key =>
$val
){
//Если значение является массивом
if (is_array($val)){
//Запускаем рекурсию дальше
$result = array_merge(
$result,
self::unfoldArray(
$val,
$keyPrefix . $key . '.'
)
);
//Если значение — не массив
}else{
//Запоминаем (в соответствии с ключом родителя)
$result[$keyPrefix . $key] = $val;
}
}

return $result;
}

/**
* sort2dArray
* @version 1.2.1 (2021-03-09)
Expand Down Expand Up @@ -529,56 +469,6 @@ public static function parseFileNameVersion($file){
return $result;
}

/**
* createDir
* @version 1.0 (2019-10-22)
*
* @desc Makes directory using `$modx->config['new_folder_permissions']`. Nested directories will be created too. Doesn't throw an exception if the folder already exists.
*
* @param $params {stdClass|arrayAssociative} — Parameters, the pass-by-name style is used. @required
* @param $params->path {string} — The directory path. @required
*
* @return {boolean} — Success status.
*/
public static function createDir($params){
return \DDTools\FilesTools::createDir($params);
}

/**
* copyDir
* @version 1.1 (2018-10-02)
*
* @desc Copies a required folder with all contents recursively.
*
* @param $sourcePath {string} — Path to the directory, that should copied. @required
* @param $destinationPath {string} — The destination path. @required
*
* @return {boolean} — Returns true on success or false on failure.
*/
public static function copyDir(
$sourcePath,
$destinationPath
){
return \DDTools\FilesTools::copyDir([
'sourcePath' => $sourcePath,
'destinationPath' => $destinationPath
]);
}

/**
* removeDir
* @version 1.1 (2018-10-02)
*
* @desc Removes a required folder with all contents recursively.
*
* @param $path {string} — Path to the directory, that should removed. @required
*
* @return {boolean}
*/
public static function removeDir($path){
return \DDTools\FilesTools::removeDir($path);
}

/**
* generateRandomString
* @version 1.0.3 (2018-06-17)
Expand Down Expand Up @@ -2872,6 +2762,72 @@ public static function getResponse(){
return new \DDTools\Response();
}

/**
* unfoldArray
* @version 1.1 (2021-11-16)
*
* @see README.md (\DDTools\ObjectTools::unfold)
*/
public static function unfoldArray(
$array,
$keyPrefix = ''
){
return \DDTools\ObjectTools::unfold([
'object' => $array,
'keyPrefix' => $keyPrefix
]);
}

/**
* createDir
* @version 1.0 (2019-10-22)
*
* @desc Makes directory using `$modx->config['new_folder_permissions']`. Nested directories will be created too. Doesn't throw an exception if the folder already exists.
*
* @param $params {stdClass|arrayAssociative} — Parameters, the pass-by-name style is used. @required
* @param $params->path {string} — The directory path. @required
*
* @return {boolean} — Success status.
*/
public static function createDir($params){
return \DDTools\FilesTools::createDir($params);
}

/**
* copyDir
* @version 1.1 (2018-10-02)
*
* @desc Copies a required folder with all contents recursively.
*
* @param $sourcePath {string} — Path to the directory, that should copied. @required
* @param $destinationPath {string} — The destination path. @required
*
* @return {boolean} — Returns true on success or false on failure.
*/
public static function copyDir(
$sourcePath,
$destinationPath
){
return \DDTools\FilesTools::copyDir([
'sourcePath' => $sourcePath,
'destinationPath' => $destinationPath
]);
}

/**
* removeDir
* @version 1.1 (2018-10-02)
*
* @desc Removes a required folder with all contents recursively.
*
* @param $path {string} — Path to the directory, that should removed. @required
*
* @return {boolean}
*/
public static function removeDir($path){
return \DDTools\FilesTools::removeDir($path);
}

/**
* screening
* @deprecated Use ddTools::escapeForJS.
Expand Down
Loading

0 comments on commit a38b907

Please sign in to comment.