Kcko
9/25/2019 - 12:36 PM

PHP Factory

<?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();
    }


}