Blame view

lib/XmlParser.php 2.91 KB
8e128526   Mihail   add xlsx parser
1
2
3
4
5
6
7
8
  <?php
  /**
   * Created by PhpStorm.
   * User: Cibermag
   * Date: 10.09.2015
   * Time: 17:47
   */
  
d0261fd1   Mihail   fixed namespace i...
9
  namespace yii\multiparser;
8e128526   Mihail   add xlsx parser
10
11
  
  
8e128526   Mihail   add xlsx parser
12
13
14
15
16
17
  class XmlParser extends  Parser{
  
      public $node;
  
      public function read()
      {
a3b2aa93   Mihail   change meaning of...
18
          $result = $this->parseToArray( );
8e128526   Mihail   add xlsx parser
19
20
21
22
23
24
  
          if ( isset($this->node) ) {
  
              $result = $result[ $this->node ];
  
          }
cd8b9f70   Mihail   add cleanUp metho...
25
          $this->cleanUp();
8e128526   Mihail   add xlsx parser
26
27
28
29
30
31
          return $result;
      }
  
  
      /**
       * Converts an XML string to a PHP array
cd8b9f70   Mihail   add cleanUp metho...
32
33
34
35
       * @param $file_path
       * @return mixed
       * @throws Exception
       * @throws \Exception
8e128526   Mihail   add xlsx parser
36
       */
a3b2aa93   Mihail   change meaning of...
37
      protected function parseToArray( ) {
8e128526   Mihail   add xlsx parser
38
          try {
221da14e   Mihail   change SplFileObj...
39
              $xml = new \SimpleXMLElement( $this->file_path, 0, true );
8e128526   Mihail   add xlsx parser
40
              //\common\components\CustomVarDamp::dumpAndDie($xml->children()->children());
a3b2aa93   Mihail   change meaning of...
41
              $result = $this->recursiveParseToArray( $xml );
221da14e   Mihail   change SplFileObj...
42
          } catch(\Exception $ex) {
8e128526   Mihail   add xlsx parser
43
44
45
  
              throw $ex;
          }
8e128526   Mihail   add xlsx parser
46
47
48
49
50
51
52
53
54
55
56
          return $result;
      }
  
      /**
       * Convert a XML string to a PHP array recursively. Do not
       * call this function directly
       *
       * @param SimpleXMLElement
       *
       * @return mixed
       */
a3b2aa93   Mihail   change meaning of...
57
      protected  function recursiveParseToArray($xml) {
8e128526   Mihail   add xlsx parser
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
          if( $xml instanceof \SimpleXMLElement ) {
              $attributes = $xml->attributes();
  
              foreach( $attributes as $key => $value ) {
                  if( $value ) {
                      $attribute_array[$key] = (string) $value;
                  }
              }
              $previous_xml = $xml;
              $xml = get_object_vars($xml);
          }
  
          if(is_array($xml)) {
  
              if( count($xml) == 0 )
                  return (string) $previous_xml; // for CDATA
  
              foreach($xml as $key => $value) {
a3b2aa93   Mihail   change meaning of...
76
                  $row[$key] = $this->recursiveParseToArray($value);
8e128526   Mihail   add xlsx parser
77
78
79
80
81
82
83
84
85
86
87
88
              }
              if ( is_string($value) ) {
                  // дошли до конца рекурсии
                  // преобразуем ряд согласно конфигурации
                  if ( $this->keys !== NULL ) {
                      // назначим ключи из конфигурации, согласно массиву $keys
                      $row = $this->compareArrayWithKeys( $row );
                  }
                  $row = $this->convert( $row );
  
              }
  
8e128526   Mihail   add xlsx parser
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
              if( isset( $attribute_array ) )
                  $row['@'] = $attribute_array; // Attributes
  
              return $row;
          }
          return (string) $xml;
      }
  
      /**
       * @param array $value_arr - текущий ряд, массив, которому нужно назначить конфигурационные ключи ($keys)
       * @return array
       */
      protected function compareArrayWithKeys( array $value_arr ){
          $res = $this->keys;
          foreach ( $this->keys as $key => $value ) {
              if ( array_key_exists( $value, $value_arr ) ) {
                  $res[$key] = $value_arr[$value];
              }
          }
          return $res;
      }
  
  }