<?php

function debug_var($input) {
    @ob_start();
    print_r($input);
    $content = ob_get_contents();
    @ob_end_clean();

    echo join("\r", ['<textarea cols="80" rows="20">', $content,  '</textarea>']) ;
}

function init_autoload(){
    $candidates = [
        ROOT_DIR . '/package/vendor/autoload.php',
        ROOT_DIR . '/vendor/autoload.php'
    ];

    $autoload = null;
    foreach ($candidates as $file) {
        if (file_exists($file)) {
            $autoload = $file;
            break;
        }
    }

    if (!$autoload) {
        $paths = implode("', '", $candidates);
        trigger_error("Composer autoload not found. Expected one of: '" . $paths . "'.\nRun 'composer install' in the 'package' directory (or the project root) to install dependencies.", E_USER_ERROR);
    }

    $classLoader = require_once $autoload;
    if (is_object($classLoader) && method_exists($classLoader, 'add')) {
        $classLoader->add("Hura8", ROOT_DIR . '/inc');
    }

    return $classLoader;
}

//get current paging id
function getPageId(){
    return getRequestInt('page', 1);
}

function getPageSize($default=10){
    return getRequestInt('pageSize', $default);
}

//Function to get the post value in submit
function getPost($var, $default="", $encode = false, $keep_tag=""){
    return isset($_POST[$var]) ? $_POST[$var] : $default;
}

//Function to get the INTERGER request value of a variable
function getRequestInt($var, $min_value = 0){
    $request = isset($_REQUEST[$var]) ? (int) $_REQUEST[$var] : (int) $min_value;
    $request = ($request >= $min_value ) ? $request : $min_value; //if user tampers request parameter
    return $request;
}


//Function to get the request value of a variable
function getRequest($var, $default=""){
    return $_REQUEST[$var] ?? $default;
}

