Skip to content

Commit

Permalink
+ \DDTools\ObjectTools::unfold → Parameters → `$params->keySeparato…
Browse files Browse the repository at this point in the history
…r`: The new parameter. You can specify custom separator between nested keys in the result object/array.
  • Loading branch information
Ronef committed Nov 16, 2021
1 parent 8d7ff53 commit 6be4642
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ Merge the contents of two or more objects or arrays together into the first one.

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

Converts a multidimensional array/object into an one-dimensional one joining the keys with `'.'`.
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`
Expand All @@ -424,6 +424,11 @@ For example, it can be helpful while using placeholders like `[+size.width+]`.
* `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`
Expand Down Expand Up @@ -1072,6 +1077,31 @@ array (
```


##### 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
5 changes: 3 additions & 2 deletions src/ObjectTools/ObjectTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public static function extend($params){

/**
* unfold
* @version 1.0 (2021-11-16)
* @version 1.1 (2021-11-17)
*
* @see README.md
*
Expand All @@ -375,6 +375,7 @@ public static function unfold($params){
'objects' => [
//Defaults
(object) [
'keySeparator' => '.',
'keyPrefix' => '',
//The internal parameter, should not be used outside. Used only in child calls of recursion.
'isSourceObject' => null
Expand Down Expand Up @@ -419,7 +420,7 @@ public static function unfold($params){
'keyPrefix' =>
$params->keyPrefix .
$key .
'.'
$params->keySeparator
,
'isSourceObject' => $isSourceObject
])
Expand Down

0 comments on commit 6be4642

Please sign in to comment.