<?php
namespace Moto\Website; use Moto; class PageSectionContentStructure { protected $_data = null; protected $_parent = null; public function __construct($parent) { $this->_parent = $parent; } public function setRawStructure($data) { $this->_data = $data; } public function convertContent() { $t0 = microtime(1); $sectionData = array( 'name' => 'section', 'properties' => array(), 'id' => Moto\Util::getValue($this->_data, 'id'), 'children' => array(), ); $childrenIds = Moto\Util::getValue($this->_data, 'childrenIds'); $sectionData['children'] = $this->_fillChildren($childrenIds); unset($sectionData['childrenIds']); if (Moto\System::isDevelopmentStage()) { Moto\System\Log::debug('PageSectionContentStructure:convertContent done by ' . (microtime(1) - $t0)); } return $sectionData; } public function getRawSection() { $sectionData = array( 'name' => 'section', 'properties' => array(), 'id' => Moto\Util::getValue($this->_data, 'id'), 'children' => Moto\Util::getValue($this->_data, 'childrenIds'), ); return $sectionData; } protected function _fillChildren($childrenIds) { $children = array(); if (empty($childrenIds) || !is_array($childrenIds)) { return $children; } foreach ($childrenIds as $widgetId) { $data = $this->_fillWidgetData($this->_parent->getWidgetDataById($widgetId)); if (!$data) { continue; } $children[] = $data; } return $children; } protected function _fillWidgetData($widgetData) { if (!is_array($widgetData)) { return null; } unset($widgetData['parent']); $childrenIds = Moto\Util::getValue($widgetData, 'childrenIds'); unset($widgetData['childrenIds']); if (!empty($childrenIds)) { $widgetData['children'] = $this->_fillChildren($childrenIds); } return $widgetData; } public function toArray() { return $this->_data; } public function toJson($options = 0) { return Moto\Util::toJson($this->toArray(), $options); } }