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

namespace Redstart\Templating\Core\Plugin;

use Redstart\Templating\Core\Html\Attributes;
use Redstart\Templating\Core\Html\CssList;
use Redstart\Templating\Core\Html\MetaList;
use Redstart\Templating\Core\System\OptionExtender;
use Redstart\Templating\Core\Html\Tag;
use Redstart\Templating\Plugin\Plugin;

/**
 * Html Plugin 
 *
 * @package Redstart    
 * @subpackage Redstart_Templating_Core_Html
 *
 * @author Hyyan Abo Fakher
 * @since Redstart 1.0
 * @version 1.0
 */
class Html extends Plugin {

    /**
     * Attributes manager
     * @var \Redstart\Templating\Core\Html\Attributes 
     */
    protected $attrsManager;

    /**
     * Css List
     * @var \Redstart\Templating\Core\Html\CssList 
     */
    protected $csslist;

    /**
     * Meta List
     * @var \Redstart\Templating\Core\Html\MetaList 
     */
    protected $metalist;

    /**
     * Option Extender
     * @var \Redstart\Templating\Core\System\OptionExtender 
     */
    protected $optionExtender;

    /**
     * Tag Creater
     * @var \Redstart\Templating\Core\Html\Tag 
     */
    protected $tag;

    /**
     * Get Array of attributes as string
     * @param array $attrs
     * @param array $listeners array of escape listeners if empty then the template
     *                         system listeners will be used
     * @return string
     */
    public function attrs(array $attrs, array $listeners = array()) {
        if (!$this->attrsManager) {
            $this->attrsManager = new Attributes(
                    array(), $this->getTemplate()->getEscapeHandler()
            );
        }
        if (empty($listeners)) {
            $this->attrsManager->setEscapeList($this->getTemplate()->getEscapeList());
        } else {
            $this->attrsManager->getEscapeList()->clear();
            $this->attrsManager->setEscape($listeners);
        }
        return (string) $this->attrsManager
                        ->clear()
                        ->set($attrs)
                        ->getString();
    }

    /**
     * Build html meta tag(s)
     * @param array $meta
     * @param boolean $boolean true to put meta tags in one line or false to put
     *                         every meta tag in its own line where default is 
     *                         false
     * @param array $listeners array of escape listeners if empty then the template
     *                         system listeners will be used
     * <b>Example : </b>
     * 
     * <code>
     * array(
     *  //  content ,is http_equiv where default = false
     *  "Content-Type"=>array("text/html; charset=UTF-8",true)
     * )
     * </code>
     * 
     * 
     * @return string
     */
    public function meta(array $meta, $inline = false, array $listeners = array()) {
        if (!$this->metalist) {
            $this->metalist = new MetaList(
                    array(), $this->getTemplate()->getEscapeHandler()
            );
        }
        if (empty($listeners)) {
            $this->metalist->setEscapeList($this->getTemplate()->getEscapeList());
        } else {
            $this->metalist->getEscapeList()->clear();
            $this->metalist->setEscape($listeners);
        }

        return $this->metalist
                        ->clear()
                        ->set($meta)
                        ->setInline($inline)
                        ->getString();
    }

    /**
     * Generat Css List
     * 
     * @param array $list array of css rules
     * @param string $inline true to put every css rule in its line where 
     * default is false
     * 
     * @return string
     */
    public function cssList(array $list, $inline = false) {
        if (!$this->csslist) {
            $this->csslist = new CssList();
        }
        $this->csslist->clear();
        $this->csslist->setInline($inline)->merge($list);
        return $this->csslist->getString();
    }

    /**
     * Extende default options array with the given options array
     * 
     * @param array $default array of defaults options
     * @param array $options array of options to extend the default options
     * @return array
     */
    public function option(array $default, array $options) {
        if (!$this->optionExtender) {
            $this->optionExtender = new OptionExtender();
        }
        return $this->optionExtender
                        ->reset()
                        ->setDefaults($default)
                        ->extend($options)
                        ->getOption();
    }

