<?php

namespace openkm\util;

/**
 * FormatUtil
 *
 * @author sochoa
 */
class FormatUtils {

    public static function format($pattern, $arguments = []) {
        
        if (count($arguments) > 0) {
            foreach ($arguments as $key => $value) {                
                $pattern = str_replace('{'.$key.'}',$value, $pattern );
            }                
            return $pattern;
        } else {
            return $pattern;
        }
    }

    private static function isPath($nodeId) {
        return self::startsWith($nodeId, '/');
    }

    private static function isUuid($nodeId) {
        // Case lenght != 36 or does not have four "-" then is not a valid UUID
        return !(strlen($nodeId) != 36 || substr_count($nodeId, '-') != 4);
    }

    public static function formatSize($size) {
        $srt = "BIG";
        if ($size / 1024 < 1) {
            $srt = $size . " Bytes";
        } else if ($size / 1048576 < 1) {
            $srt = self::round(($size / 1024), 1) . " KB";
        } else if ($size / 1073741824 < 1) {
            $srt = self::round(($size / 1048576), 1) . " MB";
        } else if ($size / 1099511627776 < 1) {
            $srt = self::round(($size / 1073741824), 1) . " GB";
        }
        return $srt;
    }

    /**
     * Clean HTML input
     */
    public static function cleanXSS($value) {
        $value = str_replace('&', '&#38;', $value);
        $value = str_replace('<', '&lt;', $value);
        $value = str_replace('>', '&gt;', $value);
        $value = str_replace("'", "&#39;", $value);
        return $value;
    }

    private static function startsWith($string, $startString) {
        $len = strlen($startString);
        return (substr($string, 0, $len) === $startString);
    }

    private static function round($number, $decimals) {
        $factor = pow(10, $decimals);
        return (round($number * $factor) / $factor);
    }

}
