Updated to ManiaLink v3
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
namespace FML\Elements;
|
||||
|
||||
use FML\Types\Container;
|
||||
use FML\Types\Identifiable;
|
||||
use FML\Types\Renderable;
|
||||
use FML\UniqueID;
|
||||
|
||||
@ -10,103 +11,172 @@ use FML\UniqueID;
|
||||
* Class representing a Frame Model
|
||||
*
|
||||
* @author steeffeen <mail@steeffeen.com>
|
||||
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
|
||||
* @copyright FancyManiaLinks Copyright © 2017 Steffen Schröder
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class FrameModel implements Container, Renderable {
|
||||
/*
|
||||
* Protected properties
|
||||
*/
|
||||
protected $tagName = 'framemodel';
|
||||
protected $modelId = null;
|
||||
/** @var Renderable[] $children */
|
||||
protected $children = array();
|
||||
/** @var Format $format */
|
||||
protected $format = null;
|
||||
class FrameModel implements Container, Identifiable, Renderable
|
||||
{
|
||||
|
||||
/**
|
||||
* Set Model id
|
||||
*
|
||||
* @param string $modelId Model id
|
||||
* @return static
|
||||
*/
|
||||
public function setId($modelId) {
|
||||
$this->modelId = (string)$modelId;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @var string $modelId Model id
|
||||
*/
|
||||
protected $modelId = null;
|
||||
|
||||
/**
|
||||
* Get Model id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getId() {
|
||||
return $this->modelId;
|
||||
}
|
||||
/**
|
||||
* @var Renderable[] $children Children
|
||||
*/
|
||||
protected $children = array();
|
||||
|
||||
/**
|
||||
* Assign an id if necessary
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function checkId() {
|
||||
if (!$this->modelId) {
|
||||
$this->setId(UniqueID::create());
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @var Format $format Format
|
||||
*/
|
||||
protected $format = null;
|
||||
|
||||
/**
|
||||
* @see \FML\Types\Container::add()
|
||||
*/
|
||||
public function add(Renderable $childElement) {
|
||||
if (!in_array($childElement, $this->children, true)) {
|
||||
array_push($this->children, $childElement);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* Create a new Frame Model
|
||||
*
|
||||
* @api
|
||||
* @param string $modelId Model id
|
||||
* @param Renderable[] $children Children
|
||||
* @return static
|
||||
*/
|
||||
public static function create($modelId = null, array $children = null)
|
||||
{
|
||||
return new static($modelId, $children);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see \FML\Types\Container::removeChildren()
|
||||
*/
|
||||
public function removeChildren() {
|
||||
$this->children = array();
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* Construct a new Frame Model
|
||||
*
|
||||
* @api
|
||||
* @param string $modelId Model id
|
||||
* @param Renderable[] $children Children
|
||||
*/
|
||||
public function __construct($modelId = null, array $children = null)
|
||||
{
|
||||
if ($modelId) {
|
||||
$this->setId($modelId);
|
||||
}
|
||||
if ($children) {
|
||||
$this->addChildren($children);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see \FML\Types\Container::setFormat()
|
||||
*/
|
||||
public function setFormat(Format $format) {
|
||||
$this->format = $format;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @see Identifiable::getId()
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
if (!$this->modelId) {
|
||||
return $this->createId();
|
||||
}
|
||||
return $this->modelId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see \FML\Types\Container::getFormat()
|
||||
*/
|
||||
public function getFormat($createIfEmpty = true) {
|
||||
if (!$this->format && $createIfEmpty) {
|
||||
$this->setFormat(new Format());
|
||||
}
|
||||
return $this->format;
|
||||
}
|
||||
/**
|
||||
* @see Identifiable::setId()
|
||||
*/
|
||||
public function setId($modelId)
|
||||
{
|
||||
$this->modelId = (string)$modelId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Identifiable::checkId()
|
||||
*/
|
||||
public function checkId()
|
||||
{
|
||||
return UniqueID::check($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new model id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function createId()
|
||||
{
|
||||
$modelId = UniqueID::create();
|
||||
$this->setId($modelId);
|
||||
return $this->getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Container::getChildren()
|
||||
*/
|
||||
public function getChildren()
|
||||
{
|
||||
return $this->children;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Container::addChild()
|
||||
*/
|
||||
public function addChild(Renderable $childElement)
|
||||
{
|
||||
if (!in_array($childElement, $this->children, true)) {
|
||||
array_push($this->children, $childElement);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Container::addChildren()
|
||||
*/
|
||||
public function addChildren(array $children)
|
||||
{
|
||||
foreach ($children as $child) {
|
||||
$this->addChild($child);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Container::removeAllChildren()
|
||||
*/
|
||||
public function removeAllChildren()
|
||||
{
|
||||
$this->children = array();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Container::getFormat()
|
||||
*/
|
||||
public function getFormat()
|
||||
{
|
||||
return $this->format;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Container::setFormat()
|
||||
*/
|
||||
public function setFormat(Format $format = null)
|
||||
{
|
||||
$this->format = $format;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Renderable::render()
|
||||
*/
|
||||
public function render(\DOMDocument $domDocument)
|
||||
{
|
||||
$domElement = $domDocument->createElement("framemodel");
|
||||
$domElement->setAttribute("id", $this->getId());
|
||||
|
||||
if ($this->format) {
|
||||
$formatElement = $this->format->render($domDocument);
|
||||
$domElement->appendChild($formatElement);
|
||||
}
|
||||
|
||||
foreach ($this->children as $child) {
|
||||
$childElement = $child->render($domDocument);
|
||||
$domElement->appendChild($childElement);
|
||||
}
|
||||
|
||||
return $domElement;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user