    /**
     * Build Html Tag
     * 
     * @param string $name tag name 
     * @param string $content content if tag is not a unary tag
     * @param array $attrbs array of attrs
     * @return string html tag
     * 
     * @throws \RuntimeException if the AttributesPlugin is not registered
     */
    public function tag($name, $content = null, array $attrs = array()) {
        if (!$this->tag) {
            $this->tag = new Tag();
        }
        return $this->tag
                        ->setTagName($name)
                        ->setAttributes($attrs)
                        ->setContent($content)
                        ->getString();
    }

    /**
     * Get html link
     * @param array $attrs
     * @return string
     */
    public function link(array $attrs) {
        return $this->tag('link', null, $attrs);
    }

    /**
     * Generat Favicon link
     * 
     * @see Html::link()
     * 
     * @param string $file
     * @param string $type default = image/gif
     * @return string
     */
    public function favicon($file, $type = 'image/gif') {
        return $this->link(array(
                    'href' => $file,
                    'type' => $type,
                    'rel' => 'shortcut icon'
        ));
    }

    /**
     * Generat Style link
     * 
     * @see Html::link()
     * 
     * @param string $file
     * @param string $media default = all
     * @return string
     */
    public function style($file, $media = 'all') {
        return $this->link(array(
                    'href' => $file,
                    'rel' => 'stylesheet',
                    'type' => 'text/css',
                    'media' => $media
        ));
    }

    /**
     * Get js tag
     * @param string $file js src
     * @param string $type script type - default=text/javascript
     * @param boolean $defer default false
     * @param boolean $async default false
     * @return string
     */
    public function js($file, $type = 'text/javascript', $defer = false, $async = false) {
        $defer = ($defer == true) ? 'defer' : '';
        $async = ($async == true) ? 'async' : '';
        return $this->tag('script', null, array(
                    'type' => $type,
                    $async,
                    $defer,
                    'src' => $file
        ));
    }

    /**
     * Get html image tag
     * @param string $file file
     * @param array $options array of options
     * @return string
     */
    public function img($file, array $options = array()) {
        $base = basename($file);
        return $this->tag('img', null
                        , $this->option(array(
                            'src' => $file,
                            'alt' => $base,
                            'title' => $base), $options));
    }

    /**
     * Get Html Href
     * 
     * @param string $content
     * @param string $file
     * @param array $attrs 
     */
    public function ahref($content, $file, array $attrs = array()) {
        return $this->tag('a', $content, $this->option(array(
                            'href' => $file,
                                ), $attrs)
        );
    }

    /**
     * Get html menu
     * @param array | \Traversable $items
     * @param string $type ul or li
     * @param $menuAttrs $attrs 
     * @param $itemAttrs $attrs 
     * @return string
     */
    public function menu($items, $type = 'ul', array $menuAttrs = array(), array $itemAttrs = array()) {
        $this->assertTraversable(
                $items
                , 'Menu Items Should Be Traversable Object'
        );
        $content = '';
        foreach ($items as $item) {
            $content.=$this->tag('li', $item, $itemAttrs);
        }
        return $this->tag($type, $content, $menuAttrs);
    }

    /**
     * Build Array of html select options 
     * @param array | \Traversable $items
     * @param array $options
     * @param array $selected 
     * @return string
     * @throws \InvalidArgumentException if $items are not Traversable
     */
    public function selectOptions(array $items = array(), array $selected = array(), array $options = array()) {
        $this->assertTraversable(
                $items, 'Select Options Items Should Be Traversable Object'
        );

        $content = '';
        $default = array(
            'value' => ''
            , 'label' => ''
        );
        foreach ($items as $value => $label) {
            if (array_key_exists('selected', $default)) {
                unset($default['selected']);
            }
            $default['value'] = $value;
            $default['label'] = $label;
            if (in_array($value, $selected)) {
                $default['selected'] = 'selected';
            }
            $content.=$this->tag('option', $label, $this->option($default, $options));
        }
        return $content;
    }

