100 lines
3.2KB

  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. public static function getDayByNumber($number, $lang = 'fr')
  21. {
  22. if($lang == 'fr') {
  23. $daysArray = [
  24. 1 => 'Lundi',
  25. 2 => 'Mardi',
  26. 3 => 'Mercredi',
  27. 4 => 'Jeudi',
  28. 5 => 'Vendredi',
  29. 6 => 'Samedi',
  30. 7 => 'Dimanche'
  31. ] ;
  32. }
  33. else {
  34. $daysArray = [
  35. 1 => 'Monday',
  36. 2 => 'Tuesday',
  37. 3 => 'Wednesday',
  38. 4 => 'Thursday',
  39. 5 => 'Friday',
  40. 6 => 'Saturday',
  41. 7 => 'Sunday',
  42. ] ;
  43. }
  44. if(isset($daysArray[$number])) {
  45. return $daysArray[$number] ;
  46. }
  47. return '' ;
  48. }
  49. public function getElementByDevAlias($devAlias, $class = PageInterface::class)
  50. {
  51. $class = $this->em->getClassMetadata($class)->getName();
  52. return $this->em->getRepository($class)->findOneByDevAlias($devAlias) ;
  53. }
  54. public function isServerLocalhost()
  55. {
  56. return in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1']) ;
  57. }
  58. public function getCookieDomain()
  59. {
  60. return ($this->isServerLocalhost()) ? null : $this->parameterBag->get('app.cookie_domain_distant') ;
  61. }
  62. function limitText($text, $limit) {
  63. if (str_word_count($text, 0) > $limit) {
  64. $words = str_word_count($text, 2);
  65. $pos = array_keys($words);
  66. $text = substr($text, 0, $pos[$limit]) . '...';
  67. }
  68. return $text;
  69. }
  70. public function isBot()
  71. {
  72. if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])) {
  73. return TRUE;
  74. } else {
  75. return FALSE;
  76. }
  77. }
  78. public function slugify($string)
  79. {
  80. $slugify = new Slugify();
  81. return $slugify->slugify($string) ;
  82. }
  83. }