| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\helpers;
- class SerializeXMLObject
- {
- protected static $instace;
- protected $data;
- private function __construct($data = null)
- {
- $this->data = $data;
- }
- private function __clone()
- {
- ;
- }
- public static function getInstance($data = null)
- {
- if (is_null(self::$instace))
- {
- $class = get_class();
- self::$instace = new $class($data);
- }
- return self::$instace;
- }
- public function refactor()
- {
- foreach ($this->data as $field)
- {
- $function = $this->checkTypeAndGetFunction($field);
- if ($function)
- {
- $this->{$function}($field);
- }
- else
- {
- return $this->data;
- }
- }
- return $this->data;
- }
- private function checkTypeAndGetFunction($data)
- {
- if (is_array($data))
- {
- return 'refactorArray';
- }
- if (is_object($data))
- {
- return 'refactorObject';
- }
- }
- protected function refactorObject(&$data)
- {
- foreach ($data as $fieldName => $fieldValue)
- {
- $function = $this->checkTypeAndGetFunction($fieldValue);
- if ($function)
- {
- if ($fieldValue instanceof \SimpleXMLElement)
- {
- $data->{$fieldName} = $this->readXML($fieldValue);
- }
- else
- {
- $this->{$function}($fieldValue);
- }
- }
- }
- }
- protected function refactorArray(&$data)
- {
- foreach ($data as $fieldName => $fieldValue)
- {
- $function = $this->checkTypeAndGetFunction($fieldValue);
- if ($function)
- {
- $this->{$function}($fieldValue);
- }
- }
- }
- private function readXML(\SimpleXMLElement $xml)
- {
- if ($xml->count() > 0)
- {
- return $xml->{$xml->getName()};
- }
- return "";
- }
- }
|