TrackManiaControl/libs/FML/Elements/FrameModel.php

113 lines
2.3 KiB
PHP
Raw Normal View History

2014-01-19 19:30:21 +01:00
<?php
namespace FML\Elements;
use FML\Types\Container;
use FML\Types\Renderable;
2014-06-21 03:18:21 +02:00
use FML\UniqueID;
2014-01-19 19:30:21 +01:00
/**
* Class representing a Frame Model
*
2014-05-20 15:44:45 +02:00
* @author steeffeen <mail@steeffeen.com>
2014-04-13 18:21:40 +02:00
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
2014-05-14 23:24:00 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-01-19 19:30:21 +01:00
*/
class FrameModel implements Container, Renderable {
2014-01-21 20:30:40 +01:00
/*
2014-06-21 03:18:21 +02:00
* Protected properties
2014-01-19 19:30:21 +01:00
*/
protected $tagName = 'framemodel';
2014-06-21 03:18:21 +02:00
protected $modelId = null;
/** @var Renderable[] $children */
2014-01-19 19:30:21 +01:00
protected $children = array();
2014-05-14 23:24:00 +02:00
/** @var Format $format */
2014-01-19 19:30:21 +01:00
protected $format = null;
/**
2014-06-21 03:18:21 +02:00
* Set Model id
2014-01-19 19:30:21 +01:00
*
2014-06-21 03:18:21 +02:00
* @param string $modelId Model id
2014-07-03 22:34:47 +02:00
* @return static
2014-01-19 19:30:21 +01:00
*/
2014-06-21 03:18:21 +02:00
public function setId($modelId) {
$this->modelId = (string)$modelId;
2014-01-19 19:30:21 +01:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Get Model id
2014-01-19 19:30:21 +01:00
*
* @return string
*/
public function getId() {
2014-06-21 03:18:21 +02:00
return $this->modelId;
2014-01-19 19:30:21 +01:00
}
/**
2014-06-21 03:18:21 +02:00
* Assign an id if necessary
2014-01-19 19:30:21 +01:00
*
* @return string
*/
public function checkId() {
2014-06-21 03:18:21 +02:00
if (!$this->modelId) {
2014-07-03 22:34:47 +02:00
$this->setId(UniqueID::create());
2014-01-19 19:30:21 +01:00
}
return $this;
}
/**
* @see \FML\Types\Container::add()
*/
2014-05-14 23:24:00 +02:00
public function add(Renderable $childElement) {
if (!in_array($childElement, $this->children, true)) {
array_push($this->children, $childElement);
2014-01-19 19:30:21 +01:00
}
return $this;
}
/**
* @see \FML\Types\Container::removeChildren()
*/
public function removeChildren() {
$this->children = array();
return $this;
}
/**
* @see \FML\Types\Container::setFormat()
*/
public function setFormat(Format $format) {
$this->format = $format;
return $this;
}
/**
* @see \FML\Types\Container::getFormat()
*/
public function getFormat($createIfEmpty = true) {
if (!$this->format && $createIfEmpty) {
2014-04-13 18:21:40 +02:00
$this->setFormat(new Format());
2014-01-19 19:30:21 +01:00
}
return $this->format;
}
/**
* @see \FML\Types\Renderable::render()
*/
public function render(\DOMDocument $domDocument) {
$xmlElement = $domDocument->createElement($this->tagName);
$this->checkId();
$xmlElement->setAttribute('id', $this->getId());
if ($this->format) {
$formatXml = $this->format->render($domDocument);
$xmlElement->appendChild($formatXml);
}
foreach ($this->children as $child) {
$childElement = $child->render($domDocument);
$xmlElement->appendChild($childElement);
}
return $xmlElement;
}
}