<?php

/**
 *JOHNN GUTIERREZ, 
 *FECHA DE CREACION   2021-02-23
 *ULTIMA MODIFICASION 2021-02-23
 */
//ini_set('display_errors', 1);
//ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

class func_tools
{
    private $oMyConn;


    public function __construct($pConn)
    {
        $oMyConn = $pConn;
    }

    private  function Div_result_Decimal($pNumber)
    {
        $whole = floor($pNumber); // 1
        $fraction = $pNumber - $whole; // .25
        return $fraction;
    }
    
    private function isUTF8($encoding, $value)
    {
        return (($encoding === 'UTF-8') && (utf8_encode(utf8_decode($value)) === $value));
    }
 
    public static function return_field_type_value($field_type)
    {
        switch ($field_type) {
            case MYSQLI_TYPE_DECIMAL:
                return 0.00;
                break;
            case MYSQLI_TYPE_NEWDECIMAL:
                return 0.00;
                break;
            case MYSQLI_TYPE_FLOAT:
                return 0.00;
                break;
            case MYSQLI_TYPE_DOUBLE:
                return 0.00;
                break;
            case MYSQLI_TYPE_BIT:
                return 0;
                break;
            case MYSQLI_TYPE_TINY:
                return 0;
                break;
            case MYSQLI_TYPE_SHORT:
                return 0;
                break;
            case MYSQLI_TYPE_LONG:
                return 0;
                break;
            case MYSQLI_TYPE_LONGLONG:
                return 0;
                break;
            case MYSQLI_TYPE_INT24:
                return 0;
                break;
            case MYSQLI_TYPE_YEAR:
                return 0;
                break;
                // case MYSQLI_TYPE_ENUM: ;
            case MYSQLI_TYPE_TIMESTAMP:
                return '2009-01-01 01:00:00';
                break;
            case MYSQLI_TYPE_DATE:
                return '2009-01-01';
                break;
            case MYSQLI_TYPE_TIME:
                return '01:00:00';
                break;
            case MYSQLI_TYPE_DATETIME:
                return '2009-01-01 01:00:00';
                break;
            case MYSQLI_TYPE_NEWDATE:
                return '2009-01-01';
                break;
                // case MYSQLI_TYPE_INTERVAL:
                // case MYSQLI_TYPE_SET:
            case MYSQLI_TYPE_VAR_STRING:
                return '';
                break;
            case MYSQLI_TYPE_STRING:
                return '';
                break;
            case MYSQLI_TYPE_CHAR:
                return '';
                break;
                // case MYSQLI_TYPE_GEOMETRY: return 's';

            case MYSQLI_TYPE_TINY_BLOB:
                return null;
                break;
            case MYSQLI_TYPE_MEDIUM_BLOB:
                return null;
                break;
            case MYSQLI_TYPE_LONG_BLOB:
                return null;
                break;
            case MYSQLI_TYPE_BLOB:
                return null;
                break;
            default:
                trigger_error("unknown type: $field_type");
                return null;
                break;
        }
    }

    public static function randStrGen($len)
    {
        $result = "";
        $chars = 'abcdefghijklmnopqrstuvwxyz$_?!-0123456789';
        $charArray = str_split($chars);
        for ($i = 0; $i < $len; $i++) {
            $randItem = array_rand($charArray);
            $result .= "" . $charArray[$randItem];
        }
        return $result;
    }

    public static function randomNumber($length)
    {
        $min = str_repeat(0, $length - 1) . 1;
        $max = str_repeat(9, $length);
        return mt_rand($min, $max);
    }

    public static function valid_null_ceros($cValue, $cTipo)
    {
        $cValue = trim($cValue);
        $cTipo = trim($cTipo);
        if (($cValue == '') || (strtoupper($cValue) == 'NULL')) {
            if (strtoupper($cTipo) == 'I')
                $cValue = '0';
            else
                $cValue = '';
        }
        return $cValue;
    }

      public static function utf8tify(&$value)
    {
        $encodings = [
            'UTF-8',
            'ISO-8859-1',
            'ASCII'
        ];

        mb_internal_encoding('UTF-8');
        mb_substitute_character(0xfffd); // REPLACEMENT CHARACTER
        mb_detect_order($encodings);

        $stringEncoding = mb_detect_encoding($value, $encodings, true);

        if (!$stringEncoding) {
            $value = null;
            throw new \RuntimeException("Unable to identify character encoding in sanitizer.");
        }

        if (isUTF8($stringEncoding, $value)) {
            return;
        } else {
            $value = mb_convert_encoding($value, 'UTF-8', $stringEncoding);
            $stringEncoding = mb_detect_encoding($value, $encodings, true);

            if (isUTF8($stringEncoding, $value)) {
                return;
            } else {
                $value = null;
                throw new \RuntimeException("Unable to convert character encoding from ISO-8859-1, or ASCII, to UTF-8 in sanitizer.");
            }
        }

        return;
    }

    private function check_field_type($pConn, $schemaName, $TableName, $FieldName)
    {
        try {
            $Sql_Line = "SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = '" . $schemaName . "' AND TABLE_NAME = '" . $TableName . "' AND COLUMN_NAME = '" . $FieldName . "'";
            $data_flds = $pConn->query($Sql_Line)->fetchArray();
            $value = $data_flds['DATA_TYPE'];
            $data_flds = null;
            return $value;
        } catch (PDOException $e) {
            print $e->getMessage();
        }
    }

    /* CONVERT A RECORDSET TO JSON AND CSV */
    public static function array2json($aData)
    {
        return  json_encode($aData);
    }

    public static function clean($string)
    {
        $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
        $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

        return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
    }
}
 
?>