Create a Interval from an expression
@param string $expression
@return Interval
@throws \InvalidArgumentException
@throws \UnexpectedValueException
@throws \RangeException
@throws \ErrorException
765084
public function parse(string $expression): Interval
    {
        $parse = \explode(',', $expression);
        if (self::BOUNDARIES !== count($parse)) {
            throw new \ErrorException('Parse interval expression');
        }

        $startTerm = \reset($parse);
        $endTerm   = \end($parse);

        $startTerm = $this->parseStartTerm($startTerm);
        $endTerm   = $this->parseEndTerm($endTerm);

        return new Interval(
            $startTerm,
            $endTerm
        );
    }
1087617
final public static function recalculateInterval($interval)
    {
        try {
            $from = new \DateTimeImmutable();
            $to = $from->add($interval);
            $diff = $to->diff($from);
            foreach ($diff as $k => $v) {
                $interval->$k = $v;
            }

        } catch (\Exception $e) {
        }

        return $interval;
    }
817482
public function withExpression(string $expression): self
    {
        if (!(new \ReflectionClass($expression))->implementsInterface(Expression::class)) {
            throw new DomainException($expression);
        }

        $self = clone $this;
        $self->expression = new $expression($self->name);

        return $self;
    }
248056
private function ensureArray($expression)
    {
        if (is_array($expression)) {
            $array = array();
            foreach ($expression as $index => $value) {
                $array[$index] = $this->ensureArray($value);
            }

            return $array;
        } elseif ($expression instanceof self) {
            return $expression->getExpression();
        }

        return $expression;
    }
115345
public function Add( $duration )
	{
		if ( ! $duration instanceof DurationValue )
		{
			throw new \InvalidArgumentException();
		}

		$thisSeconds = $this->getTotalSeconds();
		$durationSeconds = $duration->getTotalSeconds();

		/**
		 * @var DecimalValue $interval
		 */
		$interval = $thisSeconds->Add( $durationSeconds );
		$hasValue = 0 > $interval->CompareTo( 0 ) != 0;
		$invert = 0 > $interval->CompareTo( 0 ) == -1;

		if ( ! $hasValue )
		{
			$di = new \DateInterval( $this->ZeroStringValue() );
			$di->hasValue = false;
			$di->microseconds = 0;
			return $di;
		}

		$di = new \DateInterval( "PT{$interval->getAbs()->getIntegerPart()}S" );

		// Normalize
		$dt = new \DateTimeImmutable();
		$diff = $dt->add( $di );
		$diff = $diff->diff( $dt );
		$diff->hasValue = true;
		$diff->invert = $invert;
		$diff->microseconds = $interval->getDecimalPart() + 0;
		// if ( $hasValue ) $this->type = XPATH20_DURATION_TYPE_DAYTIME;
		$this->Value = $diff;
	}
645318
public static function createFromDateString($time)
    {
        $interval = parent::createFromDateString($time);
        if ($interval instanceof DateInterval && !($interval instanceof static)) {
            $interval = static::instance($interval);
        }

        return static::instance($interval);
    }
1153175
public static function cronExpression($expression) {

        try {

            $cron = CronExpression::factory($expression);

            $s = $cron->getNextRunDate()->format('c');

            $e = $cron->getExpression();

            $e_array = preg_split('/\s/', $e, -1, PREG_SPLIT_NO_EMPTY);

            $e_count = count($e_array);

            if ( $e_count < 5 || $e_count > 6 ) throw new Exception($e." is not a valid cron expression");

            if ( $e_count == 5 ) $e_array[] = "*";

        }
        catch (Exception $e) {

            throw $e;

        }

        return array($s, $e_array);

    }
1195622
protected function replaceArgumentExpression($matches) {
		//count available matches
		//this indicates the type of expression to replace
		$total_matches = count($matches);
		$type = $subindex = null;
	
		//%{TYPE|CLASS@1}
		if ($total_matches == 2) {
			//check if there are arguments left
			if ($this->counter >= count($this->args))
				throw new \OutOfBoundsException("No arguments left for expression '{$matches[0]}'");

			return $this->castParameter($this->args[$this->counter++], $matches[1]);
		}
	
		switch ($total_matches) {
			/*
			 * Simple index
			 */
			case 5: //%{NUMBER@2[INDEX]@3?:TYPE@4}
				$type = substr($matches[4], 1);
			case 4: //%{NUMBER@2[INDEX]@3}
				$subindex = empty($matches[3]) ? null : substr($matches[3], 1, -1);
			case 3: //%{NUMBER@2}
				$index = intval($matches[2]);
					
				if (!array_key_exists($index, $this->args))
					throw new \InvalidArgumentException("No value found on index $index");
	
				if ($index == 0 && !is_null($subindex))
					return $this->getIndex($subindex, null, $type);
				
				return $this->getSubIndex($this->args[$index], $subindex, $type);
	
			/*
			 * Interval
			 */
			case 10: //%{NUMBER@5[LEFT@7?..RIGHT@8?]:TYPE@9}
				$type = substr($matches[9], 1);
			case 9: //%{NUMBER@5[LEFT@7?..RIGHT@8?]}
				$index = intval($matches[5]);
					
				if (!array_key_exists($index, $this->args))
					throw new \InvalidArgumentException("No value found on index $index");
					
				return $this->getSubRange($this->args[$index], $matches[7], $matches[8], $type);
		}
	}
