Crell/HtmlModel

View on GitHub
src/Head/HeadElement.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace Crell\HtmlModel\Head;

use Crell\HtmlModel\AttributeBag;
use Crell\HtmlModel\ContentElementInterface;

class HeadElement
{
    use AttributeTrait;

    /**
     * @var string
     */
    protected $element;

    /**
     * @var bool
     */
    private $noScript = false;

    public function __construct()
    {
        $this->attributes = new AttributeBag();
    }

    /**
     * Sets if this element should be wrapped in <noscript>.
     *
     * @param bool $value
     *   (optional) Whether or not this element should be wrapped in <noscript>.
     *   Defaults to TRUE.
     *
     * @return $this
     */
    public function withNoScript(bool $value = true) : self
    {
        $that = clone($this);
        $that->noScript = $value;
        return $that;
    }

    public function __toString()
    {
        $string = ($this instanceof ContentElementInterface && $this->getContent())
          ? "<{$this->element}{$this->getAttributes()}>\n{$this->getContent()}\n</{$this->element}>"
          : "<{$this->element}{$this->getAttributes()} />";

        return $this->noScript
          ? "<noscript>$string</noscript>"
          : $string;
    }

    /**
     * Sets a new attribute bag with the specified attributes.
     *
     * This method is only to be used to set defaults from a child class's
     * constructor, as it mutates the object. That's fine within the constructor
     * but at no other time.
     *
     * @param array $attributes
     *   An array of the legal attribute keys for this element, and their default
     *   values. If left empty, all attributes will be legal.
     */
    protected function setAttributes(array $attributes = []) : void
    {
        $this->attributes = new AttributeBag($attributes);
    }
}