Skip to content
Snippets Groups Projects
TextFormatModel.php 3.21 KiB
Newer Older
  • Learn to ignore specific revisions
  • Damien's avatar
    Damien committed
    <?php
    
    /**
    * Copyright Maarch since 2008 under licence GPLv3.
    * See LICENCE.txt file at the root folder for more details.
    * This file is part of Maarch software.
    *
    */
    
    /**
    
    Damien's avatar
    Damien committed
    * @brief Text Format Model
    
    Damien's avatar
    Damien committed
    * @author dev@maarch.org
    */
    
    
    Damien's avatar
    Damien committed
    namespace SrcCore\models;
    
    Damien's avatar
    Damien committed
    class TextFormatModel
    
    Damien's avatar
    Damien committed
    {
        public static function normalize(array $aArgs)
        {
            ValidatorModel::notEmpty($aArgs, ['string']);
            ValidatorModel::stringType($aArgs, ['string']);
    
            $a = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿŔŕ';
            $b = 'aaaaaaaceeeeiiiidnoooooouuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr';
    
            $string = utf8_decode($aArgs['string']);
            $string = strtr($string, utf8_decode($a), $b);
            $string = strtolower($string);
    
            return utf8_encode($string);
        }
    
        public static function formatDate($date, $format = null)
    
    Damien's avatar
    Damien committed
            if (empty($date)) {
                return '';
    
    Damien's avatar
    Damien committed
            $date = new \DateTime($date);
    
    
            if (!empty($format)) {
                return $date->format($format);
            }
    
    
    Damien's avatar
    Damien committed
            return $date->format('d-m-Y H:i');
    
    Damien's avatar
    Damien committed
        public static function removeAccent(array $aArgs)
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['string']);
            ValidatorModel::stringType($aArgs, ['string', 'charset']);
    
    
            if (empty($aArgs['charset'])) {
    
    Damien's avatar
    Damien committed
                $aArgs['charset'] = 'utf-8';
            }
    
            $string = htmlentities($aArgs['string'], ENT_NOQUOTES, $aArgs['charset']);
    
            $string = preg_replace('#\&([A-za-z])(?:uml|circ|tilde|acute|grave|cedil|ring)\;#', '\1', $string);
            $string = preg_replace('#\&([A-za-z]{2})(?:lig)\;#', '\1', $string);
            $string = preg_replace('#\&[^;]+\;#', '', $string);
    
            return $string;
        }
    
    
        public static function htmlWasher($html)
    
            $html = str_replace("<br/>", "\\n", $html);
            $html = str_replace("<br />", "\\n", $html);
            $html = str_replace("<br/>", "\\n", $html);
            $html = str_replace("&nbsp;", " ", $html);
            $html = str_replace("&eacute;", "\u00e9", $html);
            $html = str_replace("&egrave;", "\u00e8", $html);
            $html = str_replace("&ecirc;", "\00ea", $html);
            $html = str_replace("&agrave;", "\u00e0", $html);
            $html = str_replace("&acirc;", "\u00e2", $html);
            $html = str_replace("&icirc;", "\u00ee", $html);
            $html = str_replace("&ocirc;", "\u00f4", $html);
            $html = str_replace("&ucirc;", "\u00fb", $html);
            $html = str_replace("&acute;", "\u0027", $html);
            $html = str_replace("&deg;", "\u00b0", $html);
            $html = str_replace("&rsquo;", "\u2019", $html);
    
    
            return $html;
        }
    
    
        public static function cutString(array $aArgs)
        {
            ValidatorModel::notEmpty($aArgs, ['string']);
            ValidatorModel::stringType($aArgs, ['string']);
            ValidatorModel::intType($aArgs, ['max']);
    
            $string = $aArgs['string'];
            $max    = $aArgs['max'];
            if (strlen($string) >= $max) {
                $string = substr($string, 0, $max);
                $espace = strrpos($string, " ");
                $string = substr($string, 0, $espace)."...";
                return $string;
            } else {
                return $string;
            }
        }