<?php
/*
 * lattefilters.class.php
 * @author Jérôme Brilland
 * @version 1.0
 * @copyright (c) 2023, Jérôme Brilland
 * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License
 */

class LatteFilters {
    
    protected $locale = null;
    
    public function __construct( string $locale ) {
        $this->locale = $locale;
    }

    public function load( string $filter ): ?callable {
        if ( in_array( $filter, get_class_methods( $this ) ) ) {
            return [$this, $filter];
        }
        return null;
    }
    
    public function formatDate( $date, string $format ) {
        $formatDictionary = [
            'full' => IntlDateFormatter::FULL,
            'long' => IntlDateFormatter::LONG,
            'medium' => IntlDateFormatter::MEDIUM,
            'short' => IntlDateFormatter::SHORT,
            'none' => IntlDateFormatter::NONE
        ];
        return ( !empty( $date ) ) ? ( new IntlDateFormatter( $this->locale, $formatDictionary[$format], IntlDateFormatter::NONE ) )->format( date_create( $date ) ) : '';
    }

}
