<?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
{
    protected $metaHelper;

    /**
     * Constructor
     *
     * @param 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'))),
            new Twig_SimpleFunction('href_lang_tags', array($this, 'twigHrefLangTags')),
        );
    }

    /**
     * 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 ]);
    }

    /**
     * HrefLangTags
     *
     * @return string|Array
     */
    public function twigHrefLangTags()
    {
        return $this->metaHelper->getHrefLangTags();
    }
}