956851
public function postProcessExpression(Expression $expression)
    {
        if ($this->hasHash($expression->hashCode())) {
            $alias = $this->getAlias($expression->hashCode());
            $alias = new Expr\Column($alias);
            $alias->setBuilder($this);
            $expression = new Expr\Raw(sprintf('%s AS %s', $expression->toSQL(), $alias->toSQL()));
        }
        
        return $expression;
    }
688694
private function compute(string $expression): int
    {
        // catch anything which could be a security issue
        if (0 !== preg_match("/[^\d.+*%^|&~<>\/()-]/", $this->expression)) {
            throw new \InvalidArgumentException('Wrong value in error types config value:' . $this->expression);
        }

        return 0 + (int)eval('return ' . $expression . ';');
    }
136346
private function convertErrorConstants(string $expression): string
    {
        $output = preg_replace_callback('/(E_[a-zA-Z_]+)/', function ($errorConstant) {
            if (defined($errorConstant[1])) {
                return constant($errorConstant[1]);
            }

            return $errorConstant[0];
        }, $expression);

        if (null === $output) {
            throw new \InvalidArgumentException('Unable to parse error types string: ' . $expression);
        }

        return $output;
    }
77193
public static function range(float $start, float $end, float $interval = 1.) : self
    {
        if ($interval <= 0.) {
            throw new InvalidArgumentException('Interval must be greater'
                 . " than 0, $interval given.");
        }

        return static::quick(range($start, $end, $interval));
    }
989998
public function build(ExpressionInterface $expression, array &$params = [])
    {
        $args = $expression->getArguments();

        $placeholders = [];
        foreach ($args as $argument) {
            if ($argument instanceof ExpressionInterface) {
                $placeholders[] = $this->queryBuilder->buildExpression($argument, $params);
                continue;
            }

            $placeholders[] = $this->queryBuilder->bindParam($argument, $params);
        }

        return $this->buildCallString($expression->getProcedureName(), implode(', ', $placeholders));
    }
479475
protected function getCacheLifetimeForExpression($expression)
    {
        if (!CronExpression::isValidExpression($expression)) {
            return 0;
        }

        $now = new \DateTime();
        $cronExpression = CronExpression::factory($expression);
        $endTime = $cronExpression->getNextRunDate($now);

        return $endTime->getTimestamp() - $now->getTimestamp();
    }
59093
public static function fromString($constraintExpression)
    {
        if (!preg_match('/^(==?|\!=?|~|[<>]=?)?((?:\*|[0-9]+)(?:.(?:\*|[0-9]+))*)(?:\@(\w+))?$/', $constraintExpression, $m)) {
            throw new \InvalidArgumentException("Constraint expression '$constraintExpression' could not be parsed");
        }
        return new self(
            !empty($m[1]) ? $m[1] : '=',
            $m[2],
            !empty($m[3]) ? $m[3] : 'stable'
        );
    }
1018796
public function interpolate( $tolerance )
	{
		$a	= $this->interval->getStart();
		$b	= $this->interval->getEnd();
		$c	= false;
		do{
			$ya	= $this->formula->getValue( $a );
			$yb	= $this->formula->getValue( $b );

			if( $ya * $yb > 0 )
			{
				throw new InvalidArgumentException( 'Formula has no 0 in Interval '.(string)$this->interval.'.' );
				break;
			}
			$c	= ( $a * $yb - $b * $ya ) / ( $yb - $ya );
			$found = $c - $a <= $tolerance || $b - $c <= $tolerance;
			if( $c >= 0 && $a >= 0 || $c < 0 && $a < 0 )
				$a	= $c;
			else
				$b	= $c;
		}
		while( !$found );
		return $c;
	}
841759
public function add(ExpressionContract $expression)
    {
        $this->checkType($expression);
        $this->constraints[$expression->name()] = $expression;
        return $this;
    }