    /**
     * Get html table
     * @param array | \Traversable $rows
     * @param array | \Traversable $header
     * @param string $caption table caption
     * @param array $options  array of the following options <br/>
     * 
     *  1- [tableAttr]     array of attributes to be added to the table 
     *                     where default contains one attribute [border=1]<br/>
     *  2- [captionAttr]  array of attributes to be added to table caption <br/>
     * 
     *  3- [headerAttr]    array of attributes to be added to the table's header<br/>
     *  4- [headerTrAttr]  array of attributes to be added to the header tr<br/>
     *  5- [headerThAttr]  array of attributes to be added to the header ths<br/>
     * 
     *  6- [bodyAttr]      array of attributes to be added to the table's body <br/>
     *  7- [tdAttr]        array of attributes to be added to the body tds<br/>
     *  8- [trAttr]        array of attributes to be added to the body trs
     *                     where default contains one attribute [class='']<br/>
     *  
     *  9-  [evenClass]    class to be added to the even rows(tr) in the table's 
     *                     body where default is [even]<br/>
     *  10- [oddClass]     class to be added to the odd rows(tr) in the table's 
     *                     body where default is [odd]<br/>
     *  
     * 
     * @return string
     * @throws \InvalidArgumentException if rows and header are not Traversable
     */
    public function getTable($rows, $header, $caption = null, array $options = array()) {
        $this->assertTraversable(
                $rows
                , 'Rows Should Be Traversable Object'
        );

        $this->assertTraversable(
                $header
                , 'Header Should Be Traversable Object'
        );

        // extend default options
        $options = $this->option(array(
            'tdattr' => array(),
            'trattr' => array('class' => ''),
            'tableattr' => array('border' => 1),
            'bodyattr' => array(),
            'headerattr' => array(),
            'headertrattr' => array(),
            'headerthattr' => array(),
            'captionattr' => array(),
            'evenclass' => 'even',
            'oddclass' => 'odd')
                , $options
        );

        // Build Caption
        if (!empty($caption) && is_string($caption)) {
            $caption = $this->tag(
                    'caption'
                    , (string) $caption
                    , $options['captionattr']
            );
        } else {
            $caption = '';
        }



        // Build Header
        $th = '';
        foreach ($header as $value) {
            $th.=$this->tag(
                    'th'
                    , $value
                    , $options['headerthattr']
            );
        }
        $header = $this->tag(
                'thead'
                , $this->tag('tr', $th, $options['headertrattr'])
                , $options['headerattr']
        );



        // Build Body 
        $trs = '';
        $counter = 0;
        foreach ($rows as $row) {
            $tds = '';
            foreach ($row as $value) {
                $tds.=$this->tag('td', $value, $options['tdattr']);
            }
            $class = ($counter % 2 == 0) ?
                    $options['oddclass'] : $options['evenclass'];

            $replace = ($class === $options['oddclass']) ?
                    $options['evenclass'] : $options['oddclass'];

            $options['trattr']['class'] = trim(
                    trim($class)
                    . ' '
                    . trim(
                            str_replace(
                                    $replace
                                    , ''
                                    , $options['trattr']['class']
                            )
                    )
            );

            $trs.=$this->tag('tr', $tds, $options['trattr']);

            $counter++;
        }

        $body = $this->tag('tbody', $trs, $options['bodyattr']);

        return $this->tag(
                        'table'
                        , ($caption . $header . $body)
                        , $options['tableattr']
        );
    }

    /**
     * Assert That the given object is array or Traversable object
     * @param array | \Traversable $object
     * @param string $message exception message 
     * @throws \InvalidArgumentException
     */
    protected function assertTraversable($object, $message = '') {
        if (!is_array($object) && !($object instanceof \Traversable)) {
            throw new \InvalidArgumentException($message);
        }
    }

    /**
     * Get plugin names
     * @return string
     */
    public function getName() {
        return 'html';
    }

}
Return current item: Redstart Templating