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

namespace Redstart\Templating\Core\Html;

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

/**
 * Attributes
 *
 * @package Redstart    
 * @subpackage Redstart_Templating_Core_Html
 *
 * @author Hyyan Abo Fakher
 * @since Redstart 1.0
 * @version 1.0
 */
class Attributes extends EscapeComponent implements \IteratorAggregate {

    protected $attrs = array();

    /**
     * Constrcut New Attributs Plugin
     * @param array $attrs
     * @param \Redstart\Templating\Escape\EscapeHandlerInterfacer $handler
     */
    public function __construct(array $attrs = array(),EscapeHandlerInterface $handler = null) {
        parent::__construct($handler);
        $this->set($attrs);
    }

    /**
     * Add Element Attribute
     * @param string $attr
     * @param string $value
     * @return \Redstart\Templating\Core\Html\Attributes
     */
    public function add($attr, $value) {
        $this->attrs[$this->escape($attr)] = $this->escape($value);
        return $this;
    }

    /**
     * Add Array Of Attributes
     * @param array $attrs
     * @return \Redstart\Templating\Core\Html\Attributes
     */
    public function set(array $attrs) {
        foreach ($attrs as $attr => $value) {
            $this->add($attr, $value);
        }
        return $this;
    }

    /**
     * Remove Attribute from element's attributes set
     * @param string $attr
     * @return \Redstart\Templating\Core\Html\Attributes
     */
    public function remove($attr) {
        if ($this->has($attr)) {
            unset($this->attrs[$attr]);
        }
        return $this;
    }

    /**
     * Remove Array Of Attributes
     * @param array $attr
     * @return \Redstart\Templating\Core\Html\Attributes
     */
    public function removeArray(array $attr) {
        foreach ($attr as $attr) {
            $this->remove($attr);
        }
        return $this;
    }

    /**
     * Check if the attributes set conatins the given attribute
     * @param string $attr
     * @return boolean true if it has false otherwise
     */
    public function has($attr) {
        return isset($this->attrs[$attr]) ? true : false;
    }

    /**
     * Clear The Attributes Set
     * @return \Redstart\Templating\Core\Html\Attributes
     */
    public function clear() {
        $this->attrs = array();
        return $this;
    }

    /**
     * Get Attributes as string
     * @return string
     */
    public function getString() {
        $result = '';
        foreach ($this->attrs as $attr => $value) {
            if (is_numeric($attr) || empty($attr)) {
                if (!empty($value)) {
                    $result.=sprintf(' %s ', $value);
                }
            } else {
                $result.=sprintf(' %s="%s" ', $attr, $value);
            }
        }
        return $result;
    }

    /**
     * Get Attributes as string
     * @return string
     */
    public function __toString() {
        return $this->getString();
    }

    /**
     * Get iterator
     * @return \ArrayIterator
     */
    public function getIterator() {
        return new \ArrayIterator($this->attrs);
    }

}
Return current item: Redstart Templating