adbario / php-dot-notation

Dot notation access to PHP arrays

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Set method does not split key with dot

KarelWintersky opened this issue · comments

$dot = new \Adbar\Dot();

$dot->set('LOGGING.KEY_3', 3);

$dot->set('LOGGING', [
    'KEY_1'       =>  1,
    'KEY_2'             =>  2,
]);

var_dump( $dot->get('LOGGING.KEY_3') );

var_dump( $dot->get('LOGGING') );

var_dump( $dot->get() );

Return:

NULL
array(2) {
  ["KEY_1"]=>
  int(1)
  ["KEY_2"]=>
  int(2)
}
array(1) {
  ["LOGGING"]=>
  array(2) {
    ["KEY_1"]=>
    int(1)
    ["KEY_2"]=>
    int(2)
  }
}

Expected not null and array with KEY_3 => (int)3

also, add() works strangely:

$dot->add('LOGGING.KEY_3', 3);

$dot->add('LOGGING', [
    'KEY_1'       =>  1,
    'KEY_2'             =>  2,
]);

var_dump( $dot->get('LOGGING.KEY_3') );

var_dump( $dot->get('LOGGING') );

var_dump( $dot->get() );

return:

int(3)
array(1) {
  ["KEY_3"]=>
  int(3)
}
array(1) {
  ["LOGGING"]=>
  array(1) {
    ["KEY_3"]=>
    int(3)
  }
}

Hi @KarelWintersky 👋

If you wish to add more items under the LOGGING...

$dot->set('LOGGING.KEY_3', 3);

$dot->set('LOGGING', [
    'KEY_1' => 1,
    'KEY_2' => 2,
]);

...use should use the merge method:

$dot->set('LOGGING.KEY_3', 3);

$dot->merge('LOGGING', [
    'KEY_1' => 1,
    'KEY_2' => 2,
]);

And about the add() method: see the documentation:

Sets a given key / value pair if the key doesn't exist already

In your example, the merge() method should be used as the key already exists.