Peeks until a token with the given type is found.

@param  array|integer     $type
@param  array|string|null $value
@return Token|null
343030
public function peekUntil($type, $value = null)
    {
        while($token = $this->peek() and !$token->test($type, $value)) {
            $token = null;
        }
        return $token;
    }
1044882
public function takeToken($type, $value = null)
    {
        return array(
            'type'     => $type,
            'line'     => $this->lineNumber,
            'value'    => $value ?: null,
            'deferred' => false
        );
    }
850369
public function elseValue($value = null, $type = null)
    {
        if (is_array($value)) {
            end($value);
            $value = key($value);
        }

        if ($value !== null && !$value instanceof ExpressionInterface) {
            $value = $this->_castToExpression($value, $type);
        }

        if (!$value instanceof ExpressionInterface) {
            $value = ['value' => $value, 'type' => $type];
        }

        $this->_elseValue = $value;
    }
998198
public static function setType($value, $type, $nullable = TRUE)
	{
		if (!$nullable || NULL !== $value) {
			settype($value, $type);
		}

		return $value;
	}
136989
private function parseDate(string $type, $value)
    {
        try {
            switch ($type) {
                case 'datetime':
                    $datetime = DateTime::createFromFormat('Y-m-d\TH:i:s', $value);
                    break;

                case 'date':
                    $datetime = Date::createFromFormat('Y-m-d', $value);
                    break;

                case 'time':
                    $datetime = Time::createFromFormat('H:i:s', $value);
                    break;

                default:
                    throw new \InvalidArgumentException(sprintf("Invalid parse date type '%s'", $type));
            }
        } catch (\Exception $exception) {
            return null;
        }

        return $datetime;
    }
286305
public function accept(int $type, $value = null) : bool
    {
        if ($this->getCurrentToken()->getType() === $type) {
            if ($value) {
                if ($this->getCurrentToken()->getValue() === $value) {
                    return true;
                }

                return false;
            }

            return true;
        }

        return false;
    }
1232005
private function cast($value, $type)
    {
        switch ($type) {
            case 'bool':
                return boolval($value);
                break;
            case 'int':
                return intval($value);
                break;
            case 'float':
                return floatval($value);
                break;
            case 'string':
                return $value;
                break;
            case 'array':
                if (is_array($value)) {
                    return serialize($value);
                } else {
                    return unserialize($value);
                }
                break;
            default:
                return $value;
        }
    }
991584
public static function encode($value, string $type = null): string
    {
        return self::run(self::ENCODER, $value, $type);
    }
1106703
public function prepareValue(&$value, $type)
    {
        if ($value === null) {
            return $value = 'NULL';
        }

        if (is_array($value)) {
            foreach ($value as &$v) {
                $this->prepareValue($v, $type);
            }

            return '(' . implode(', ', $value) . ')';
        }

        switch (strtolower($type)) {
            case 'timestamp':
                $value = is_int($value)
                    ?
                    '\'' . date(static::DATETIME_FORMAT, $value) . '\''
                    :
                    ($value instanceof \DateTime
                        ?
                        '\'' . $value->format(static::DATETIME_FORMAT) . '\''
                        :
                        '\'' . $this->connection->escape($value) . '\''
                    );
                break;

            case 'bit':
                $value = (int)intval($value);
                break;

            case 'dec':
            case 'decimal':
            case 'tinyint':
            case 'smallint':
            case 'bigint':
            case 'int':
                $value = (int)intval($value);
                break;
            case 'double':
            case 'float':
                $value = (float)floatval($value);
                break;

            default:
                $value = '\'' . $this->connection->escape($value) . '\'';
        }

        return $value;
    }
