protected function toScalar($value)
    {
        if (is_scalar($value)) {
            return $value;
        } elseif (null === $value) {
            return 'NULL';
        } else {
            if (is_object($value)) {
                if (is_callable([$value, '__toString'])) {
                    return (string) $value;
                } elseif ($value instanceof \DateTimeInterface) {
                    return $value->format('Y-m-d H:i:s');
                } else {
                    throw new \InvalidArgumentException(sprintf("Cast of class %s is impossible", get_class($value)));
                }
            } else {
                throw new \InvalidArgumentException(sprintf("Cast of type %s is impossible", gettype($value)));
            }
        }
    }