2015-04-27 14:05:31 +00:00
|
|
|
<?php
|
|
|
|
namespace Wikia\PortableInfobox\Parser\Nodes;
|
|
|
|
|
|
|
|
use Wikia\PortableInfobox\Parser\ExternalParser;
|
2015-05-07 12:37:13 +00:00
|
|
|
use Wikia\PortableInfobox\Parser\SimpleParser;
|
2015-04-27 14:05:31 +00:00
|
|
|
|
|
|
|
class Node {
|
|
|
|
|
|
|
|
const DATA_SRC_ATTR_NAME = 'source';
|
|
|
|
const DEFAULT_TAG_NAME = 'default';
|
|
|
|
const VALUE_TAG_NAME = 'value';
|
|
|
|
const LABEL_TAG_NAME = 'label';
|
|
|
|
|
|
|
|
protected $xmlNode;
|
|
|
|
|
|
|
|
/* @var $externalParser ExternalParser */
|
|
|
|
protected $externalParser;
|
|
|
|
|
|
|
|
public function __construct( \SimpleXMLElement $xmlNode, $infoboxData ) {
|
|
|
|
$this->xmlNode = $xmlNode;
|
|
|
|
$this->infoboxData = $infoboxData;
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:37:13 +00:00
|
|
|
/**
|
|
|
|
* @return ExternalParser
|
|
|
|
*/
|
|
|
|
public function getExternalParser() {
|
|
|
|
if ( !isset( $this->externalParser ) ) {
|
|
|
|
$this->setExternalParser( new SimpleParser() );
|
|
|
|
}
|
|
|
|
return $this->externalParser;
|
|
|
|
}
|
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
/**
|
|
|
|
* @param mixed $externalParser
|
|
|
|
*/
|
|
|
|
public function setExternalParser( ExternalParser $externalParser ) {
|
|
|
|
$this->externalParser = $externalParser;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getType() {
|
|
|
|
return $this->xmlNode->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getData() {
|
2015-05-07 12:37:13 +00:00
|
|
|
return [ 'value' => (string)$this->xmlNode ];
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isEmpty( $data ) {
|
|
|
|
return !( isset( $data[ 'value' ] ) ) || empty( $data[ 'value' ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getValueWithDefault( \SimpleXMLElement $xmlNode ) {
|
|
|
|
$source = $this->getXmlAttribute( $xmlNode, self::DATA_SRC_ATTR_NAME );
|
|
|
|
$value = null;
|
|
|
|
if ( !empty( $source ) ) {
|
|
|
|
$value = $this->getInfoboxData( $source );
|
|
|
|
}
|
|
|
|
if ( !$value ) {
|
|
|
|
if ( $xmlNode->{self::DEFAULT_TAG_NAME} ) {
|
2015-05-07 12:37:13 +00:00
|
|
|
$value = (string)$xmlNode->{self::DEFAULT_TAG_NAME};
|
|
|
|
$value = $this->getExternalParser()->parseRecursive( $value );
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:37:13 +00:00
|
|
|
protected function getXmlAttribute( \SimpleXMLElement $xmlNode, $attribute ) {
|
|
|
|
if ( isset( $xmlNode[ $attribute ] ) )
|
|
|
|
return (string)$xmlNode[ $attribute ];
|
2015-04-27 14:05:31 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getInfoboxData( $key ) {
|
|
|
|
$data = isset( $this->infoboxData[ $key ] ) ? $this->infoboxData[ $key ] : null;
|
2015-05-07 13:19:46 +00:00
|
|
|
return $this->getExternalParser()->parseRecursive( $data );
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
}
|