<?php
if (!defined('ABSPATH')) {
    exit;
}

class BMTQB_Formatter {
    public static function money($value, $fallback = '-') {
        if ($value === '' || $value === null) {
            return $fallback;
        }

        return '$' . number_format((float) $value, 2);
    }

    public static function whole($value, $fallback = '-') {
        if ($value === '' || $value === null) {
            return $fallback;
        }

        return number_format((float) $value, 0);
    }

    public static function pdf_url_from_path($path) {
        if (empty($path)) {
            return '';
        }

        $uploads = wp_upload_dir();
        if (empty($uploads['basedir']) || empty($uploads['baseurl'])) {
            return '';
        }

        $basedir = wp_normalize_path($uploads['basedir']);
        $path = wp_normalize_path($path);

        if (strpos($path, $basedir) !== 0) {
            return '';
        }

        return trailingslashit($uploads['baseurl']) . ltrim(str_replace($basedir, '', $path), '/');
    }
}
