<commit_before><?php

/**
 * Copyright 2015, <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * This file is part of the Nucleus package
 */

namespace Chromabits\Nucleus\View;

use Chromabits\Nucleus\Exceptions\CoreException;
use Chromabits\Nucleus\Foundation\BaseObject;
use Chromabits\Nucleus\Meditation\Spec;
use Chromabits\Nucleus\View\Exceptions\InvalidAttributesException;
use Chromabits\Nucleus\View\Interfaces\Renderable;

/**
 * Class Node.
 *
 * WIP
 *
 * @author <NAME> <<EMAIL>>
 * @package Chromabits\Nucleus\View
 */
class Node extends BaseObject implements Renderable
{
    /**
     * @var null|string
     */
    protected $tagName = null;

    /**
     * @var string[]
     */
    protected $attributes = [];

    /**
     * @var bool
     */
    protected $selfClosing = false;

    /**
     * @var Renderable|Interfaces\Renderable[]|string|\string[]
     */
    protected $content;

    /**
     * @var Spec
     */
    protected $spec;

    /**
     * Construct an instance of a Node.
     *
     * @param string $tagName
     * @param string[] $attributes
     * @param string|Renderable|string[]|Renderable[] $content
     * @param bool $selfClosing
     */
    public function __construct(
        $tagName,
        $attributes,
        $content,
        $selfClosing = false
    ) {
        parent::__construct();

        $this->tagName = $tagName;
        $this->attributes = $attributes;
        $this->content = $content;
        $this->selfClosing = $selfClosing;

        $this->spec = new Spec();
    }

    /**
     * Render a single attribute in a node.
     *
     * @param string $name
     * @param null|string $value
     *
     * @return string
     */
    protected function renderAttribute($name, $value = null)
    {
        if ($value === null) {
            return $name;
        }

        return vsprintf('%s="%s"', [
            $name,
            nucleus_escape_html((string) $value),
        ]);
    }

    /**
     * Render the attributes part of the opening tag.
     *
     * @return string
     */
    protected function renderAttributes()
    {
        return implode(' ', array_map(function ($name, $value) {
            return $this->renderAttribute($name, $value);
        }, array_keys($this->attributes), $this->attributes));
    }

    /**
     * Render the content of the tag.
     *
     * @throws CoreException
     * @return string
     */
    protected function renderContent()
    {
        if (is_string($this->content)) {
            return nucleus_escape_html($this->content);
        } elseif ($this->content instanceof Renderable) {
            return $this->content->render();
        } elseif (is_array($this->content)) {
            return implode('', array_map(function ($child) {
                if (is_string($child)) {
                    return nucleus_escape_html($child);
                } elseif ($child instanceof Renderable) {
                    return $child->render();
                }

                throw new CoreException(
                    'Unknown content type. Child item cannot be rendered.'
                );
            }, $this->content));
        }

        throw new CoreException(
            'Unknown content type. Node cannot be rendered.'
        );
    }

