<commit_before><?php

namespace EloquentJs\Model;

use Carbon\Carbon;
use DateTime;
use InvalidArgumentException;
use EloquentJs\Events\EloquentJsWasCalled;
use UnexpectedValueException;

trait EloquentJsQueries
{
    /**
     * Scope to results that satisfy the string-encoded list of query methods described by $stack.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param string $stack
     */
    public function scopeUseEloquentJs($query, $stack = null)
    {
        if ( ! static::$dispatcher) {
            throw new UnexpectedValueException(
                'Event dispatcher not found. Install illuminate/events package for EloquentJS usage without Laravel.'
            );
        }

        static::$dispatcher->fire(new EloquentJsWasCalled($query, $stack));
    }

    /**
     * Handle dynamic calls to scope methods.
     *
     * Yes, this is indeed a scope for calling another scope method.
     * While there are other ways we could support scopes, this has
     * the benefit of requiring no special treatment in our BaseQueryTranslator
     * - no different from how we handle `where`, `orderBy`, etc.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param string $name
     * @param array $parameters
     */
    public function scopeScope($query, $name, $parameters = [])
    {
        $methodName = 'scope'.ucfirst($name);

        if ( ! method_exists($this, $methodName)) {
            throw new InvalidArgumentException("No such scope [$name]");
        }

        array_unshift($parameters, $query);
        call_user_func_array([$this, $methodName], $parameters);
    }

    /**
     * Prepare a date for array / JSON serialization.
     *
     * Overrides the date serializer to ensure our Javascript can
     * understand the format.
     *
     * @todo  implement date handling without global side effects
     * @param DateTime $date
     * @return string
     */
    protected function serializeDate(DateTime $date)
    {
        return $date->toIso8601String();
    }

    /**
     * Return a timestamp as DateTime object.
     *
     * Accept dates from Javascript in any format that Carbon recognises.
     *
     * @param  string $value
     * @return Carbon
     */
    protected function asDateTime($value)
    {
        return Carbon::parse($value);
    }

    /**
     * Get the endpoint defined on this model.
     *
     * @return string|null
     */
    public function getEndpoint()
    {
        if (property_exists($this, 'endpoint')) {
            return $this->endpoint;
        }

        return null;
    }
}
<commit_msg>Fix for date handling when not in EloquentJs context<commit_after><?php

namespace EloquentJs\Model;

use Carbon\Carbon;
use DateTime;
use InvalidArgumentException;
use EloquentJs\Events\EloquentJsWasCalled;
use UnexpectedValueException;

trait EloquentJsQueries
{
    /**
     * Scope to results that satisfy the string-encoded list of query methods described by $stack.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param string $stack
     */
    public function scopeUseEloquentJs($query, $stack = null)
    {
        if ( ! static::$dispatcher) {
            throw new UnexpectedValueException(
                'Event dispatcher not found. Install illuminate/events package for EloquentJS usage without Laravel.'
            );
        }

        static::$dispatcher->fire(new EloquentJsWasCalled($query, $stack));
    }

    /**
     * Handle dynamic calls to scope methods.
     *
     * Yes, this is indeed a scope for calling another scope method.
     * While there are other ways we could support scopes, this has
     * the benefit of requiring no special treatment in our BaseQueryTranslator
     * - no different from how we handle `where`, `orderBy`, etc.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param string $name
     * @param array $parameters
     */
    public function scopeScope($query, $name, $parameters = [])
    {
        $methodName = 'scope'.ucfirst($name);

        if ( ! method_exists($this, $methodName)) {
            throw new InvalidArgumentException("No such scope [$name]");
        }

        array_unshift($parameters, $query);
        call_user_func_array([$this, $methodName], $parameters);
    }

    /**
     * Prepare a date for array / JSON serialization.
     *
     * Overrides the date serializer to ensure our Javascript can
     * understand the format.
     *
     * @todo  implement date handling without global side effects
     * @param DateTime $date
     * @return string
     */
    protected function serializeDate(DateTime $date)
    {
        return $date->toIso8601String();
    }

    /**
     * Return a timestamp as DateTime object.
     *
     * Accept dates from Javascript in any format that Carbon recognises.
     *
     * @param  string $value
     * @return Carbon
     */
    protected function asDateTime($value)
    {
        if (is_string($value)) {
            return Carbon::parse($value);
        }

        return parent::asDateTime($value);
    }

    /**
     * Get the endpoint defined on this model.
     *
     * @return string|null
     */
    public function getEndpoint()
    {
        if (property_exists($this, 'endpoint')) {
            return $this->endpoint;
        }

        return null;
    }
}

{'fraction_non_alphanumeric': 0.09691220238095238, 'fraction_numerical': 0.001488095238095238, 'mean_word_length': 2.386020151133501, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '3199317', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1475, 'n_words': 452}