third_party
3 years ago
IQuery.php
3 years ago
LICENSE
4 years ago
gan_formatter.php
3 years ago
gan_node_html.php
3 years ago
gan_parser_html.php
3 years ago
gan_selector_html.php
3 years ago
gan_tokenizer.php
3 years ago
gan_xml2array.php
3 years ago
ganon.php
3 years ago
index.php
4 years ago
pQuery.php
3 years ago
gan_xml2array.php
103 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | /** |
| 3 | * @author Niels A.D. |
| 4 | * @author Todd Burry <todd@vanillaforums.com> |
| 5 | * @copyright 2010 Niels A.D., 2014 Todd Burry |
| 6 | * @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1 |
| 7 | * @package pQuery |
| 8 | */ |
| 9 | |
| 10 | namespace MailPoetVendor\pQuery; |
| 11 | |
| 12 | if (!defined('ABSPATH')) exit; |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * Converts a XML document to an array |
| 17 | */ |
| 18 | class XML2ArrayParser extends HtmlParserBase { |
| 19 | |
| 20 | /** |
| 21 | * Holds the document structure |
| 22 | * @var array array('name' => 'tag', 'attrs' => array('attr' => 'val'), 'childen' => array()) |
| 23 | */ |
| 24 | var $root = array( |
| 25 | 'name' => '', |
| 26 | 'attrs' => array(), |
| 27 | 'children' => array() |
| 28 | ); |
| 29 | |
| 30 | /** |
| 31 | * Current parsing hierarchy |
| 32 | * @var array |
| 33 | * @access private |
| 34 | */ |
| 35 | var $hierarchy = array(); |
| 36 | |
| 37 | protected function parse_hierarchy($self_close) { |
| 38 | if ($this->status['closing_tag']) { |
| 39 | $found = false; |
| 40 | for ($count = count($this->hierarchy), $i = $count - 1; $i >= 0; $i--) { |
| 41 | if (strcasecmp($this->hierarchy[$i]['name'], $this->status['tag_name']) === 0) { |
| 42 | |
| 43 | for($ii = ($count - $i - 1); $ii >= 0; $ii--) { |
| 44 | $e = array_pop($this->hierarchy); |
| 45 | if ($ii > 0) { |
| 46 | $this->addError('Closing tag "'.$this->status['tag_name'].'" while "'.$e['name'].'" is not closed yet'); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | $found = true; |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if (!$found) { |
| 56 | $this->addError('Closing tag "'.$this->status['tag_name'].'" which is not open'); |
| 57 | } |
| 58 | } else { |
| 59 | $tag = array( |
| 60 | 'name' => $this->status['tag_name'], |
| 61 | 'attrs' => $this->status['attributes'], |
| 62 | 'children' => array() |
| 63 | ); |
| 64 | if ($this->hierarchy) { |
| 65 | $current =& $this->hierarchy[count($this->hierarchy) - 1]; |
| 66 | $current['children'][] = $tag; |
| 67 | $tag =& $current['children'][count($current['children']) - 1]; |
| 68 | unset($current['tagData']); |
| 69 | } else { |
| 70 | $this->root = $tag; |
| 71 | $tag =& $this->root; |
| 72 | $self_close = false; |
| 73 | } |
| 74 | if (!$self_close) { |
| 75 | $this->hierarchy[] =& $tag; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | function parse_tag_default() { |
| 81 | if (!parent::parse_tag_default()) {return false;} |
| 82 | |
| 83 | if ($this->status['tag_name'][0] !== '?') { |
| 84 | $this->parse_hierarchy(($this->status['self_close']) ? true : null); |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | function parse_text() { |
| 90 | parent::parse_text(); |
| 91 | if (($this->status['text'] !== '') && $this->hierarchy) { |
| 92 | $current =& $this->hierarchy[count($this->hierarchy) - 1]; |
| 93 | if (!$current['children']) { |
| 94 | $current['tagData'] = $this->status['text']; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | function parse_all() { |
| 100 | return ((parent::parse_all()) ? $this->root : false); |
| 101 | } |
| 102 | } |
| 103 |