You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.9KB

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