class XmlConverter
{
/**
* Conver array in xml
*/
public function as_array(array $data, $xml = NULL)
{
if (is_null($xml))
{
$xml = simplexml_load_string('<'.key($data).'/>');
$data = current($data);
$return = TRUE;
}
if (is_array($data))
{
foreach ($data as $name => $value)
{
self::from_array($value, is_numeric($name) ? $xml : $xml->addChild($name));
}
}
else
{
$xml->{0} = $data;
}
if ( ! empty($return))
{
return $xml->asXML();
}
}
/**
* Conver xml in array
*/
public function to_array($xml)
{
$tree = NULL;
while($xml->read())
{
switch ($xml->nodeType)
{
case XMLReader::END_ELEMENT:
return $tree;
case XMLReader::ELEMENT:
$node = array(
'tag' => $xml->name,
'value' => $xml->isEmptyElement ? '' : self::to_array($xml)
);
if ($xml->hasAttributes)
{
while ($xml->moveToNextAttribute())
{
$node['attributes'][$xml->name] = $xml->value;
}
}
$tree[] = $node;
break;
case XMLReader::TEXT:
case XMLReader::CDATA:
$tree .= $xml->value;
}
}
return $tree;
}
}
$formatter = new \Phpcf\Formatter(new \Phpcf\Options());
$result = $formatter->formatFile('path/to/file.php');
if ($result->getError()) {
throw $result->getError();
} else {
file_put_contents($result->getFile(), $result->getContent());
}
$file = __DIR__ . '/demo.php';
$config = \PhpCsFixer\Config::create()
->setRules([
'@PSR12' => true,
'list_syntax' => ['syntax' => 'short'],
])
->setFinder(new ArrayIterator([new SplFileInfo($file)]));
$resolver = new \PhpCsFixer\Console\ConfigurationResolver(
$config,
[],
__DIR__,
new \PhpCsFixer\ToolInfo()
);
$runner = new \PhpCsFixer\Runner\Runner(
$config->getFinder(),
$resolver->getFixers(),
$resolver->getDiffer(),
null,
new \PhpCsFixer\Error\ErrorsManager(),
$resolver->getLinter(),
$resolver->isDryRun(),
$resolver->getCacheManager(),
$resolver->getDirectory(),
$resolver->shouldStopOnViolation()
);
$runner->fix();