    /**
     * Render the Node.
     *
     * @throws CoreException
     * @throws InvalidAttributesException
     * @return string
     */
    public function render()
    {
        $result = $this->spec->check($this->attributes);
        if ($result->failed()) {
            throw new InvalidAttributesException($result);
        }

        if ($this->selfClosing) {
            return sprintf(
                '<%s%s/>',
                [$this->tagName, $this->renderAttributes()]
            );
        }

        $attributes = $this->renderAttributes();
        if (strlen($attributes)) {
            $attributes = ' ' . $attributes;
        }

        return vsprintf(
            '<%s%s>%s</%s>',
            [
                $this->tagName,
                $attributes,
                $this->renderContent(),
                $this->tagName,
            ]<commit_msg>Fix some rendering problems on Node<commit_after><?php

/**
 * Copyright 2015, <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * This file is part of the Nucleus package
 */

namespace Chromabits\Nucleus\View;

use Chromabits\Nucleus\Exceptions\CoreException;
use Chromabits\Nucleus\Foundation\BaseObject;
use Chromabits\Nucleus\Meditation\Spec;
use Chromabits\Nucleus\Meditation\TypeHound;
use Chromabits\Nucleus\View\Exceptions\InvalidAttributesException;
use Chromabits\Nucleus\View\Interfaces\Renderable;

/**
 * Class Node.
 *
 * WIP
 *
 * @author <NAME> <<EMAIL>>
 * @package Chromabits\Nucleus\View
 */
class Node extends BaseObject implements Renderable
{
    /**
     * @var null|string
     */
    protected $tagName = null;

    /**
     * @var string[]
     */
    protected $attributes = [];

    /**
     * @var bool
     */
    protected $selfClosing = false;

    /**
     * @var Renderable|Interfaces\Renderable[]|string|\string[]
     */
    protected $content;

    /**
     * @var Spec
     */
    protected $spec;

    /**
     * Construct an instance of a Node.
     *
     * @param string $tagName
     * @param string[] $attributes
     * @param string|Renderable|string[]|Renderable[] $content
     * @param bool $selfClosing
     */
    public function __construct(
        $tagName,
        $attributes,
        $content,
        $selfClosing = false
    ) {
        parent::__construct();

        $this->tagName = $tagName;
        $this->attributes = $attributes;
        $this->content = $content;
        $this->selfClosing = $selfClosing;

        $this->spec = new Spec();
    }

    /**
     * Render a single attribute in a node.
     *
     * @param string $name
     * @param null|string $value
     *
     * @return string
     */
    protected function renderAttribute($name, $value = null)
    {
        if ($value === null) {
            return $name;
        }

        return vsprintf('%s="%s"', [
            $name,
            nucleus_escape_html((string) $value),
        ]);
    }

    /**
     * Render the attributes part of the opening tag.
     *
     * @return string
     */
    protected function renderAttributes()
    {
        return implode(' ', array_map(function ($name, $value) {
            return $this->renderAttribute($name, $value);
        }, array_keys($this->attributes), $this->attributes));
    }

    /**
     * Render the content of the tag.
     *
     * @throws CoreException
     * @return string
     */
    protected function renderContent()
    {
        if (is_string($this->content)) {
            return nucleus_escape_html($this->content);
        } elseif ($this->content instanceof Renderable) {
            return $this->content->render();
        } elseif (is_array($this->content)) {
            return implode('', array_map(function ($child) {
                if (is_string($child)) {
                    return nucleus_escape_html($child);
                } elseif ($child instanceof Renderable) {
                    return $child->render();
                }

                throw new CoreException(vsprintf(
                    'Unknown content type: %s. Child item cannot be rendered.',
                    [
                        TypeHound::fetch($child),
                    ]
                ));
            }, $this->content));
        }

        throw new CoreException(vsprintf(
            'Unknown content type. Node cannot be rendered.',
            [
                TypeHound::fetch($this->content),
            ]
        ));
    }

    /**
     * Render the Node.
     *
     * @throws CoreException
     * @throws InvalidAttributesException
     * @return string
     */
    public function render()
    {
        $result = $this->spec->check($this->attributes);
        if ($result->failed()) {
            throw new InvalidAttributesException($result);
        }

        if ($this->selfClosing) {
            return vsprintf(
                '<%s%s/>',
                [$this->tagName, $this->renderAttributes()]
            );
        }

        $attributes = $this->renderAttributes();
        if (strlen($attributes)) {
            $attributes = ' ' . $attributes;
        }

        return vsprintf(
            '<%s%s>%s</%s>',
            [
                $this->tagName,
                $attributes,
                $this->renderContent(),
                $this->tagName,
            ]
{'fraction_non_alphanumeric': 0.12846347607052896, 'fraction_numerical': 0.0009159606136936112, 'mean_word_length': 1.7916267178012144, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 72, 'https://': 0, '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': '3294862', 'n_tokens_mistral': 2466, 'n_tokens_neox': 2389, 'n_words': 554}