Please check following class that I've create to build few XML frames..
class CommandBuilder
{
public function __construct()
{
//
}
public function login($username, $password)
{
$frame = $this->frame();
$command = $frame->addChild('command');
$login = $command->addChild('login');
$login->addChild('username', $username);
$login->addChild('password', $password);
$command->addChild('authKey', 'authkey');
return $frame->asXML();
}
public function info($id)
{
$frame = $this->frame();
$command = $frame->addChild('command');
$login = $command->addChild('product');
$login->addChild('id', $id);
$command->addChild('authKey', 'authkey');
return $frame->asXML();
}
protected function frame()
{
return new SimpleXMLElement(
'<app/>'
);
}
}
What's the best way to avoid duplication of $frame->addChild('command') and $command->addChild('authKey', 'authkey') without changing order of elements?
Please help to improve the code. Thanks