using Php;

namespace PhpNative {
    private string? active_output_buffer = null;

    public void output_buffer_start () {
        active_output_buffer = "";
    }

    public bool output_buffer_is_active () {
        return active_output_buffer != null;
    }

    public string output_buffer_get_clean () {
        string result = active_output_buffer ?? "";
        active_output_buffer = null;
        return result;
    }

    public void output_buffer_write (string value) {
        if (active_output_buffer != null) {
            active_output_buffer += value;
            return;
        }
        stdout.printf ("%s", value);
    }

    public void echo_static (string value, string php_file, int php_line) throws Error {
        output_buffer_write (value);
    }

    public zval echo_zval (zval value, string php_file, int php_line) throws Error {
        echo_static (zval_to_string (value), php_file, php_line);
        return zval.null_value ();
    }
}
