<commit_before><?php
/**
 * Webino (http://webino.sk)
 *
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
 * @copyright   Copyright (c) 2012-2014 Webino, s. r. o. (http://webino.sk)
 * @author      <NAME> <<EMAIL>>
 * @license     BSD-3-Clause
 */

namespace WebinoDraw\Draw\Helper;

use DOMAttr as DomAttr;
use WebinoDraw\Dom\NodeList;
use WebinoDraw\VarTranslator\VarTranslator;
use WebinoDraw\Exception\RuntimeException;
use Zend\View\Helper\ServerUrl;
use Zend\View\Helper\BasePath;

/**
 * Draw helper used for prepend relative URL with basePath
 */
class Absolutize extends AbstractHelper
{
    /**
     * @var ServerUrl
     */
    protected $serverUrl;

    /**
     * @var BasePath
     */
    protected $basePath;

    /**
     * @param ServerUrl $serverUrl
     * @param BasePath $basePath
     */
    public function __construct(ServerUrl $serverUrl, BasePath $basePath)
    {
        $this->serverUrl = $serverUrl;
        $this->basePath  = $basePath;
    }

    /**
     * @param NodeList $nodes
     * @param array $spec
     */
    public function drawNodes(NodeList $nodes, array $spec)
    {
        $varTranslator = $this->getVarTranslator();
        $translation   = $varTranslator->createTranslation($this->getVars());
        $varTranslator->apply($translation, $spec);

        $translationVars = $translation->getVarTranslation();
        $basePath        = $this->basePath->__invoke();
        $serverUrl       = $this->resolveServerUrl($spec);

        foreach ($nodes as $node) {
            if (!($node instanceof DomAttr)) {
                throw new RuntimeException('Expected DOMAttr for spec ' . print_r($spec, true));
            }

            $nodeValue = $node->nodeValue;
            $translationVars->translate($nodeValue);
            $node->nodeValue = $serverUrl . $this->removeDotSegments($basePath . '/' . ltrim($nodeValue, '/'));
        }
    }

    /**
     * @param array $spec
     * @return string
     */
    private function resolveServerUrl(array $spec)
    {
        if (!array_key_exists('serverUrl', $spec)) {
            return '';
        }
        if (true === $spec['serverUrl']) {
            return $this->serverUrl->__invoke();
        }

        return (string) $spec['serverUrl'];
    }

    /**<commit_msg>Updated Draw\Helper\Absolutize, make sure that nodeValue is string by type casting<commit_after><?php
/**
 * Webino (http://webino.sk)
 *
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
 * @copyright   Copyright (c) 2012-2014 Webino, s. r. o. (http://webino.sk)
 * @author      <NAME> <<EMAIL>>
 * @license     BSD-3-Clause
 */

namespace WebinoDraw\Draw\Helper;

use DOMAttr as DomAttr;
use WebinoDraw\Dom\NodeList;
use WebinoDraw\VarTranslator\VarTranslator;
use WebinoDraw\Exception\RuntimeException;
use Zend\View\Helper\ServerUrl;
use Zend\View\Helper\BasePath;

/**
 * Draw helper used for prepend relative URL with basePath
 */
class Absolutize extends AbstractHelper
{
    /**
     * @var ServerUrl
     */
    protected $serverUrl;

    /**
     * @var BasePath
     */
    protected $basePath;

    /**
     * @param ServerUrl $serverUrl
     * @param BasePath $basePath
     */
    public function __construct(ServerUrl $serverUrl, BasePath $basePath)
    {
        $this->serverUrl = $serverUrl;
        $this->basePath  = $basePath;
    }

    /**
     * @param NodeList $nodes
     * @param array $spec
     */
    public function drawNodes(NodeList $nodes, array $spec)
    {
        $varTranslator = $this->getVarTranslator();
        $translation   = $varTranslator->createTranslation($this->getVars());
        $varTranslator->apply($translation, $spec);

        $translationVars = $translation->getVarTranslation();
        $basePath        = $this->basePath->__invoke();
        $serverUrl       = $this->resolveServerUrl($spec);

        foreach ($nodes as $node) {
            if (!($node instanceof DomAttr)) {
                throw new RuntimeException('Expected DOMAttr for spec ' . print_r($spec, true));
            }

            $nodeValue = $node->nodeValue;
            $translationVars->translate($nodeValue);
            $node->nodeValue = $serverUrl . $this->removeDotSegments($basePath . '/' . ltrim((string) $nodeValue, '/'));
        }
    }

    /**
     * @param array $spec
     * @return string
     */
    private function resolveServerUrl(array $spec)
    {
        if (!array_key_exists('serverUrl', $spec)) {
            return '';
        }
        if (true === $spec['serverUrl']) {
            return $this->serverUrl->__invoke();
        }

        return (string) $spec['serverUrl'];
    }

    /**
{'fraction_non_alphanumeric': 0.13972777541471715, 'fraction_numerical': 0.0038281582305401958, 'mean_word_length': 2.4079710144927535, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 41, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '4614555', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1388, 'n_words': 301}