TrackManiaControl/application/core/Libs/FML/Elements/FrameModel.php

112 lines
2.2 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;
/**
* 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-01-19 19:30:21 +01:00
* Protected Properties
*/
protected $tagName = 'framemodel';
protected $id = '';
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;
/**
* Set Model Id
*
* @param string $id Model Id
* @return \FML\Elements\FrameModel
*/
public function setId($id) {
2014-05-14 23:24:00 +02:00
$this->id = (string)$id;
2014-01-19 19:30:21 +01:00
return $this;
}
/**
* Get Model Id
*
* @return string
*/
public function getId() {
return $this->id;
}
/**
* Assign an Id if necessary
*
* @return string
*/
public function checkId() {
if (!$this->id) {
$this->id = uniqid();
}
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) {
2014-05-14 23:24:00 +02:00
/** @var Renderable $child */
2014-01-19 19:30:21 +01:00
$childElement = $child->render($domDocument);
$xmlElement->appendChild($childElement);
}
return $xmlElement;
}
}