Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  /*
  3.   * @author manyrus
  4.   */
  5.  
  6. abstract class Parse{
  7.     /**
  8.      * @abstract
  9.      * @param string $string string|path to file
  10.      * @return array string parsed data
  11.      */
  12.     abstract public function decode($string);
  13. }
  14.  
  15. abstract class Xml extends Parse{
  16.     /**
  17.      * @param SimpleXMLElement $xml
  18.      * @return array parsed xml
  19.      */
  20.      protected function simpleXmlToArray($xml) {
  21.         $array = $xml;
  22.         $newArray = array();
  23.         $array = (array) $array;
  24.         foreach ($array as $key => $value) {
  25.             $value = (array) $value;
  26.             if (isset($value [0])) {
  27.                 $newArray [$key] = trim($value [0]);
  28.             } else {
  29.                 if(!empty($array[$key])) {
  30.                     $newArray [$key] = $this->simpleXmlToArray($value);
  31.                 } else {
  32.                     $newArray [$key] ='';
  33.                 }
  34.             }
  35.         }
  36.         return $newArray;
  37.     }
  38. }
  39.  
  40. abstract class Ini extends Parse{}
  41.  
  42. class ParseXmlFile extends Xml{
  43.     /**
  44.      * @param string $file
  45.      * @see Parse
  46.      */
  47.     public function decode($file) {
  48.         return $this->simpleXmlToArray(simplexml_load_file($file));
  49.     }
  50. }
  51.  
  52. class ParseXmlString extends Xml{
  53.     /**
  54.      * @param string $str
  55.      * @see Parse
  56.      */
  57.     public function decode($str) {
  58.         return $this->simpleXmlToArray(simplexml_load_string($str));
  59.     }
  60.  
  61. }
  62.  
  63. class ParseIniFile extends Ini{
  64.     /**
  65.      * @param string $file
  66.      * @see Parse
  67.      */
  68.     public function decode($file) {
  69.         return parse_ini_file($file);
  70.     }
  71.  
  72. }
  73.  
  74. class ParseIniString extends Ini{
  75.     /**
  76.      * @param string $str
  77.      * @see Parse
  78.      */
  79.     public function decode($str) {
  80.         return parse_ini_string($str);
  81.     }
  82.  
  83. }
  84. abstract class ParseFactory {
  85.     /**
  86.      * const of type
  87.      */
  88.     const XML = 0;
  89.     const INI = 1;
  90.     /**
  91.      * @static
  92.      * @abstract
  93.      * @param const $type
  94.      * @return Parse
  95.      */
  96.     abstract public static function createFactory($type);
  97. }
  98.  
  99. class ParseStringFactory extends ParseFactory{
  100.     /**
  101.      * @see ParseFactory
  102.      */
  103.     public static function createFactory($type)
  104.     {
  105.         switch($type) {
  106.             case self::INI:
  107.                 return new ParseIniString();
  108.                 break;
  109.             case self::XML:
  110.                 return new ParseXmlString();
  111.                 break;
  112.         }
  113.     }
  114. }
  115.  
  116. class ParseFileFactory extends ParseFactory{
  117.     /**
  118.      * @see ParseFactory
  119.      */
  120.     public static function createFactory($type)
  121.     {
  122.         switch($type) {
  123.             case self::INI:
  124.                 return new ParseIniFile();
  125.                 break;
  126.             case self::XML:
  127.                 return new ParseXmlFile();
  128.                 break;
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement