/** * Create from map tests. * * @dataProvider providerCreateFromMap * * @param string $method * @param array $children */ public function testCreateFromMap($method, array $children = array()) { $matches = array(); preg_match('/^(\\^|\\$)?(.*)$/', $method, $matches); $isBase = $matches[1] == '^'; $isEnd = $matches[1] == '$'; $methodName = $matches[2]; $command = Command::createFromMap($method, $children); $this->assertEquals($methodName, $command->getMethodName()); $this->assertEquals($isBase, $command->willReturnToBase()); $this->assertEquals($isEnd, $command->willEndChain()); $this->assertEquals(count($children), count($command->getChildren())); }
/** * Creates a command instance from an array map representing the chain of * command. Will also iterate the creation of the command's children. * * Methods prefixed with a special character will have it's attributes set * as the following. * * $foo, endChain attribute will be set to true * ^foo, returnToBase attribute will be set to true * * N.b. Special characters are mutually exclusive. * * @param string $method * @param array $children * @return \Chainnn\Command */ public static function createFromMap($method, array $children = array()) { $flag = substr($method, 0, 1); $attr = array('endChain' => $flag === '$', 'returnToBase' => $flag === '^'); if (array_search(true, $attr, true)) { $method = substr($method, 1); } $command = new Command($method, $attr); foreach ($children as $childMethod => $childChildren) { if (is_int($childMethod)) { list($childMethod, $childChildren) = array($childChildren, array()); } $command->addChild(Command::createFromMap($childMethod, $childChildren)); } return $command; }