<commit_before><?php
declare(strict_types=1);
namespace Viserio\Component\View;

use ArrayAccess;
use BadMethodCallException;
use Closure;
use Throwable;
use Viserio\Component\Contract\Support\Arrayable;
use Viserio\Component\Contract\Support\Renderable;
use Viserio\Component\Contract\View\Engine as EngineContract;
use Viserio\Component\Contract\View\Factory as FactoryContract;
use Viserio\Component\Contract\View\View as ViewContract;
use Viserio\Component\Support\Str;

class View implements ArrayAccess, ViewContract
{
    /**
     * The view factory instance.
     *
     * @var \Viserio\Component\Contract\View\Factory
     */
    protected $factory;

    /**
     * The engine implementation.
     *
     * @var \Viserio\Component\Contract\View\Engine
     */
    protected $engine;

    /**
     * The name of the view.
     *
     * @var string
     */
    protected $view;

    /**
     * The array of view data.
     *
     * @var array
     */
    protected $data = [];

    /**
     * The file info to the view file.
     *
     * @var array
     */
    protected $fileInfo;

    /**
     * Create a new view instance.
     *
     * @param \Viserio\Component\Contract\View\Factory            $factory
     * @param \Viserio\Component\Contract\View\Engine             $engine
     * @param string                                              $view
     * @param array                                               $fileInfo
     * @param array|\Viserio\Component\Contract\Support\Arrayable $data
     */
    public function __construct(FactoryContract $factory, EngineContract $engine, string $view, array $fileInfo, $data = [])
    {
        $this->view     = $view;
        $this->fileInfo = $fileInfo;
        $this->engine   = $engine;
        $this->factory  = $factory;

        $this->data = $data instanceof Arrayable ? $data->toArray() : $data;
    }

    /**
     * Set a piece of data on the view.
     *
     * @param string $key
     * @param mixed  $value
     */
    public function __set($key, $value): void
    {
        $this->with($key, $value);
    }

    /**
     * Check if a piece of data is bound to the view.
     *
     * @param string $key
     *
     * @return bool
     */
    public function __isset($key): bool
    {
        return isset($this->data[$key]);
    }

    /**
     * Remove a piece of bound data from the view.
     *
     * @param string $key
     *
     * @return void
     */
    public function __unset($key): void
    {
        unset($this->data[$key]);
    }

    /**
     * Dynamically bind parameters to the view.
     *
     * @param string $method
     * @param array  $parameters
     *
     * @throws \BadMethodCallException
     *
     * @return \Viserio\Component\Contract\View\View
     */
    public function __call(string $method, array $parameters)
    {
        if (Str::startsWith($method, 'with')) {
            return $this->with(Str::snake(\mb_substr($method, 4)), $parameters[0]);
        }

        throw new BadMethodCallException(\sprintf('Method [%s] does not exist on view.', $method));
    }

    /**
     * Get the string contents of the view.
     *
     * @return string
     */
    public function __toString()
    {
        return $this->render();
    }

    /**
     * {@inheritdoc}
     */
    public function render(callable $callback = null): string<commit_msg>allow throwing from `__toString()` with `return trigger_error($e, E_U… (#702)<commit_after><?php
declare(strict_types=1);
namespace Viserio\Component\View;

use ArrayAccess;
use BadMethodCallException;
use Closure;
use Throwable;
use Viserio\Component\Contract\Support\Arrayable;
use Viserio\Component\Contract\Support\Renderable;
use Viserio\Component\Contract\View\Engine as EngineContract;
use Viserio\Component\Contract\View\Factory as FactoryContract;
use Viserio\Component\Contract\View\View as ViewContract;
use Viserio\Component\Support\Str;

class View implements ArrayAccess, ViewContract
{
    /**
     * The view factory instance.
     *
     * @var \Viserio\Component\Contract\View\Factory
     */
    protected $factory;

    /**
     * The engine implementation.
     *
     * @var \Viserio\Component\Contract\View\Engine
     */
    protected $engine;

    /**
     * The name of the view.
     *
     * @var string
     */
    protected $view;

    /**
     * The array of view data.
     *
     * @var array
     */
    protected $data = [];

    /**
     * The file info to the view file.
     *
     * @var array
     */
    protected $fileInfo;

    /**
     * Create a new view instance.
     *
     * @param \Viserio\Component\Contract\View\Factory            $factory
     * @param \Viserio\Component\Contract\View\Engine             $engine
     * @param string                                              $view
     * @param array                                               $fileInfo
     * @param array|\Viserio\Component\Contract\Support\Arrayable $data
     */
    public function __construct(FactoryContract $factory, EngineContract $engine, string $view, array $fileInfo, $data = [])
    {
        $this->view     = $view;
        $this->fileInfo = $fileInfo;
        $this->engine   = $engine;
        $this->factory  = $factory;

        $this->data = $data instanceof Arrayable ? $data->toArray() : $data;
    }

    /**
     * Set a piece of data on the view.
     *
     * @param string $key
     * @param mixed  $value
     */
    public function __set($key, $value): void
    {
        $this->with($key, $value);
    }

    /**
     * Check if a piece of data is bound to the view.
     *
     * @param string $key
     *
     * @return bool
     */
    public function __isset($key): bool
    {
        return isset($this->data[$key]);
    }

    /**
     * Remove a piece of bound data from the view.
     *
     * @param string $key
     *
     * @return void
     */
    public function __unset($key): void
    {
        unset($this->data[$key]);
    }

    /**
     * Dynamically bind parameters to the view.
     *
     * @param string $method
     * @param array  $parameters
     *
     * @throws \BadMethodCallException
     *
     * @return \Viserio\Component\Contract\View\View
     */
    public function __call(string $method, array $parameters)
    {
        if (Str::startsWith($method, 'with')) {
            return $this->with(Str::snake(\mb_substr($method, 4)), $parameters[0]);
        }

        throw new BadMethodCallException(\sprintf('Method [%s] does not exist on view.', $method));
    }

    /**
     * Get the string contents of the view.
     *
     * @return string
     */
    public function __toString(): string
    {
        try {
            return $this->render();
        } catch (Throwable $exception) {
            // Really, PHP? https://bugs.php.net/bug.php?id=53648
            \trigger_error(self::class . '::__toString exception: ' . (string) $exception, \E_USER_ERROR);

            return '';
        }
    }

    /**
     * {@inheritdoc}
     */
    public function render(callable $callback = null): string
{'fraction_non_alphanumeric': 0.12503544088460447, 'fraction_numerical': 0.001984689537850865, 'mean_word_length': 1.954355108877722, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4052671', 'n_tokens_mistral': 2078, 'n_tokens_neox': 2043, 'n_words': 514}