<?php
//----------------------------------------------------------------------
// src/Services/Common/TextTools.php
//----------------------------------------------------------------------
namespace App\Services\Common;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\SocietyGroup;
use App\Entity\Client\Zone;
use App\Services\LogTools;
class TextTools
{
public function __construct(ManagerRegistry $doctrine, LogTools $logTools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
}
public function formatIkeaBpObjectLogs($text)
{
$text = str_replace("\t\t\t", "<br><br> ", $text);
$text = str_replace("\t\t", "<br><br> ", $text);
$text = str_replace("\t", "<br><br>", $text);
return $text;
}
// Mainly used by Sms
public function removeInsecableSpace($text)
{
// Need to transform in html to replace space and transform it again in a plain text
$text = htmlentities($text);
$textWithoutInsecableSpace = str_replace(' ',' ',$text);
$text = html_entity_decode($textWithoutInsecableSpace);
return $text;
}
public function generateAlphaChars($length = 16)
{
$charAllowed = "abcdefghijqlmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ";
$sizeCharAllowed = mb_strlen($charAllowed);
$chars = "";
for ($i = 0; $i < $length; $i++)
{
$letterIndex = random_int(0, $sizeCharAllowed-1);
$chars .= $charAllowed[$letterIndex];
}
return $chars;
}
public function encodeForJson($value)
{
if ($value === null)
return "null";
if ($value == 0)
return "0";
return $value;
}
public function arrayToString($minions)
{
if (count($minions) < 1)
return "(0)";
$ids = "";
foreach ($minions as $minion)
{
$ids .= $minion->getId();
$ids .= ",";
}
$ids = substr($ids, 0, -1);
$ids = "(".$ids.")";
return $ids;
}
public function arrayToStringDisplay($minions)
{
if (count($minions) < 1)
return "";
$output = "";
foreach ($minions as $minion)
{
$output .= $minion;
$output .= ", ";
}
$output = substr($output, 0, -1);
$output = substr($output, 0, -1);
return $output;
}
public function camelToSnakeCase($text)
{
// preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $text, $matches);
// $ret = $matches[0];
// foreach ($ret as &$match)
// {
// $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
// }
// return implode('_', $ret);
return strtolower(preg_replace(['/([a-z\d])([A-Z])/', '/([^_])([A-Z][a-z])/'], '$1_$2', $text));
}
public function cleanTextWithoutHtmlentities($text, $removeEOL = 1)
{
// Plan.io Task #3953
if (empty($text)) return $text;
$text = str_replace('"', "'", $text);
$text = str_replace('œ', 'oe', $text);
$text = str_replace('’', "'", $text);
$text = preg_replace('/[\t]/', ' ', trim($text));
if($removeEOL == 1)
{
$text = str_replace(array("\r\n", "\n", "\r"), ' ', $text);
}
else if($removeEOL == 2)
{
$text = str_replace(array("\r\n", "\n", "\r"), '<br>', $text);
}
return $text;
}
public function cleanText($text, $removeEOL = 1)
{
// Plan.io Task #3953
if (empty($text)) return $text;
$text = str_replace('"', "'", $text);
$text = str_replace('œ', 'oe', $text);
$text = str_replace('’', "'", $text);
$text = preg_replace('/[\t]/', ' ', trim($text));
if($removeEOL == 1)
{
$text = str_replace(array("\r\n", "\n", "\r"), ' ', $text);
}
else if($removeEOL == 2)
{
$text = str_replace(array("\r\n", "\n", "\r"), '<br>', $text);
}
return htmlentities($text);
}
public function strtoupperFr($string)
{
// Plan.io Task #3953
if (empty($string)) return $string;
$string = strtoupper($string);
$string = str_replace(
array('é', 'è', 'ê', 'ë', 'à', 'â', 'î', 'ï', 'ô', 'ù', 'û'),
array('É', 'È', 'Ê', 'Ë', 'À', 'Â', 'Î', 'Ï', 'Ô', 'Ù', 'Û'),
$string
);
return $string;
}
public function removeWhitespaces($string)
{
// Plan.io Task #3953
if (empty($string)) return $string;
$string = preg_replace('/\s+/', '', $string);
return $string;
}
public function cropText($text, $maxNbChar)
{
// Plan.io Task #3953
if (empty($text)) return $text;
// If title si too long, crop it
if(strlen($text) > $maxNbChar-3)
{
return mb_substr($text,0,$maxNbChar, 'UTF-8') . "...";
}
return $text;
}
public function encode_utf8($string, $encode = null)
{
$fromEncoding = mb_detect_encoding($string);
if ($fromEncoding === false)
{
return $string;
}
if ($encode === null)
{
$encode = 'UTF-8';
}
try
{
$stringEncoded = mb_convert_encoding($string, $encode, $fromEncoding);
}
catch(\Exception $e)
{
return $string;
}
if ($stringEncoded === false)
{
return $string;
}
return $stringEncoded;
}
public function removeNonAlphaNumeric($string)
{
$string = preg_replace('/[^a-z\d ]/i', '', $string);
return $string;
}
public function craftSimpleNameForZone(SocietyGroup $societyGroup, Zone $zone)
{
$display = $this->removeNonAlphaNumeric($societyGroup->getRef());
$display = $this->camelToSnakeCase($display);
$societyGroupDisplay = $societyGroup->getId()."_".$display;
$display = $zone->getDisplay();
$display = $this->removeNonAlphaNumeric($display);
$display = $this->camelToSnakeCase($display);
$zoneDisplay = $zone->getId()."_".$display;
return $societyGroupDisplay."_".$zoneDisplay;
}
public function toUpperASCII( $str )
{
return strtoupper(strtr(utf8_decode($str),
utf8_decode(
'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ'),
'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy'));
}
public function parseEmailConfigData($string)
{
$string = preg_replace('/[^a-z_]/', '', $string);
return $string;
}
}