Skip to content

Commit

Permalink
+ \DDTools\ObjectTools::unfold: The new method. Converts a multidim…
Browse files Browse the repository at this point in the history
…ensional array/object into an one-dimensional one joining the keys with `'.'`. For example, it can be helpful while using placeholders like `[+size.width+]`. See more info and examples in README.md.

* `\ddTools::unfoldArray`: Still works and just uses `\DDTools\ObjectTools::unfold`.
  • Loading branch information
Ronef committed Nov 16, 2021
1 parent a7ce175 commit de6b136
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 51 deletions.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,40 @@ 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 `'.'`.
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->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 +1009,69 @@ 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'
)
```


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

Checks if the object, class or array has a property / element using the same syntax.
Expand Down
58 changes: 7 additions & 51 deletions modx.ddtools.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,62 +288,18 @@ public static function explodeAssoc(

/**
* 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.
* @version 1.1 (2021-11-16)
*
* @see README.md (\DDTools\ObjectTools::unfold)
*/
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;
return \DDTools\ObjectTools::unfold([
'object' => $array,
'keyPrefix' => $keyPrefix
]);
}

/**
Expand Down
82 changes: 82 additions & 0 deletions src/ObjectTools/ObjectTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,86 @@ public static function extend($params){

return $result;
}

/**
* unfold
* @version 1.0 (2021-11-16)
*
* @see README.md
*
* @return {stdClass|array}
*/
public static function unfold($params){
$params = self::extend([
'objects' => [
//Defaults
(object) [
'keyPrefix' => '',
//The internal parameter, should not be used outside. Used only in child calls of recursion.
'isSourceObject' => null
],
$params
]
]);

//Array is used as base and it always be returned in child calls of recursion
$result = [];

$isSourceObject =
//If it's the first call of recurson
is_null($params->isSourceObject) ?
//Use original type
is_object($params->object) :
//Use from parent call of recursion
$params->isSourceObject
;

//Iterate over source
foreach (
$params->object as
$key =>
$value
){
//If the value must be unfolded
if (
(
$isSourceObject &&
is_object($value)
) ||
(
!$isSourceObject &&
is_array($value)
)
){
$result = array_merge(
$result,
self::unfold([
'object' => $value,
'keyPrefix' =>
$params->keyPrefix .
$key .
'.'
,
'isSourceObject' => $isSourceObject
])
);
//Если значение — не массив
}else{
//Запоминаем (в соответствии с ключом родителя)
$result[$params->keyPrefix . $key] = $value;
}
}

if (
//If it's first call of recurson
is_null($params->isSourceObject) &&
//And the final result must be an object
$isSourceObject
){
//Only the first call of recursion can return an object
$result = (object) $result;
}

return $result;
}
}

0 comments on commit de6b136

Please sign in to comment.