<commit_before><?php
/**
 * Kodekit Component - http://www.timble.net/kodekit
 *
 * @copyright	Copyright (C) 2011 - 2016 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license		MPL v2.0 <https://www.mozilla.org/en-US/MPL/2.0>
 * @link		https://github.com/timble/kodekit-activities for the canonical source repository
 */

namespace Kodekit\Component\Activities;

use Kodekit\Library;

/**
 * Activity Template Helper.
 *
 * @author  <NAME> <https://github.com/amazeika>
 * @package Kodekit\Component\Activities
 */
class TemplateHelperActivity extends Library\TemplateHelperAbstract implements Library\ObjectMultiton, ActivityRendererInterface
{
    /**
     * Renders an activity.
     *
     * Wraps around {@link render()} to easily render activities on layouts.
     *
     * @param array $config An optional configuration array.
     * @return string The rendered activity.
     */
    public function activity($config = array())
    {
        $config = new Library\ObjectConfig($config);

        $output = '';

        if ($activity = $config->entity) {
            $output = $this->render($activity, $config);
        }

        return $output;
    }

    /**
     * Renders an activity.
     *
     * @param ActivityInterface $activity The activity object.
     * @param array             $config   An optional configuration array.
     * @return string The rendered activity.
     */
    public function render(ActivityInterface $activity, $config = array())
    {
        $config = new Library\ObjectConfig($config);

        $output = $activity->getActivityFormat();

        if (preg_match_all('/{(.*?)}/', $output, $labels))
        {
            $tokens = $activity->tokens;

            foreach ($labels[1] as $label)
            {
                $parts = explode(':', $label);

                if (isset($tokens[$parts[0]]))
                {
                    $object = $tokens[$parts[0]];

                    // Deal with context translations.
                    if (isset($parts[1]))
                    {
                        $object = clone $object;
                        $object->setDisplayName($parts[1]);
                    }

                    if ($object = $this->_renderObject($object, $config)) {
                        $output = str_replace('{' . $label . '}', $object, $output);
                    }
                }
            }
        }

        return $output;
    }

    /**
     * Renders an activity object.
     *
     * @param ActivityObjectInterface $object The activity object.
     * @param Library\ObjectConfig    $config The configuration object.
     * @return string The rendered object.
     */
    protected function _renderObject(ActivityObjectInterface $object, Library\ObjectConfig $config)
    {
        $config->append(array('html' => true, 'escaped_urls' => true, 'fqr' => false));

        if ($output = $object->getDisplayName())
        {
            if ($config->html)
            {
                $output  = $object->getDisplayName();
                $attribs = $object->getAttributes() ? $this->buildAttributes($object->getAttributes()) : '';

                if ($url = $object->getUrl())
                {
                    // Make sure we have a fully qualified route.
                    if ($config->fqr && !$url->getHost()) {
                        $url->setUrl($this->getTemplate()->url()->toString(Library\HttpUrl::AUTHORITY));
                    }

                    $url    = $url->toString(Library\HttpUrl::FULL, $config->escaped_urls);<commit_msg>Passthrough the template object where required.<commit_after><?php
/**
 * Kodekit Component - http://www.timble.net/kodekit
 *
 * @copyright	Copyright (C) 2011 - 2016 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license		MPL v2.0 <https://www.mozilla.org/en-US/MPL/2.0>
 * @link		https://github.com/timble/kodekit-activities for the canonical source repository
 */

namespace Kodekit\Component\Activities;

use Kodekit\Library;

/**
 * Activity Template Helper.
 *
 * @author  <NAME> <https://github.com/amazeika>
 * @package Kodekit\Component\Activities
 */
class TemplateHelperActivity extends Library\TemplateHelperAbstract implements Library\ObjectMultiton, ActivityRendererInterface
{
    /**
     * Renders an activity.
     *
     * Wraps around {@link render()} to easily render activities on layouts.
     *
     * @param array $config An optional configuration array.
     * @return string The rendered activity.
     */
    public function activity($config = array(), Library\TemplateInterface $template)
    {
        $config = new Library\ObjectConfig($config);
        $config->url = $template->route();

        $output = '';

        if ($activity = $config->entity) {
            $output = $this->render($activity, $config);
        }

        return $output;
    }

    /**
     * Renders an activity.
     *
     * @param ActivityInterface $activity The activity object.
     * @param array             $config   An optional configuration array.
     * @return string The rendered activity.
     */
    public function render(ActivityInterface $activity, $config = array())
    {
        $config = new Library\ObjectConfig($config);

        $output = $activity->getActivityFormat();

        if (preg_match_all('/{(.*?)}/', $output, $labels))
        {
            $tokens = $activity->tokens;

            foreach ($labels[1] as $label)
            {
                $parts = explode(':', $label);

                if (isset($tokens[$parts[0]]))
                {
                    $object = $tokens[$parts[0]];

                    // Deal with context translations.
                    if (isset($parts[1]))
                    {
                        $object = clone $object;
                        $object->setDisplayName($parts[1]);
                    }

                    if ($object = $this->_renderObject($object, $config)) {
                        $output = str_replace('{' . $label . '}', $object, $output);
                    }
                }
            }
        }

        return $output;
    }

    /**
     * Renders an activity object.
     *
     * @param ActivityObjectInterface $object The activity object.
     * @param Library\ObjectConfig    $config The configuration object.
     * @return string The rendered object.
     */
    protected function _renderObject(ActivityObjectInterface $object, Library\ObjectConfig $config)
    {
        $config->append(array(
            'html' => true,
            'escaped_urls' => true,
            'fqr' => false
        ));

        if ($output = $object->getDisplayName())
        {
            if ($config->html)
            {
                $output  = $object->getDisplayName();
                $attribs = $object->getAttributes() ? $this->buildAttributes($object->getAttributes()) : '';

                if ($url = $object->getUrl())
                {
                    // Make sure we have a fully qualified route.
                    if ($config->fqr && !$url->getHost()) {
                        $url->setUrl($config->url->toString(Library\HttpUrl::AUTHORITY));
                    }

                    $url    = $url->toString(Library\HttpUrl::FULL, $config->escaped_urls);
{'fraction_non_alphanumeric': 0.1347507938699434, 'fraction_numerical': 0.0046941874913709785, 'mean_word_length': 1.8430141287284145, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 62, 'https://': 6, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5519894', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1877, 'n_words': 441}