PortableInfobox/includes/services/Parser/Nodes/NodeGroup.php

116 lines
2.9 KiB
PHP
Raw Normal View History

<?php
namespace PortableInfobox\Parser\Nodes;
class NodeGroup extends Node {
2015-07-03 10:16:18 +00:00
const LAYOUT_ATTR_NAME = 'layout';
const SHOW_ATTR_NAME = 'show';
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';
const COLLAPSE_ATTR_NAME = 'collapse';
const COLLAPSE_OPEN_OPTION = 'open';
const COLLAPSE_CLOSED_OPTION = 'closed';
const ROW_ITEMS_ATTR_NAME = 'row-items';
2015-06-10 16:07:28 +00:00
private $supportedGroupLayouts = [
self::LAYOUT_DEFAULT_OPTION,
self::LAYOUT_HORIZONTAL_OPTION
2015-06-10 16:07:28 +00:00
];
2015-07-03 10:16:18 +00:00
private $supportedGroupDisplays = [
self::SHOW_DEFAULT_OPTION,
2015-07-03 10:16:18 +00:00
self::SHOW_INCOMPLETE_OPTION
];
private $supportedGroupCollapses = [
self::COLLAPSE_OPEN_OPTION,
self::COLLAPSE_CLOSED_OPTION
];
public function getData() {
if ( !isset( $this->data ) ) {
$this->data = [
'value' => $this->getDataForChildren(),
'layout' => $this->getLayout(),
'collapse' => $this->getCollapse(),
'row-items' => $this->getRowItems()
];
}
return $this->data;
}
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(),
'data' => [
'value' => $value,
'layout' => $this->getLayout(),
'collapse' => $this->getCollapse(),
'row-items' => $this->getRowItems()
],
2015-06-11 09:34:28 +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() ) {
return false;
}
}
return true;
}
public function getSources() {
return $this->getSourcesForChildren();
}
2015-06-15 12:58:48 +00:00
2017-01-02 09:58:02 +00:00
public function getMetadata() {
return [
'type' => $this->getType(),
'metadata' => $this->getMetadataForChildren()
];
}
2015-07-03 10:16:18 +00:00
protected function showIncomplete() {
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 );
return ( isset( $show ) && in_array( strtolower( $show ), $this->supportedGroupDisplays ) ) ? $show
: self::SHOW_DEFAULT_OPTION;
2015-07-03 10:16:18 +00:00
}
protected function getCollapse() {
$collapse = $this->getXmlAttribute( $this->xmlNode, self::COLLAPSE_ATTR_NAME );
return ( isset( $collapse ) && in_array( $collapse, $this->supportedGroupCollapses ) ) ? $collapse : 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
: self::LAYOUT_DEFAULT_OPTION;
2015-06-15 12:58:48 +00:00
}
protected function getRowItems() {
$rowItems = $this->getXmlAttribute( $this->xmlNode, self::ROW_ITEMS_ATTR_NAME );
return ( isset( $rowItems ) && ctype_digit( $rowItems ) ) ? intval( $rowItems ) : null;
}
}