2015-04-27 14:05:31 +00:00
|
|
|
<?php
|
|
|
|
namespace Wikia\PortableInfobox\Parser\Nodes;
|
2015-05-07 12:37:13 +00:00
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
class NodeGroup extends Node {
|
2015-07-03 10:16:18 +00:00
|
|
|
const LAYOUT_ATTR_NAME = 'layout';
|
|
|
|
const SHOW_ATTR_NAME = 'show';
|
2015-07-02 09:37:00 +00:00
|
|
|
const LAYOUT_DEFAULT_OPTION = 'default';
|
|
|
|
const LAYOUT_HORIZONTAL_OPTION = 'horizontal';
|
|
|
|
const SHOW_DEFAULT_OPTION = 'default';
|
2015-07-03 10:16:18 +00:00
|
|
|
const SHOW_INCOMPLETE_OPTION = 'incomplete';
|
2015-10-26 21:59:34 +00:00
|
|
|
const COLLAPSE_ATTR_NAME = 'collapse';
|
|
|
|
const COLLAPSE_OPEN_OPTION = 'open';
|
|
|
|
const COLLAPSE_CLOSED_OPTION = 'closed';
|
2015-06-10 16:07:28 +00:00
|
|
|
|
|
|
|
private $supportedGroupLayouts = [
|
2015-07-02 09:37:00 +00:00
|
|
|
self::LAYOUT_DEFAULT_OPTION,
|
|
|
|
self::LAYOUT_HORIZONTAL_OPTION
|
2015-06-10 16:07:28 +00:00
|
|
|
];
|
2015-04-27 14:05:31 +00:00
|
|
|
|
2015-07-03 10:16:18 +00:00
|
|
|
private $supportedGroupDisplays = [
|
2015-07-02 09:37:00 +00:00
|
|
|
self::SHOW_DEFAULT_OPTION,
|
2015-07-03 10:16:18 +00:00
|
|
|
self::SHOW_INCOMPLETE_OPTION
|
|
|
|
];
|
|
|
|
|
2015-10-26 21:59:34 +00:00
|
|
|
private $supportedGroupCollapses = [
|
|
|
|
self::COLLAPSE_OPEN_OPTION,
|
|
|
|
self::COLLAPSE_CLOSED_OPTION
|
|
|
|
];
|
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
public function getData() {
|
2015-06-10 09:28:33 +00:00
|
|
|
if ( !isset( $this->data ) ) {
|
2015-10-26 21:59:34 +00:00
|
|
|
$this->data = [
|
|
|
|
'value' => $this->getDataForChildren(),
|
|
|
|
'layout' => $this->getLayout(),
|
|
|
|
'collapse' => $this->getCollapse()
|
|
|
|
];
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
2015-06-10 09:28:33 +00:00
|
|
|
|
|
|
|
return $this->data;
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-11 09:34:28 +00:00
|
|
|
public function getRenderData() {
|
2015-07-03 10:16:18 +00:00
|
|
|
$value = $this->showIncomplete() ?
|
|
|
|
array_map(
|
|
|
|
function ( Node $item ) {
|
|
|
|
return $item->getRenderData();
|
|
|
|
},
|
|
|
|
$this->getChildNodes()
|
|
|
|
)
|
|
|
|
: $this->getRenderDataForChildren();
|
|
|
|
|
2015-06-11 09:34:28 +00:00
|
|
|
return [
|
|
|
|
'type' => $this->getType(),
|
2015-07-02 09:37:00 +00:00
|
|
|
'data' => [
|
|
|
|
'value' => $value,
|
2015-10-26 21:59:34 +00:00
|
|
|
'layout' => $this->getLayout(),
|
|
|
|
'collapse' => $this->getCollapse()
|
2015-07-02 09:37:00 +00:00
|
|
|
],
|
2015-06-11 09:34:28 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-06-10 09:28:33 +00:00
|
|
|
public function isEmpty() {
|
2015-06-11 09:34:28 +00:00
|
|
|
/** @var Node $item */
|
|
|
|
foreach ( $this->getChildNodes() as $item ) {
|
|
|
|
if ( !$item->isType( 'header' ) && !$item->isEmpty() ) {
|
2015-04-27 14:05:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-06-10 09:28:33 +00:00
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-06-10 15:19:40 +00:00
|
|
|
|
|
|
|
public function getSource() {
|
2015-06-11 09:48:32 +00:00
|
|
|
return $this->getSourceForChildren();
|
2015-06-10 15:19:40 +00:00
|
|
|
}
|
2015-06-15 12:58:48 +00:00
|
|
|
|
2016-03-31 09:58:34 +00:00
|
|
|
public function getSourceLabel() {
|
|
|
|
return $this->getSourceLabelForChildren();
|
|
|
|
}
|
|
|
|
|
2015-07-03 10:16:18 +00:00
|
|
|
protected function showIncomplete() {
|
2015-07-02 09:39:49 +00:00
|
|
|
return strcasecmp( $this->getDisplay(), self::SHOW_INCOMPLETE_OPTION ) === 0;
|
2015-07-03 10:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getDisplay() {
|
|
|
|
$show = $this->getXmlAttribute( $this->xmlNode, self::SHOW_ATTR_NAME );
|
|
|
|
|
2015-07-02 09:39:49 +00:00
|
|
|
return ( isset( $show ) && in_array( strtolower( $show ), $this->supportedGroupDisplays ) ) ? $show
|
2015-07-02 09:37:00 +00:00
|
|
|
: self::SHOW_DEFAULT_OPTION;
|
2015-07-03 10:16:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 21:59:34 +00:00
|
|
|
protected function getCollapse() {
|
|
|
|
$layout = $this->getXmlAttribute( $this->xmlNode, self::COLLAPSE_ATTR_NAME );
|
|
|
|
return ( isset( $layout ) && in_array( $layout, $this->supportedGroupCollapses ) ) ? $layout : null;
|
|
|
|
}
|
|
|
|
|
2015-06-15 12:58:48 +00:00
|
|
|
protected function getLayout() {
|
2015-07-03 10:16:18 +00:00
|
|
|
$layout = $this->getXmlAttribute( $this->xmlNode, self::LAYOUT_ATTR_NAME );
|
2015-06-15 12:58:48 +00:00
|
|
|
|
|
|
|
return ( isset( $layout ) && in_array( $layout, $this->supportedGroupLayouts ) ) ? $layout
|
2015-07-02 09:37:00 +00:00
|
|
|
: self::LAYOUT_DEFAULT_OPTION;
|
2015-06-15 12:58:48 +00:00
|
|
|
}
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|