<?php


class Format {

    public static function Date($d, $format = 'm-d-Y') {
        return date($format , strtotime($d));
    }


    public static function Phone($phoneNumber) {
        $phoneNumber = preg_replace('/[^0-9]/','',$phoneNumber);

        if(strlen($phoneNumber) > 10) {
            $countryCode = substr($phoneNumber, 0, strlen($phoneNumber)-10);
            $areaCode = substr($phoneNumber, -10, 3);
            $nextThree = substr($phoneNumber, -7, 3);
            $lastFour = substr($phoneNumber, -4, 4);

            $phoneNumber = '+'.$countryCode.' ('.$areaCode.') '.$nextThree.'-'.$lastFour;
        }
        else if(strlen($phoneNumber) == 10) {
            $areaCode = substr($phoneNumber, 0, 3);
            $nextThree = substr($phoneNumber, 3, 3);
            $lastFour = substr($phoneNumber, 6, 4);

            $phoneNumber = '('.$areaCode.') '.$nextThree.'-'.$lastFour;
        }
        else if(strlen($phoneNumber) == 7) {
            $nextThree = substr($phoneNumber, 0, 3);
            $lastFour = substr($phoneNumber, 3, 4);

            $phoneNumber = $nextThree.'-'.$lastFour;
        }

        return $phoneNumber;
    }

    public static function formatCurrency($amount='', $thousands='') {
        // Convert to float and divide by 100
        $formattedAmount = number_format($amount / 100, 2, '.', $thousands);

        return $formattedAmount;
    }


    /**
     * Convert Equifax date strings to MM/DD/YYYY display format.
     * Handles: YYYYMMDD, YYYY-MM-DD, MM/YYYY, MM/DD/YYYY passthrough.
     * Returns null on empty/null input.
     */
    public static function eqFormatDate($date) {
        if (empty($date)) return null;
        $date = trim((string)$date);

        // YYYYMMDD (8 digits)
        if (preg_match('/^\d{8}$/', $date)) {
            $d = \DateTime::createFromFormat('Ymd', $date);
            return $d ? $d->format('m/d/Y') : null;
        }

        // YYYY-MM-DD
        if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
            $d = \DateTime::createFromFormat('Y-m-d', $date);
            return $d ? $d->format('m/d/Y') : null;
        }

        // MM/YYYY — partial date, use first of month
        if (preg_match('/^\d{2}\/\d{4}$/', $date)) {
            $d = \DateTime::createFromFormat('m/Y', $date);
            return $d ? $d->format('m/01/Y') : null;
        }

        // MM/DD/YYYY — already correct format, passthrough
        if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $date)) {
            return $date;
        }

        // Fallback: try strtotime
        $ts = strtotime($date);
        return $ts ? date('m/d/Y', $ts) : null;
    }

} // End Class