You are here

class XML2Array in Opigno SCORM 3.x

Same name and namespace in other branches
  1. 8 src/XML2Array.php \Drupal\opigno_scorm\XML2Array

Class XML2Array.

Hierarchy

Expanded class hierarchy of XML2Array

File

src/XML2Array.php, line 8

Namespace

Drupal\opigno_scorm
View source
class XML2Array {
  private $output = [];
  private $xml_parser;
  private $xml_string;

  /**
   * Parse function.
   */
  public function parse($xlm_string) {
    $this->xml_parser = xml_parser_create();
    xml_set_object($this->xml_parser, $this);
    xml_set_element_handler($this->xml_parser, "tagOpen", "tagClosed");
    xml_set_character_data_handler($this->xml_parser, "tagData");
    $this->xml_string = xml_parse($this->xml_parser, $xlm_string);
    if (!$this->xml_string) {
    }
    xml_parser_free($this->xml_parser);
    return $this->output;
  }

  /**
   * Tag open function.
   */
  public function tagOpen($parser, $name, $attrs) {
    $tag = [
      "name" => $name,
      "attrs" => $attrs,
    ];
    array_push($this->output, $tag);
  }

  /**
   * Tag data function.
   */
  public function tagData($parser, $tagData) {
    if (trim($tagData)) {
      if (isset($this->output[count($this->output) - 1]['tagData'])) {
        $this->output[count($this->output) - 1]['tagData'] .= $tagData;
      }
      else {
        $this->output[count($this->output) - 1]['tagData'] = $tagData;
      }
    }
  }

  /**
   * Tag closed function.
   */
  public function tagClosed($parser, $name) {
    $this->output[count($this->output) - 2]['children'][] = $this->output[count($this->output) - 1];
    array_pop($this->output);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
XML2Array::$output private property
XML2Array::$xml_parser private property
XML2Array::$xml_string private property
XML2Array::parse public function Parse function.
XML2Array::tagClosed public function Tag closed function.
XML2Array::tagData public function Tag data function.
XML2Array::tagOpen public function Tag open function.