src/WebBundle/Helper/FilterHelper.php line 150

Open in your IDE?
  1. <?php
  2. namespace WebBundle\Helper;
  3. use AdmBundle\Helper\StrAdm;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\EntityNotFoundException;
  7. use Exception;
  8. use FlexApp\Entity\PriceEntity;
  9. use FlexApp\Interfaces\HasCodeInterface;
  10. use FlexApp\Interfaces\LocaleInterface;
  11. use WebBundle\Repository\FilterRepository;
  12. use WebBundle\Service\FiltersService;
  13. use WebBundle\WebBundle;
  14. /**
  15.  * Class FilterHelper
  16.  * @package WebBundle\Helper
  17.  */
  18. class FilterHelper
  19. {
  20.     private static $arr = [];
  21.     /**
  22.      * ЗАПАТКА
  23.      * Хелпер для сфинкса. Для получения адекватных значений фильтрации по цене. Ну и логику вынес из цикла
  24.      * @param $val
  25.      * @param null $country
  26.      * @return mixed
  27.      * @throws Exception
  28.      */
  29.     public static function getCost($val$country null)
  30.     {
  31.         $arr = [
  32.             '10' => [
  33.                 'en' => 10,
  34.                 'pl' => 43,
  35.                 'ru' => 2000,
  36.             ],
  37.             '20' => [
  38.                 'en' => 20,
  39.                 'pl' => 85.8,
  40.                 'ru' => 3000,
  41.             ],
  42.             '30' => [
  43.                 'en' => 30,
  44.                 'pl' => 129,
  45.                 'ru' => 6000,
  46.             ],
  47.         ];
  48.         $country $country $country App::getCurCountry();
  49.         if (!empty($arr[$val][$country])) {
  50.             $res $arr[$val][$country];
  51.         } else {
  52.             $res $arr[$val]['en'];
  53.         }
  54.         return $res;
  55.     }
  56.     /**
  57.      * Получение части строки запроса фильтрации , только KEY
  58.      * @param null $papam
  59.      * @param null $locale
  60.      * @return array|mixed|null
  61.      * @throws Exception
  62.      */
  63.     public static function getWord($papam null$locale null)
  64.     {
  65.         return static::get($papam$locale'word');
  66.     }
  67.     /**
  68.      * Получение полного адреса фильтра
  69.      * @param null $papam
  70.      * @param null $locale
  71.      * @param null $_locale_replace указываем, если хотим, что бы URL был выдан в нужной нам локали
  72.      * @return string
  73.      * @throws Exception
  74.      */
  75.     public static function getUrl($papam null$locale null$_locale_replace null)
  76.     {
  77.         $url = static::get($papam$locale);
  78.         if ($_locale_replace) {
  79.             if (preg_match("~\/{$locale}$~iu"$url)) {
  80.                 $url preg_replace("~\/{$locale}$~iu""/{$_locale_replace}"$url, -1);
  81.             }
  82.             if (preg_match("~\/{$locale}\/~iu"$url)) {
  83.                 $url preg_replace("~\/{$locale}\/~iu""/{$_locale_replace}/"$url, -1);
  84.             }
  85.         }
  86.         return $url;
  87.     }
  88.     /**
  89.      * @param $name
  90.      * @param null $limit
  91.      * @return mixed|string
  92.      */
  93.     public static function normaliseAltName($name$limit null)
  94.     {
  95.         return StrAdm::normaliseAltName($name$limit);
  96.     }
  97.     /**
  98.      * @param $url
  99.      * @param null $limit
  100.      * @return mixed|string
  101.      */
  102.     public static function normaliseUrl($url$limit null)
  103.     {
  104.         $url StrAdm::normaliseUrl($url$limit);
  105.         return $url;
  106.     }
  107.     /**
  108.      * Универсальный метод формирования строки фильтров
  109.      * @param null $papam
  110.      * @param null $locale
  111.      * @param string $type
  112.      * @return array|mixed|null
  113.      * @throws Exception
  114.      */
  115.     private static function get($papam null$locale nullstring $type 'url')
  116.     {
  117.         $locale = !$locale App::getCurLocale() : $locale;
  118.         $papam is_array($papam) ? array_diff($papam, ['']) : $papam;
  119.         $hash md5(json_encode($papam));
  120.         $curFS $papam false true;
  121.         $getType 'get' StrHelper::ucFirst($type);
  122.         if (!static::getArr($hash '.' $type)) {
  123.             // первым запросом инициируем FiltersService по указанному параметру
  124.             if (!static::getArr($hash '.FS')) {
  125.                 /** @var FiltersService $oFiltersService */
  126.                 $oFiltersService = static::getFS($hash$curFS);
  127.                 switch ($type) {
  128.                     case 'word':
  129.                         $oFiltersService->getWord($papam$locale);
  130.                         break;
  131.                     default:
  132.                         $oFiltersService->$getType($papam$locale);
  133.                 }
  134.             }
  135.             foreach (LocaleHelper::getListCodeAvailable() as $sLocale) {
  136.                 static::$arr[$hash][$type][$sLocale] = static::getFS($hash$curFS)->$getType(null$sLocale);
  137.             }
  138.         }
  139.         return ArrHelper::get(static::$arr"{$hash}.{$type}.{$locale}");
  140.     }
  141.     /**
  142.      * @param      $hash
  143.      * @param bool $cur
  144.      * @return FiltersService|array
  145.      */
  146.     private static function getFS($hash$cur false)
  147.     {
  148.         if (!static::getArr($hash '.FS')) {
  149.             if ($cur) {
  150.                 static::$arr[$hash]['FS'] = WebBundle::getContainer()->get('app.service.filters');
  151.             } else {
  152.                 static::$arr[$hash]['FS'] = new FiltersService();
  153.             }
  154.         }
  155.         return static::getArr($hash '.FS');
  156.     }
  157.     /**
  158.      * @param $val
  159.      * @return array
  160.      */
  161.     private static function getArr($val)
  162.     {
  163.         return ArrHelper::get(static::$arr$val);
  164.     }
  165.     /**
  166.      * @return object|FiltersService|null
  167.      */
  168.     private static function filterService()
  169.     {
  170.         return WebBundle::getContainer()->get('app.service.filters');
  171.     }
  172.     /**
  173.      * @param $param
  174.      * @return mixed|string
  175.      * @throws Exception
  176.      */
  177.     public static function checkPageUrl($param)
  178.     {
  179.         $url = static::get($param);
  180.         return explode('/'$url)[3] ?? '';
  181.     }
  182.     /**
  183.      * @param $leftMenu
  184.      * @return string|null
  185.      */
  186.     public static function getKeyByLeftMenu(string $leftMenu)
  187.     {
  188.         /** @var FilterRepository $oRepoFilter */
  189.         try {
  190.             $oRepoFilter App::getRepository('WebBundle:FilterEntity');
  191.         } catch (Exception $e) {
  192.             return null;
  193.         }
  194.         return $oRepoFilter->getKeyByLeftMenu($leftMenu);
  195.     }
  196.     public static function filterText (array $filters): string
  197.     {
  198.         $list = [];
  199.         $special = [];
  200.         foreach ($filters as $key => $filter) {
  201.             if ($key != 'type' && !empty($filter['list'])) {
  202.                 foreach ($filter['list'] as $row) {
  203.                     if (in_array($filter['id'], [422424])) {
  204.                         $special[$filter['nameFull']][] = $row['nameMany'];
  205.                     } else {
  206.                         $list[] = $row['nameMany'];
  207.                     }
  208.                 }
  209.             }
  210.         }
  211.         foreach ($special as $key => $row) {
  212.             $list[] = $key ' ' join(', '$row);
  213.         }
  214.         return join('; '$list);
  215.     }
  216.     /**
  217.      * @param Collection<mixed, LocaleInterface> $collection
  218.      * @return Collection<string, LocaleInterface>
  219.      */
  220.     public static function getSameCollectionWithKeysAsLocaleCode(Collection $collection): Collection
  221.     {
  222.         $newCollection = new ArrayCollection();
  223.         /** @var LocaleInterface $oneEntity */
  224.         foreach ($collection->toArray() as $oneEntity) {
  225.             try {
  226.                 $newCollection->set($oneEntity->getLocale()->getCode(), $oneEntity);
  227.             } catch (EntityNotFoundException $ex) {
  228.             }
  229.         }
  230.         return $newCollection;
  231.     }
  232.     public static function clearCollectionOfKeys(Collection $collection, array $keysForRemove): void
  233.     {
  234.         foreach ($keysForRemove as $key) {
  235.             $collection->remove($key);
  236.         }
  237.     }
  238.     /**
  239.      * Передаём сюда коллекцию с сущностями реализующими HasCodeInterface, возвращаем коллекцию, где ключи - это строка кода
  240.      *
  241.      * @param array<HasCodeInterface> $collection
  242.      * @return ArrayCollection
  243.      */
  244.     public static function getCollectionWhereKeysIsCodes(array $collection): ArrayCollection
  245.     {
  246.         return new ArrayCollection(array_combine(array_map(static fn(HasCodeInterface $item) => $item->getCode(), $collection), $collection));
  247.     }
  248.     public static function getHashForPriceEntity(PriceEntity $priceEntity): string
  249.     {
  250.         return $priceEntity->getSquare()->getCode() . $priceEntity->getCurrency()->getCode() . ($priceEntity->getCountry() ? $priceEntity->getCountry()->getCode() : '');
  251.     }
  252.     /**
  253.      * @param Collection<mixed, LocaleInterface> $collection
  254.      * @return array<string, int>
  255.      */
  256.     public static function getKeysOfCollectionWhereKeyIsLocaleCode(Collection $collection): array
  257.     {
  258.         $returnArray = [];
  259.         foreach ($collection->toArray() as $key => $oneElementCollection) {
  260.             try {
  261.                 $returnArray[$oneElementCollection->getLocale()->getCode()] = $key;
  262.             } catch (EntityNotFoundException $ex) {
  263.             }
  264.         }
  265.         return $returnArray;
  266.     }
  267. }