Utils.php 3.3KB

4 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Lc\ShopBundle\Services;
  3. use Cocur\Slugify\Slugify;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  6. use Lc\ShopBundle\Context\PageInterface;
  7. use Lc\ShopBundle\Context\TaxRateInterface;
  8. use Lc\ShopBundle\Context\UnitInterface;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Symfony\Component\HttpFoundation\ParameterBag;
  11. class Utils
  12. {
  13. protected $em ;
  14. protected $parameterBag ;
  15. public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag)
  16. {
  17. $this->em = $em ;
  18. $this->parameterBag = $parameterBag ;
  19. }
  20. // @todo : À supprimer et passer dans DateUtils (gérer du coup le cas du modèle DeliverySlot qui dépend de cette fonction)
  21. public static function getDayByNumber($number, $lang = 'fr')
  22. {
  23. if($lang == 'fr') {
  24. $daysArray = [
  25. 1 => 'Lundi',
  26. 2 => 'Mardi',
  27. 3 => 'Mercredi',
  28. 4 => 'Jeudi',
  29. 5 => 'Vendredi',
  30. 6 => 'Samedi',
  31. 7 => 'Dimanche'
  32. ] ;
  33. }
  34. else {
  35. $daysArray = [
  36. 1 => 'Monday',
  37. 2 => 'Tuesday',
  38. 3 => 'Wednesday',
  39. 4 => 'Thursday',
  40. 5 => 'Friday',
  41. 6 => 'Saturday',
  42. 7 => 'Sunday',
  43. ] ;
  44. }
  45. if(isset($daysArray[$number])) {
  46. return $daysArray[$number] ;
  47. }
  48. return '' ;
  49. }
  50. public function getElementByDevAlias($devAlias, $class = PageInterface::class)
  51. {
  52. $class = $this->em->getClassMetadata($class)->getName();
  53. return $this->em->getRepository($class)->findOneByDevAlias($devAlias) ;
  54. }
  55. public function isServerLocalhost()
  56. {
  57. return in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1']) ;
  58. }
  59. public function getCookieDomain()
  60. {
  61. return ($this->isServerLocalhost()) ? null : $this->parameterBag->get('app.cookie_domain_distant') ;
  62. }
  63. function limitText($text, $limit) {
  64. if (str_word_count($text, 0) > $limit) {
  65. $words = str_word_count($text, 2);
  66. $pos = array_keys($words);
  67. $text = substr($text, 0, $pos[$limit]) . '...';
  68. }
  69. return $text;
  70. }
  71. public function isBot()
  72. {
  73. if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])) {
  74. return TRUE;
  75. } else {
  76. return FALSE;
  77. }
  78. }
  79. public function slugify($string)
  80. {
  81. $slugify = new Slugify();
  82. return $slugify->slugify($string) ;
  83. }
  84. }