<?php
/**
 * Twig MetaRenderer Extension
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
namespace VisualM\TagManagerBundle\Twig;

use Twig_Extension;
use Twig_SimpleFunction;
use VisualM\TagManagerBundle\Content\MetaHelper;
use VisualM\TagManagerBundle\Doctrine\FieldType\MetaType;


/**
 * Twig MetaRenderer Extension
 *
 * Bridge between Twig and MetaHelper
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class MetaRenderer extends Twig_Extension
{
    /**
     * MetaHelper
     * @var MetaHelper
     */
    protected $metaHelper;

    /**
     * Constructor
     * 
     * @param MetaHelper $metaHelper MetaHelper
     */
    function __construct(MetaHelper $metaHelper)
    {
        $this->metaHelper = $metaHelper;
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'visualm_tag_manager_meta_renderer';
    }

    /**
     * {@inheritdoc}
     */
    public function getFunctions()
    {
        return array(
            new Twig_SimpleFunction('meta_tags', array($this, 'twigMetaTags'), array('is_safe' => array('all'))),
            new Twig_SimpleFunction('meta_tags_header', array($this, 'twigMetaTagsHeader'), array('is_safe' => array('all'))),
            new Twig_SimpleFunction('meta_tags_body', array($this, 'twigMetaTagsBody'), array('is_safe' => array('all'))),
        );
    }

    /**
     * Render MetaTags
     *
     * @param array $types MetaTypes to render
     *
     * @return string Rendered tags
     */
    public function twigMetaTags(array $types)
    {
        return $this->metaHelper->renderMetaTags($types);
    }

    /**
     * Render MetaTags
     *
     * @param array $types MetaTypes to render
     *
     * @return string Rendered tags
     */
    public function twigMetaTagsHeader()
    {
        return $this->metaHelper->renderMetaTags([ MetaType::HTML_HEADER_SNIPPET, MetaType::META_TAG ]);
    }

    /**
     * Render MetaTags
     *
     * @param array $types MetaTypes to render
     *
     * @return string Rendered tags
     */
    public function twigMetaTagsBody()
    {
        return $this->metaHelper->renderMetaTags([ MetaType::HTML_BODY_SNIPPET ]);
    }

}