2015-06-10 09:28:33 +00:00
|
|
|
<?php
|
2022-03-11 20:35:51 +00:00
|
|
|
|
2018-08-09 09:49:10 +00:00
|
|
|
namespace PortableInfobox\Parser\Nodes;
|
2015-06-10 09:28:33 +00:00
|
|
|
|
2018-08-09 09:49:10 +00:00
|
|
|
use PortableInfobox\Parser\XmlParser;
|
2022-03-11 20:35:51 +00:00
|
|
|
use SimpleXMLElement;
|
2015-06-10 15:19:40 +00:00
|
|
|
|
2015-06-10 09:28:33 +00:00
|
|
|
class NodeFactory {
|
|
|
|
|
2018-08-16 09:25:53 +00:00
|
|
|
public static function newFromXML( $text, array $data = [] ) {
|
2015-06-10 15:19:40 +00:00
|
|
|
return self::getInstance( XmlParser::parseXmlString( $text ), $data );
|
|
|
|
}
|
|
|
|
|
2022-03-11 20:35:51 +00:00
|
|
|
public static function newFromSimpleXml( SimpleXMLElement $xmlNode, array $data = [] ) {
|
2015-06-10 15:19:40 +00:00
|
|
|
return self::getInstance( $xmlNode, $data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-11 20:35:51 +00:00
|
|
|
* @param SimpleXMLElement $xmlNode
|
2015-06-10 15:19:40 +00:00
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Node|NodeUnimplemented
|
|
|
|
*/
|
2022-03-11 20:35:51 +00:00
|
|
|
protected static function getInstance( SimpleXMLElement $xmlNode, array $data ) {
|
2015-06-10 09:28:33 +00:00
|
|
|
$tagType = $xmlNode->getName();
|
2022-03-11 20:35:51 +00:00
|
|
|
$className = Node::class . mb_convert_case( mb_strtolower( $tagType ), MB_CASE_TITLE );
|
2015-06-10 09:28:33 +00:00
|
|
|
if ( class_exists( $className ) ) {
|
2022-03-11 20:35:51 +00:00
|
|
|
/* @var $instance Node */
|
2015-06-10 09:28:33 +00:00
|
|
|
$instance = new $className( $xmlNode, $data );
|
2015-06-10 15:19:40 +00:00
|
|
|
|
2018-08-22 08:04:07 +00:00
|
|
|
if ( $instance instanceof Node ) {
|
|
|
|
return $instance;
|
|
|
|
}
|
2015-06-10 09:28:33 +00:00
|
|
|
}
|
2015-06-10 15:19:40 +00:00
|
|
|
|
2015-06-10 09:28:33 +00:00
|
|
|
return new NodeUnimplemented( $xmlNode, $data );
|
|
|
|
}
|
|
|
|
|
2018-08-16 09:25:53 +00:00
|
|
|
}
|