Skip to content

Commit

Permalink
Allow Nesty to prepend an item without knowing the current first item.
Browse files Browse the repository at this point in the history
Signed-off-by: crynobone <[email protected]>
  • Loading branch information
crynobone committed Jul 3, 2013
1 parent e4b22e7 commit 4ab3730
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/Orchestra/Support/Nesty.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,15 @@ protected function addParent($id)
public function add($id, $location = '#')
{
preg_match('/^(<|>|\^):(.+)$/', $location, $matches);

switch (true)
{
case $location === '<' :
$keys = array_keys($this->items);

if (count($keys) > 0) return $this->addBefore($id, $keys[0]);
break;

case count($matches) >= 3 and $matches[1] === '<' :
return $this->addBefore($id, $matches[2]);
break;
Expand All @@ -190,9 +196,10 @@ public function add($id, $location = '#')
break;

default :
return $this->addParent($id);
break;
# passthru;
}

return $this->addParent($id);
}

/**
Expand Down
14 changes: 12 additions & 2 deletions tests/NestyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function testNewInstanceReturnEmptyArray()
public function testAddMethod()
{
$expected = array(
'crynobone' => new Fluent(array(
'id' => 'crynobone',
'childs' => array(),
)),
'hello' => new Fluent(array(
'id' => 'hello',
'childs' => array(),
Expand Down Expand Up @@ -103,17 +107,23 @@ public function testAddMethod()
'childs' => array(),
)),
),
))
)),
'orchestra' => new Fluent(array(
'id' => 'orchestra',
'childs' => array(),
)),
);

$this->stub->add('foo');
$this->stub->add('foo', '<');
$this->stub->add('hello', '<:foo');
$this->stub->add('world', '>:hello');
$this->stub->add('bar', '^:foo');
$this->stub->add('foobar', '^:foo');
$this->stub->add('foo-bar', '^:foo');
$this->stub->add('hello-foobar', '^:foo.foobar');
$this->stub->add('hello-world-foobar', '^:foo.dummy');
$this->stub->add('crynobone', '<');
$this->stub->add('orchestra', '#');

$this->assertEquals($expected, $this->stub->getItems());
}
Expand Down

0 comments on commit 4ab3730

Please sign in to comment.