484764
protected function typecastValue($value, $type)
    {
        if (is_scalar($type)) {
            if (is_object($value) && method_exists($value, '__toString')) {
                $value = $value->__toString();
            }

            switch ($type) {
                case self::TYPE_INTEGER:
                    return (int)$value;
                case self::TYPE_FLOAT:
                    return (float)$value;
                case self::TYPE_BOOLEAN:
                    return (bool)$value;
                case self::TYPE_STRING:
                    return (string)$value;
                case self::TYPE_JSON:
                    return is_string($value) ? $value : json_encode($value);
                case self::TYPE_IMPLODE:
                    return is_string($value) ? $value : (string)implode($this->delimiter, $value);
                case self::TYPE_SERIALIZE:
                    return is_string($value) ? $value : serialize($value);
                default:
                    throw new InvalidParamException("Unsupported type '{$type}'");
            }
        }

        return call_user_func($type, $value);
    }
309192
private function checkType($type, $value)
    {        
        switch($type)
        {
            case 'bool':
            case 'boolean':
                $codeException = (!is_bool($value)) ? 1 : 0;
                break;
            case 'int':
            case 'integer':
                $codeException = (!is_int($value)) ? 1 : 0;
                break;
            case 'string':
                $codeException = (!is_string($value)) ? 1 : 0;
                break;
            case 'not_resource':
                $codeException = (is_resource($value)) ? 2 : 0;
                break;
            default :
                $codeException = 3;
                break;
        }
        
        return $codeException;
    }
364639
public static function decodeFromBase64(string $value, string $type = null)
    {
        return self::run(self::DECODER, \base64_decode($value), $type);
    }
359025
private function isBlock($type, $value = NULL)
    {
        return $this->_current == $type
        && (NULL === $value ? true : $this->_blocks[$this->_pos][3] == $value);
    }
856146
public function setValue($value = null, $type = self::T_TEXT)
    {
        unset($this->rawValue);
        unset($this->value);
        $this->_initValue($value, $type);
    }
843711
private static function isSimpleType($value, string $type): ?bool
    {
        switch ($type) {
            case 'array':
                return static::isArray($value);
                break;
            case 'object':
                return static::isObject($value);
                break;
            case 'bool':
                return static::isBool($value);
                break;
            case 'int':
                return static::isInt($value);
                break;
            case 'float':
                return static::isFloat($value);
                break;
            case 'string':
                return static::isString($value);
                break;
            case 'callable':
                return static::isCallable($value);
                break;
            default:
                break;
        }

        return null;
    }
891343
public static function tryGetTypeForValue($value)
    {
        if (!is_object($value)) {
            return null;
        }

        // maybe it's a ValueObject

        // try get type name from types map
        $className = get_class($value);
        if (isset(self::$typesMap[$className])) {
            return Type::getType(self::$typesMap[$className]);
        }

        // use class name as type name
        $classNameParts = explode('\\', $className);
        $typeName = array_pop($classNameParts);

        if (array_key_exists($typeName, Type::getTypesMap())) {
            return Type::getType($typeName);
        }

        return null;
    }
211029
public static function TreatValueAs( $value, $type)
	{
		if ( $value instanceof Undefined )
		{
			return $value;
		}

		if ( is_null( $value ) )
		{
			$value = CoreFuncs::$False;
		}

		if ( $type->TypeCode == XmlTypeCode::None)
			throw XPath2Exception::withErrorCodeAndParams( "XPDY0050", Resources::XPDY0050,
				array(
					$type,
					"empty-sequence()"
				)
			);

		if ( Type::FromValue( $value ) != $type->ItemType && $type->ItemType != Types::$ObjectType )
		{
			if ( $type->ItemType == Types::$IntegerType )
			{
				return Integer::ToInteger( $value, null );
			}
			else if ( $type->ItemType == Types::$DecimalType )
			{
				return DecimalValue::FromValue( $value );
				return $value + 0.0;
			}
			throw XPath2Exception::withErrorCodeAndParams( "XPDY0050", Resources::XPDY0050,
				array(
					$type,
					SequenceType::WithTypeCodeAndCardinality( SequenceType::GetXmlTypeCodeFromObject( $value ), XmlTypeCardinality::One ),
				)
			);
		}
		return $value;
	}