2015-04-27 14:05:31 +00:00
|
|
|
<?php
|
2018-08-09 09:49:10 +00:00
|
|
|
namespace PortableInfobox\Parser\Nodes;
|
2015-05-07 12:37:13 +00:00
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
class NodeGroup extends Node {
|
2021-09-10 02:52:19 +00:00
|
|
|
private const LAYOUT_ATTR_NAME = 'layout';
|
|
|
|
private const SHOW_ATTR_NAME = 'show';
|
|
|
|
private const LAYOUT_DEFAULT_OPTION = 'default';
|
|
|
|
private const LAYOUT_HORIZONTAL_OPTION = 'horizontal';
|
|
|
|
private const SHOW_DEFAULT_OPTION = 'default';
|
|
|
|
private const SHOW_INCOMPLETE_OPTION = 'incomplete';
|
|
|
|
private const COLLAPSE_ATTR_NAME = 'collapse';
|
|
|
|
private const COLLAPSE_OPEN_OPTION = 'open';
|
|
|
|
private const COLLAPSE_CLOSED_OPTION = 'closed';
|
|
|
|
private const ROW_ITEMS_ATTR_NAME = 'row-items';
|
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(),
|
2017-01-10 12:05:22 +00:00
|
|
|
'collapse' => $this->getCollapse(),
|
2019-02-02 22:34:48 +00:00
|
|
|
'row-items' => $this->getRowItems(),
|
|
|
|
'item-name' => $this->getItemName()
|
2015-10-26 21:59:34 +00:00
|
|
|
];
|
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(
|
2021-09-10 02:52:19 +00:00
|
|
|
static function ( Node $item ) {
|
2015-07-03 10:16:18 +00:00
|
|
|
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(),
|
2017-01-10 12:05:22 +00:00
|
|
|
'collapse' => $this->getCollapse(),
|
2019-02-02 22:34:48 +00:00
|
|
|
'row-items' => $this->getRowItems(),
|
|
|
|
'item-name' => $this->getItemName()
|
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
|
|
|
|
2016-12-09 13:01:49 +00:00
|
|
|
public function getSources() {
|
|
|
|
return $this->getSourcesForChildren();
|
2015-06-10 15:19:40 +00:00
|
|
|
}
|
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()
|
|
|
|
];
|
2016-03-31 09:58:34 +00:00
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
|
2018-10-02 07:41:19 +00:00
|
|
|
return ( isset( $show ) && in_array( strtolower( $show ), $this->supportedGroupDisplays ) ) ?
|
|
|
|
$show : self::SHOW_DEFAULT_OPTION;
|
2015-07-03 10:16:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 21:59:34 +00:00
|
|
|
protected function getCollapse() {
|
2016-12-29 09:16:53 +00:00
|
|
|
$collapse = $this->getXmlAttribute( $this->xmlNode, self::COLLAPSE_ATTR_NAME );
|
2018-10-02 07:41:19 +00:00
|
|
|
return ( isset( $collapse ) && in_array( $collapse, $this->supportedGroupCollapses ) ) ?
|
|
|
|
$collapse : null;
|
2015-10-26 21:59:34 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-10-02 07:41:19 +00:00
|
|
|
return ( isset( $layout ) && in_array( $layout, $this->supportedGroupLayouts ) ) ?
|
|
|
|
$layout : self::LAYOUT_DEFAULT_OPTION;
|
2015-06-15 12:58:48 +00:00
|
|
|
}
|
2017-01-10 12:05:22 +00:00
|
|
|
|
|
|
|
protected function getRowItems() {
|
|
|
|
$rowItems = $this->getXmlAttribute( $this->xmlNode, self::ROW_ITEMS_ATTR_NAME );
|
|
|
|
|
2017-01-10 13:28:24 +00:00
|
|
|
return ( isset( $rowItems ) && ctype_digit( $rowItems ) ) ? intval( $rowItems ) : null;
|
2017-01-10 12:05:22 +00:00
|
|
|
}
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|