Skip to content

Comments

setValue method bug fix#1

Open
fukuball wants to merge 4 commits intotebru:masterfrom
fukuball:master
Open

setValue method bug fix#1
fukuball wants to merge 4 commits intotebru:masterfrom
fukuball:master

Conversation

@fukuball
Copy link

setValue method will get different result according to the order of
keys, the short key will overwrite the long keys, should check the long
key is set, and push the short key.

setValue method will get different result according to the order of
keys, the short key will overwrite the long keys, should check the long
key is set, and push the short key.
@fukuball
Copy link
Author

for example

$multiArray = new Tebru\MultiArray(array());
$multiArray->set('考.察.队.员', array("end"=>""));
$multiArray->set('考.察.上', array("end"=>""));
$multiArray->set('考.察', array("end"=>""));
var_dump($multiArray);

will get:

object(Tebru\MultiArray)#4 (3) {
  ["keyDelimiter":"Tebru\MultiArray":private]=>
  string(1) "."
  ["storage":"Tebru\MultiArray":private]=>
  array(1) {
    ["考"]=>
    array(1) {
      [""]=>
      array(1) {
        ["end"]=>
        string(0) ""
      }
    }
  }
  ["cache":"Tebru\MultiArray":private]=>
  array(3) {
    ["考.察.队.员"]=>
    array(1) {
      ["end"]=>
      string(0) ""
    }
    ["考.察.上"]=>
    array(1) {
      ["end"]=>
      string(0) ""
    }
    ["考.察"]=>
    array(1) {
      ["end"]=>
      string(0) ""
    }
  }
}

and when change order:

$multiArray = new Tebru\MultiArray(array());
$multiArray->set('考.察', array("end"=>""));
$multiArray->set('考.察.上', array("end"=>""));
$multiArray->set('考.察.队.员', array("end"=>""));
var_dump($multiArray);

will get:

object(Tebru\MultiArray)#5 (3) {
  ["keyDelimiter":"Tebru\MultiArray":private]=>
  string(1) "."
  ["storage":"Tebru\MultiArray":private]=>
  array(1) {
    ["考"]=>
    array(1) {
      [""]=>
      array(3) {
        ["end"]=>
        string(0) ""
        [""]=>
        array(1) {
          ["end"]=>
          string(0) ""
        }
        [""]=>
        array(1) {
          [""]=>
          array(1) {
            ["end"]=>
            string(0) ""
          }
        }
      }
    }
  }
  ["cache":"Tebru\MultiArray":private]=>
  array(3) {
    ["考.察"]=>
    array(1) {
      ["end"]=>
      string(0) ""
    }
    ["考.察.上"]=>
    array(1) {
      ["end"]=>
      string(0) ""
    }
    ["考.察.队.员"]=>
    array(1) {
      ["end"]=>
      string(0) ""
    }
  }
}

@natebrunette
Copy link
Member

Could you also update the tests and rebase?

setValue method will get InvalidArgumentException when set string value
to keys according to the order of set value order. Fix this bug then we
don’t get InvalidArgumentException anymore.
@fukuball
Copy link
Author

When I write test, I fix another bug: setValue method will get InvalidArgumentException when set string value(not get exception when set array value) to keys according to the order of set value order. Fix this bug then we don’t have to check InvalidArgumentException anymore.

For example:

$multiArray = new Tebru\MultiArray(array());
$multiArray->set('key1.key2.key3.key4', 'value1');
$multiArray->set('key1.key2.key5', 'value2');
$multiArray->set('key1.key2', 'value3');
var_dump($multiArray);

will not get InvalidArgumentException:

object(Tebru\MultiArray)#3 (3) {
  ["keyDelimiter":"Tebru\MultiArray":private]=>
  string(1) "."
  ["storage":"Tebru\MultiArray":private]=>
  array(1) {
    ["key1"]=>
    array(1) {
      ["key2"]=>
      array(3) {
        ["key3"]=>
        array(1) {
          ["key4"]=>
          string(6) "value1"
        }
        ["key5"]=>
        string(6) "value2"
        [0]=>
        string(6) "value3"
      }
    }
  }
  ["cache":"Tebru\MultiArray":private]=>
  array(3) {
    ["key1.key2.key3.key4"]=>
    string(6) "value1"
    ["key1.key2.key5"]=>
    string(6) "value2"
    ["key1.key2"]=>
    string(6) "value3"
  }
}

change the order form short key to long key:

$multiArray = new Tebru\MultiArray(array());
$multiArray->set('key1.key2', 'value3');
$multiArray->set('key1.key2.key5', 'value2');
$multiArray->set('key1.key2.key3.key4', 'value1');
var_dump($multiArray);

will get InvalidArgumentException:

PHP Fatal error:  Uncaught exception 'InvalidArgumentException' with message 'Expected array, got string'

@fukuball
Copy link
Author

@natebrunette I add a simple test case and rebase the repo, and pass the build.
https://travis-ci.org/fukuball/multi-array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants