public function testParseText() { $parser = new Parser(); /** @var Element $element */ $element = $parser->parse('<some-element>Some text</some-element>'); $this->assertInstanceOf(Element::class, $element); $this->assertEquals(1, count($element)); /** @var Text $textChild */ $textChild = $element->getChildAt(0); $this->assertInstanceOf(Text::class, $textChild); $this->assertEquals('Some text', $element->getText(), 'element text'); $this->assertEquals('Some text', $textChild->getText(), 'text child text'); }
public function formatText($text, $level = null) { $indent = $this->getIndentation($level ?: 0); $newLine = $this->getNewLine(); $text = Parser::normalize($text); if ($this->isPretty() && !$this->isShortText($text)) { $text = wordwrap($text, $this->options['line_limit'], "{$newLine}{$indent}"); } return $indent . $text; }
/** * @param string $string * * @return Element * @throws Exception */ public function parse($string) { $this->internalParser = xml_parser_create($this->encoding); xml_set_object($this->internalParser, $this); xml_parser_set_option($this->internalParser, \XML_OPTION_CASE_FOLDING, false); xml_parser_set_option($this->internalParser, \XML_OPTION_SKIP_WHITE, true); xml_set_element_handler($this->internalParser, 'handleOpenTag', 'handleCloseTag'); xml_set_character_data_handler($this->internalParser, 'handleText'); $string = Parser::normalize($string); $this->currentElement = null; if (!xml_parse($this->internalParser, Parser::normalize($string))) { $this->throwException(); } if (is_resource($this->internalParser)) { xml_parser_free($this->internalParser); } return $this->currentElement; }