Location: PHPKode > scripts > Redstart Templating > Redstart-Templating-master/Redstart/Templating/Core/Html/MetaList.php
<?php

namespace Redstart\Templating\Core\Html;

use Redstart\Util\Dict;
use Redstart\Templating\Escape\EscapeComponent;
use Redstart\Templating\Escape\EscapeHandlerInterface;

/**
 * Meta List Plugin
 *
 * @package Redstart    
 * @subpackage Redstart_Templating_Core_Html
 *
 * @author Hyyan Abo Fakher
 * @since Redstart 1.0
 * @version 1.0
 */
class MetaList extends EscapeComponent {

    private $inline;

    /**
     * Meta list
     * @var \Redstart\Util\Dict 
     */
    public $meta;

    /**
     * Constrcut new meta list
     * @param array $metas
     * @param \Redstart\Templating\Escape\EscapeHandlerInterface $handler
     */
    public function __construct(array $metas = array(), EscapeHandlerInterface $handler = null) {
        parent::__construct($handler);
        $this->meta = new Dict();
        $this->meta->set('http_equiv', new Dict());
        $this->meta->set('standard', new Dict());
        $this->set($metas);
    }

    /**
     * Add new meta tag
     * @param string $name name of meta tag
     * @param string $content content of meta tag
     * @param boolean $http_equiv true if http equive false otherwise
     * @return \Redstart\Templating\Core\Html\MetaList
     */
    public function add($name, $content, $http_equiv = false) {
        if ($http_equiv == true) {
            $this->meta->get('http_equiv')->set(
                    $this->escape($name), $this->escape($content)
            );
        } else {
            $this->meta->get('standard')->set(
                    $this->escape($name), $this->escape($content)
            );
        }
        return $this;
    }

    /**
     * Check if the given id exists in the meta set
     * @param string $name meta name
     * @param boolean $http_equiv true if http equive false otherwise
     */
    public function has($name, $http_equiv = false) {
        $result = false;
        if ($http_equiv == true) {
            $result = $this->meta->get('http_equiv')->has($name);
        } else {
            $result = $this->meta->get('standard')->has($name);
        }
        return $this->meta->has($name);
    }

    /**
     * Remove Meta from MetaSet
     * @param string $name meta name
     * @param boolean $http_equiv true if http equive false otherwise
     * @return \Redstart\Templating\Core\Html\MetaList
     */
    public function remove($name, $http_equiv = false) {
        $result = false;
        if ($http_equiv == true) {
            $result = $this->meta->get('http_equiv')->popItem($name);
        } else {
            $result = $this->meta->get('standard')->popItem($name);
        }
        return $this;
    }

    /**
     * Set array of meta tags as key => value
     * 
     * <b>Example : </b>
     * 
     * <code>
     * array(
     *  //  content ,is http_equiv where default = false
     *  "Content-Type"=>array("text/html; charset=UTF-8",true)
     * )
     * </code>
     * 
     * @param array $metas  array of meta tags
     * @return \Redstart\Templating\Core\Html\MetaList
     */
    public function set(array $metas) {
        foreach ($metas as $name => $array) {
            if (is_array($array)) {
                if (!empty($array)) {
                    $http_equiv = isset($array[1]) ? $array[1] : false;
                    $content = $array[0];
                    $this->add($name, $content, $http_equiv);
                }
            } else {
                if (is_string($array)) {
                    $this->add($name, $array, false);
                }
            }
        }
        return $this;
    }

    /**
     * Set meta inline
     * @param boolean $boolean true to put meta tags in one line or false to put
     *                         every meta tag in its own line
     * @return \Redstart\Templating\Core\Html\MetaList
     */
    public function setInline($boolean) {
        $this->inline = (boolean) $boolean;
        return $this;
    }

    /**
     * Check if meta inline is enabled
     * @return boolean
     */
    public function isInline() {
        return $this->inline;
    }

    /**
     * Get meta list as string
     * @return string
     */
    public function getString() {
        $result = '';
        $http_equiv = $this->meta->get('http_equiv')->items();
        $standard = $this->meta->get('standard')->items();
        $inline = $this->isInline() ? PHP_EOL : '';
        foreach ($http_equiv as $meta => $content) {
            $result .= sprintf(
                    '<meta http-equiv="%s" content="%s" />%s'
                    , $meta
                    , $content
                    , $inline
            );
        }

        foreach ($standard as $meta => $content) {
            $result .= sprintf(
                    '<meta name="%s" content="%s" />%s'
                    , $meta
                    , $content
                    , $inline
            );
        }
        return $result;
    }

    /**
     * Clear meta list
     * @return \Redstart\Templating\Core\Html\MetaList
     */
    public function clear() {
        $this->meta->get('http_equiv')->clear();
        $this->meta->get('standard')->clear();
        return $this;
    }

    /**
     * Get meta list as string
     * 
     * @see MetaList::getString()
     * 
     * @return string
     */
    public function __toString() {
        return $this->getString();
    }

}
Return current item: Redstart Templating