src/Services/Common/TextTools.php line 18

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Services/Common/TextTools.php
  4. //----------------------------------------------------------------------
  5. namespace App\Services\Common;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\SocietyGroup;
  8. use App\Entity\Client\Zone;
  9. use App\Services\LogTools;
  10. class TextTools
  11. {
  12.     public function __construct(ManagerRegistry $doctrineLogTools $logTools)
  13.     {
  14.         $this->em $doctrine->getManager();
  15.         $this->logTools $logTools;
  16.     }
  17.     public function formatIkeaBpObjectLogs($text)
  18.     {
  19.         $text str_replace("\t\t\t""<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"$text);
  20.         $text str_replace("\t\t""<br><br>&nbsp;&nbsp;&nbsp;"$text);
  21.         $text str_replace("\t""<br><br>"$text);
  22.         return $text;
  23.     }
  24.     // Mainly used by Sms
  25.     public function removeInsecableSpace($text)
  26.     {
  27.         // Need to transform in html to replace &nbsp; space and transform it again in a plain text
  28.         $text htmlentities($text);
  29.         $textWithoutInsecableSpace str_replace('&nbsp;',' ',$text);
  30.         $text html_entity_decode($textWithoutInsecableSpace);
  31.         return $text;
  32.     }
  33.     public function generateAlphaChars($length 16)
  34.     {
  35.         $charAllowed "abcdefghijqlmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ";
  36.         $sizeCharAllowed mb_strlen($charAllowed);
  37.         $chars "";
  38.         for ($i 0$i $length$i++)
  39.         {
  40.             $letterIndex random_int(0$sizeCharAllowed-1);
  41.             $chars .= $charAllowed[$letterIndex];
  42.         }
  43.         return $chars;
  44.     }
  45.     public function encodeForJson($value)
  46.     {
  47.         if ($value === null)
  48.             return "null";
  49.         if ($value == 0)
  50.             return "0";
  51.         return $value;
  52.     }
  53.     public function arrayToString($minions)
  54.     {
  55.         if (count($minions) < 1)
  56.             return "(0)";
  57.         $ids "";
  58.         foreach ($minions as $minion)
  59.         {
  60.             $ids .= $minion->getId();
  61.             $ids .= ",";
  62.         }
  63.         $ids substr($ids0, -1);
  64.         $ids "(".$ids.")";
  65.         return $ids;
  66.     }
  67.     public function arrayToStringDisplay($minions)
  68.     {
  69.         if (count($minions) < 1)
  70.             return "";
  71.         $output "";
  72.         foreach ($minions as $minion)
  73.         {
  74.             $output .= $minion;
  75.             $output .= ", ";
  76.         }
  77.         $output substr($output0, -1);
  78.         $output substr($output0, -1);
  79.         return $output;
  80.     }
  81.     public function camelToSnakeCase($text)
  82.     {
  83.         // preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $text, $matches);
  84.         // $ret = $matches[0];
  85.         // foreach ($ret as &$match)
  86.         // {
  87.         //     $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
  88.         // }
  89.         // return implode('_', $ret);
  90.         return strtolower(preg_replace(['/([a-z\d])([A-Z])/''/([^_])([A-Z][a-z])/'], '$1_$2'$text));
  91.     }
  92.     public function cleanTextWithoutHtmlentities($text$removeEOL 1)
  93.     {
  94.         // Plan.io Task #3953
  95.         if (empty($text)) return $text;
  96.         $text str_replace('"'"'"$text);
  97.         $text str_replace('œ''oe'$text);
  98.         $text str_replace('’'"'"$text);
  99.         $text preg_replace('/[\t]/'' 'trim($text));
  100.         if($removeEOL == 1)
  101.         {
  102.             $text str_replace(array("\r\n""\n""\r"), ' '$text);
  103.         }
  104.         else if($removeEOL == 2)
  105.         {
  106.             $text str_replace(array("\r\n""\n""\r"), '<br>'$text);
  107.         }
  108.         return $text;
  109.     }
  110.     public function cleanText($text$removeEOL 1)
  111.     {
  112.         // Plan.io Task #3953
  113.         if (empty($text)) return $text;
  114.         $text str_replace('"'"'"$text);
  115.         $text str_replace('œ''oe'$text);
  116.         $text str_replace('’'"'"$text);
  117.         $text preg_replace('/[\t]/'' 'trim($text));
  118.         if($removeEOL == 1)
  119.         {
  120.             $text str_replace(array("\r\n""\n""\r"), ' '$text);
  121.         }
  122.         else if($removeEOL == 2)
  123.         {
  124.             $text str_replace(array("\r\n""\n""\r"), '<br>'$text);
  125.         }
  126.         return htmlentities($text);
  127.     }
  128.     public function strtoupperFr($string)
  129.     {
  130.         // Plan.io Task #3953
  131.         if (empty($string)) return $string;
  132.         $string strtoupper($string);
  133.         $string str_replace(
  134.             array('é''è''ê''ë''à''â''î''ï''ô''ù''û'),
  135.             array('É''È''Ê''Ë''À''Â''Î''Ï''Ô''Ù''Û'),
  136.             $string
  137.         );
  138.         return $string;
  139.     }
  140.     public function removeWhitespaces($string)
  141.     {
  142.         // Plan.io Task #3953
  143.         if (empty($string)) return $string;
  144.         $string preg_replace('/\s+/'''$string);
  145.         return $string;
  146.     }
  147.     public function cropText($text$maxNbChar)
  148.     {
  149.         // Plan.io Task #3953
  150.         if (empty($text)) return $text;
  151.         // If title si too long, crop it
  152.         if(strlen($text) > $maxNbChar-3)
  153.         {
  154.             return mb_substr($text,0,$maxNbChar'UTF-8') . "...";
  155.         }
  156.         return $text;
  157.     }
  158.     public function encode_utf8($string$encode null)
  159.     {
  160.         $fromEncoding mb_detect_encoding($string);
  161.         if ($fromEncoding === false)
  162.         {
  163.             return $string;
  164.         }
  165.         if ($encode === null)
  166.         {
  167.             $encode 'UTF-8';
  168.         }
  169.         try
  170.         {
  171.             $stringEncoded mb_convert_encoding($string$encode$fromEncoding);
  172.         }
  173.         catch(\Exception $e)
  174.         {
  175.             return $string;
  176.         }
  177.         if ($stringEncoded === false)
  178.         {
  179.             return $string;
  180.         }
  181.         return $stringEncoded;
  182.     }
  183.     public function removeNonAlphaNumeric($string)
  184.     {
  185.         $string preg_replace('/[^a-z\d ]/i'''$string);
  186.         return $string;
  187.     }
  188.     public function craftSimpleNameForZone(SocietyGroup $societyGroupZone $zone)
  189.     {
  190.         $display $this->removeNonAlphaNumeric($societyGroup->getRef());
  191.         $display $this->camelToSnakeCase($display);
  192.         $societyGroupDisplay $societyGroup->getId()."_".$display;
  193.         $display $zone->getDisplay();
  194.         $display $this->removeNonAlphaNumeric($display);
  195.         $display $this->camelToSnakeCase($display);
  196.         $zoneDisplay $zone->getId()."_".$display;
  197.         return $societyGroupDisplay."_".$zoneDisplay;
  198.     }
  199.     public function toUpperASCII$str )
  200.     {
  201.         return strtoupper(strtr(utf8_decode($str),
  202.             utf8_decode(
  203.             'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ'),
  204.             'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy'));
  205.     }
  206.     public function parseEmailConfigData($string)
  207.     {
  208.         $string preg_replace('/[^a-z_]/'''$string);
  209.         return $string;
  210.     }
  211. }