<commit_before><?php

namespace Domanage;

use DateTime;
use Carbon\Carbon;
use Domanage\Parsers\GregorianDate\CarbonParser;
use Domanage\Parsers\GregorianDate\StringParser;
use Domanage\Parsers\GregorianDate\DateTimeParser;

/**
 * Class HebrewDate
 *
 * @package Domanage
 */
class HebrewDate
{

    /**
     * The default numeric format.
     *
     * @var string
     */
    const NUMERIC = 'Numeric';

    /**
     * Format for english month date.
     *
     * @var string
     */
    const ENGLISH_MONTH = 'EnglishMonth';

    /**
     * Format for full hebrew date.
     *
     * @var string
     */
    const HEBREW_FULL = 'HebrewFull';

    /**
     * Format for presentation hebrew date.
     *
     * @var string
     */
    const PRESENTABLE_HEBREW_DATE = 'PresentableHebrewDate';

    /**
     * Parsed gregorian date input in mm/dd/yyyy format.
     *
     * @var array
     */
    protected $gregorianDate;

    /**
     * The default format for output.
     *
     * @var string
     */
    protected $format = 'Numeric';

    /**
     * HebrewDate constructor.
     *
     * @param $gregorianDate
     */
    public function __construct($gregorianDate)
    {
        $this->gregorianDate = $gregorianDate;
    }

    /**
     * Create a new HebrewDate instance with parsed date.
     *
     * @param $date
     * @return static
     */
    public static function fromGregorian($date)
    {
        $parser = static::getParser($date);

        return new static($parser->handle());
    }

    /**
     * Get the right parser for the given input.
     *
     * @param $date
     * @return CarbonParser|DateTimeParser|StringParser
     */
    protected static function getParser($date)
    {
        if ($date instanceof Carbon) {
            return new CarbonParser($date);
        }

        if ($date instanceof DateTime) {
            return new DateTimeParser($date);
        }

        return new StringParser($date);
    }

    /**
     * Set the format.
     *
     * @param $format
     * @return $this
     */
    public function format($format)
    {
        $this->format = $format;

        return $this;
    }

    /**
     * Parse the HebrewDate object and return a result based on format.
     *
     * @param string $delimiter
     * @return string
     */
    public function parse($delimiter = " ")
    {
        list($gregorianDay, $gregorianMonth, $gregorianYear) = $this->gregorianDate;
        $julianDate = gregoriantojd($gregorianDay, $gregorianMonth, $gregorianYear);

        $jewishDate = jdtojewish($julianDate);

        $jewishDate = $this->applyFormat(explode('/', $jewishDate));

        return implode($delimiter, $jewishDate);
    }

    /**
     * Apply a format.
     *
     * @param array $jewishDate
     * @return mixed
     */
    protected function applyFormat(array $jewishDate)
    {
        $class = "Domanage\\Parsers\\JewishDate\\{$this->format}";

        return (new $class($jewishDate))->handle();
    }

    /**
     * Convert the HebrewDate object into its string representation.
     *
     * @return string
     */
    public function __toString()
    {
        return $this->parse();
    }
}<commit_msg>refactor HebrewDate internal usage to parse the date just in time<commit_after><?php

namespace Domanage;

use DateTime;
use Carbon\Carbon;
use Domanage\Parsers\GregorianDate\CarbonParser;
use Domanage\Parsers\GregorianDate\StringParser;
use Domanage\Parsers\GregorianDate\DateTimeParser;

/**
 * Class HebrewDate
 *
 * @package Domanage
 */
class HebrewDate
{

    /**
     * The default numeric format.
     *
     * @var string
     */
    const NUMERIC = 'Numeric';

    /**
     * Format for english month date.
     *
     * @var string
     */
    const ENGLISH_MONTH = 'EnglishMonth';

    /**
     * Format for full hebrew date.
     *
     * @var string
     */
    const HEBREW_FULL = 'HebrewFull';

    /**
     * Format for presentation hebrew date.
     *
     * @var string
     */
    const PRESENTABLE_HEBREW_DATE = 'PresentableHebrewDate';

    /**
     * Parsed gregorian date input in mm/dd/yyyy format.
     *
     * @var array
     */
    protected $gregorianDate;

    /**
     * The default format for output.
     *
     * @var string
     */
    protected $format = 'Numeric';

    /**
     * HebrewDate constructor.
     *
     * @param $gregorianDate
     */
    public function __construct($gregorianDate)
    {
        $this->gregorianDate = $gregorianDate;
    }

    /**
     * Create a new HebrewDate instance with given gregorian date.
     *
     * @param $date
     * @return static
     */
    public static function fromGregorian($date)
    {
        return new static($date);
    }

    /**
     * Set the format.
     *
     * @param $format
     * @return $this
     */
    public function format($format)
    {
        $this->format = $format;

        return $this;
    }

    /**
     * Parse the HebrewDate object and return a result based on format.
     *
     * @param string $delimiter
     * @return string
     */
    public function parse($delimiter = " ")
    {
        $julianDate = $this->toJulianDate();

        $jewishDate = $this->applyFormat(
            explode('/', jdtojewish($julianDate))
        );

        return implode($delimiter, $jewishDate);
    }

    /**
     * Get the julian date representation for the current date.
     *
     * @return int
     */
    protected function toJulianDate()
    {
        $gregorianDate = $this->getParser()->handle();
        list($gregorianDay, $gregorianMonth, $gregorianYear) = $gregorianDate;

        return gregoriantojd($gregorianDay, $gregorianMonth, $gregorianYear);
    }

    /**
     * Get the right parser for the date.
     *
     * @return CarbonParser|DateTimeParser|StringParser
     */
    protected function getParser()
    {
        if ($this->gregorianDate instanceof Carbon) {
            return new CarbonParser($this->gregorianDate);
        }

        if ($this->gregorianDate instanceof DateTime) {
            return new DateTimeParser($this->gregorianDate);
        }

        return new StringParser($this->gregorianDate);
    }

    /**
     * Apply a format.
     *
     * @param array $jewishDate
     * @return mixed
     */
    protected function applyFormat(array $jewishDate)
    {
        $class = "Domanage\\Parsers\\JewishDate\\{$this->format}";

        return (new $class($jewishDate))->handle();
    }

    /**
     * Convert the HebrewDate object into its string representation.
     *
     * @return string
     */
    public function __toString()
    {
        return $this->parse();
    }
}
{'fraction_non_alphanumeric': 0.1169714372071936, 'fraction_numerical': 0.0, 'mean_word_length': 2.0455591348366315, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '2532747', 'n_tokens_mistral': 2093, 'n_tokens_neox': 1958, 'n_words': 468}