Skip to content

Instantly share code, notes, and snippets.

@Kcko
Created September 25, 2019 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcko/edde8bef39e71436f4533bfc02a4780c to your computer and use it in GitHub Desktop.
Save Kcko/edde8bef39e71436f4533bfc02a4780c to your computer and use it in GitHub Desktop.
<?php
$arr = [
'company' => 'AMI',
'employeeCount' => 25,
'years' => 12,
'bankInfo' => [
'account' => 'xxxx',
'money' => '2 billions'
]
];
echo F::create()->getResource($arr)->formatToXml();
class F
{
private $resource;
public static function create()
{
return new self;
}
public function getResource($resource)
{
$this->resource = $resource;
return $this;
}
public function formatToJson()
{
return json_encode($this->resource);
}
public function formatToArray()
{
return $this->resource;
}
public function formatToXml()
{
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($this->resource, array ($xml, 'addChild'));
return $xml->asXML();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment