src/Service/MobileAppSerializeService.php line 999

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Dish;
  4. use App\Entity\DishCategory;
  5. use App\Entity\Wine;
  6. use App\Entity\WineCategory;
  7. use App\Entity\Beer;
  8. use App\Entity\BeerCategory;
  9. use App\Entity\Drink;
  10. use App\Entity\DrinkCategory;
  11. use App\Entity\Menu;
  12. use App\Entity\MenuCategory;
  13. use App\Entity\MenuInnerCategory;
  14. use App\Entity\ProductCategoryPosition;
  15. use App\Entity\Institution;
  16. use App\Entity\WinePairing;
  17. use App\Entity\BeerPairing;
  18. use App\Entity\Allergen;
  19. use App\Entity\Label;
  20. use App\Entity\Grape;
  21. use App\Entity\LocaleFront;
  22. use App\Entity\InstitutionLocaleFront;
  23. use App\Entity\InstitutionReview;
  24. use App\Entity\Contest;
  25. use App\Entity\ContestPrize;
  26. use App\Entity\ContestCompany;
  27. use App\Entity\ContestTicket;
  28. use App\Entity\Subscription;
  29. use Doctrine\ORM\EntityManagerInterface;
  30. use Doctrine\Common\Collections\Criteria;
  31. use Doctrine\Common\Collections\Collection;
  32. class MobileAppSerializeService
  33. {
  34.     private EntityManagerInterface $em;
  35.     private SerializeService $serializeService;
  36.     private $appLanguage;
  37.     public function __construct(
  38.         EntityManagerInterface $em,
  39.         SerializeService $serializeService
  40.     ) {
  41.         $this->em $em;
  42.         $this->serializeService $serializeService;
  43.         $this->appLanguage null;
  44.     }
  45.     /**
  46.      * @param string $language
  47.      * @return void
  48.      */
  49.     public function setAppLanguage(string $language): void
  50.     {
  51.         $this->appLanguage $language;
  52.     }
  53.     /**
  54.      * @param string|null $language
  55.      * @return void
  56.      */
  57.     public function setLanguage(?string $language): void
  58.     {
  59.         $requestedLocaleFront $this->em->getRepository(LocaleFront::class)->findBy(array('tag' => $language));
  60.         if ($requestedLocaleFront) {
  61.             $this->setAppLanguage($requestedLocaleFront[0]->getTag());
  62.         }
  63.     }
  64.     /**
  65.      * @param Collection $translations
  66.      * @return object|null $translation
  67.      */
  68.     public function getTranslation(Collection $translations): object|null
  69.     {
  70.         $currentLocale $this->em->getRepository(LocaleFront::class)->findBy(array('tag' => $this->appLanguage))[0];
  71.         $criteria Criteria::create()
  72.             ->where(Criteria::expr()->eq('localeFront'$currentLocale));
  73.         return $translations->matching($criteria)->first();
  74.     }
  75.     /**
  76.      * @param LocaleFront|null $localeFront
  77.      * @param Collection|InstitutionLocaleFront[] $institutionLocaleFronts
  78.      * @return array|null
  79.      */
  80.     public function serializeLanguage(?LocaleFront $localeFrontCollection $institutionLocaleFronts)
  81.     {
  82.         if (!$localeFront) {
  83.             return null;
  84.         }
  85.         $isDefault false;
  86.         $validated false;
  87.         foreach ($institutionLocaleFronts as $institutionLocaleFront) {
  88.             if ($institutionLocaleFront->getLocaleFront()->getId() == $localeFront->getId()) {
  89.                 $validated true;
  90.                 if ($institutionLocaleFront->getIsDefault()) {
  91.                     $isDefault true;
  92.                 }
  93.             }
  94.         }
  95.         return [
  96.             'name' => $localeFront->getName(),
  97.             'original_name' => $localeFront->getOriginalName(),
  98.             'tag' => $localeFront->getTag(),
  99.             'isDefault' => $isDefault,
  100.             'validated' => $validated,
  101.         ];
  102.     }
  103.     /**
  104.      * @param Dish|null $dish
  105.      * @return array|null
  106.      */
  107.     public function serializeDishOverview(?Dish $dish)
  108.     {
  109.         if (!$dish) {
  110.             return null;
  111.         }
  112.         if ($dish->isActive()) {
  113.             $dishTranslations $dish->getDishTranslations();
  114.             $document $this->serializeService->serializeDocument($dish->getImage(), true, [
  115.                 ['width' => 1000'height' => 1000],
  116.             ]);
  117.             $document['src'] = strstr($document['thumbnails']['1000_1000'], '/media');
  118.             $allergens = [];
  119.             foreach ($dish->getAllergens() as $allergen) {
  120.                 $allergens[] = $allergen->getId();
  121.             }
  122.             $verifiedReducedPrice Tools::getVerifiedReducedPrice(
  123.                 $dish->getReducedPrice(),
  124.                 $dish->getReducedPriceStart(),
  125.                 $dish->getReducedPriceEnd()
  126.             );
  127.             return [
  128.                 'id' => $dish->getId(),
  129.                 'type' => "DISH",
  130.                 'name' => $this->getTranslation($dishTranslations)->getName(),
  131.                 'price' => $dish->getSellingPriceTtc(),
  132.                 'reducedPrice' => $verifiedReducedPrice,
  133.                 'description' => $this->getTranslation($dishTranslations)->getDescription(),
  134.                 'category' => $this->getTranslation($dish->getDishCategory()->getDishCategoryTranslations())->getName(),
  135.                 'image' => $document,
  136.                 'allergens' => $allergens,
  137.             ];
  138.         }
  139.     }
  140.     /**
  141.      * @param Wine|null $wine
  142.      * @return array|null
  143.      */
  144.     public function serializeWineOverview(?Wine $wine)
  145.     {
  146.         if (!$wine) {
  147.             return null;
  148.         }
  149.         if ($wine->isActive()) {
  150.             $wineTranslations $wine->getWineTranslations();
  151.             $appName null;
  152.             $name $this->getTranslation($wineTranslations)->getName();
  153.             $appelation $this->getTranslation($wineTranslations)->getAppellation();
  154.             !is_null($name) ? $appName $name ' - ' $appelation $appName $appelation;
  155.             $winePrices = [];
  156.             foreach ($wine->getWinePrices() as $winePrice) {
  157.                 $winePrices[] = $this->serializeService->serializeWinePrice($winePrice);
  158.             }
  159.             $wineColor null;
  160.             if ($wine->getWineColor() !== null) {
  161.                 $wineColor = [
  162.                     'id' => $wine->getWineColor()->getId(),
  163.                     'name' => $this->getTranslation($wine->getWineColor()->getWineColorTranslations())->getName(),
  164.                 ];
  165.             }
  166.             $document $this->serializeService->serializeDocument($wine->getImage(), true, [
  167.                 ['width' => 1000'height' => 1000],
  168.             ]);
  169.             $document['src'] = strstr($document['thumbnails']['1000_1000'], '/media');
  170.             return [
  171.                 'id' => $wine->getId(),
  172.                 'type' => "WINE",
  173.                 'name' => $appName,
  174.                 'winery' => $this->getTranslation($wineTranslations)->getWinery(),
  175.                 'color' => $wineColor,
  176.                 'vintage' => $wine->getVintage(),
  177.                 'prices' => $winePrices,
  178.                 'image' => $document,
  179.             ];
  180.         }
  181.     }
  182.     /**
  183.      * @param Beer|null $beer
  184.      * @return array|null
  185.      */
  186.     public function serializeBeerOverview(?Beer $beer)
  187.     {
  188.         if (!$beer) {
  189.             return null;
  190.         }
  191.         if ($beer->isActive()) {
  192.             $beerTranslations $beer->getBeerTranslations();
  193.             $beerPrices = [];
  194.             foreach ($beer->getBeerPrices() as $beerPrice) {
  195.                 $beerPrices[] = $this->serializeService->serializeBeerPrice($beerPrice);
  196.             }
  197.             $beerColor null;
  198.             if ($beer->getBeerColor() !== null) {
  199.                 $beerColor = [
  200.                     'id' => $beer->getBeerColor()->getId(),
  201.                     'name' => $this->getTranslation($beer->getBeerColor()->getBeerColorTranslations())->getName(),
  202.                 ];
  203.             }
  204.             $document $this->serializeService->serializeDocument($beer->getImage(), true, [
  205.                 ['width' => 1000'height' => 1000],
  206.             ]);
  207.             $document['src'] = strstr($document['thumbnails']['1000_1000'], '/media');
  208.             return [
  209.                 'id' => $beer->getId(),
  210.                 'type' => "BEER",
  211.                 'name' => $this->getTranslation($beerTranslations)->getName(),
  212.                 'color' => $beerColor,
  213.                 'description' => $this->getTranslation($beerTranslations)->getDescription(),
  214.                 'prices' => $beerPrices,
  215.                 'image' => $document,
  216.             ];
  217.         }
  218.     }
  219.     /**
  220.      * @param Drink|null $drink
  221.      * @return array|null
  222.      */
  223.     public function serializeDrinkOverview(?Drink $drink)
  224.     {
  225.         if (!$drink) {
  226.             return null;
  227.         }
  228.         if ($drink->isActive()) {
  229.             $drinkTranslations $drink->getDrinkTranslations();
  230.             $drinkPrices = [];
  231.             foreach ($drink->getDrinkPrices() as $drinkPrice) {
  232.                 $drinkPrices[] = $this->serializeService->serializeDrinkPrice($drinkPrice);
  233.             }
  234.             $document $this->serializeService->serializeDocument($drink->getImage(), true, [
  235.                 ['width' => 1000'height' => 1000],
  236.             ]);
  237.             $document['src'] = strstr($document['thumbnails']['1000_1000'], '/media');
  238.             return [
  239.                 'id' => $drink->getId(),
  240.                 'type' => "DRINK",
  241.                 'name' => $this->getTranslation($drinkTranslations)->getName(),
  242.                 'description' => $this->getTranslation($drinkTranslations)->getDescription(),
  243.                 'prices' => $drinkPrices,
  244.                 'image' => $document,
  245.             ];
  246.         }
  247.     }
  248.     /**
  249.      * @param Menu|null $menu
  250.      * @return array|null
  251.      */
  252.     public function serializeMenuOverview(?Menu $menu)
  253.     {
  254.         if (!$menu) {
  255.             return null;
  256.         }
  257.         if ($menu->isActive()) {
  258.             $menuTranslations $menu->getMenuTranslations();
  259.             $menuInnerCategories = [];
  260.             foreach ($menu->getInnerCategories() as $innerCategory) {
  261.                 $serializedMenuInnerCategory $this->serializeMenuInnerCategoryOverview($innerCategory);
  262.                 if (count($serializedMenuInnerCategory['data']) > 0) {
  263.                     $menuInnerCategories[] = $serializedMenuInnerCategory;
  264.                 }
  265.             }
  266.             $document $this->serializeService->serializeDocument($menu->getImage(), true, [
  267.                 ['width' => 1000'height' => 1000],
  268.             ]);
  269.             $document['src'] = strstr($document['thumbnails']['1000_1000'], '/media');
  270.             $verifiedReducedPrice Tools::getVerifiedReducedPrice(
  271.                 $menu->getReducedPrice(),
  272.                 $menu->getReducedPriceStart(),
  273.                 $menu->getReducedPriceEnd()
  274.             );
  275.             return [
  276.                 'id' => $menu->getId(),
  277.                 'type' => "MENU",
  278.                 'name' => $this->getTranslation($menuTranslations)->getName(),
  279.                 'description' => $this->getTranslation($menuTranslations)->getDescription(),
  280.                 'price' => (!is_null($menu->getSellingPriceTtc()) ? $menu->getSellingPriceTtc() . '€' null),
  281.                 'reducedPrice' => (!is_null($verifiedReducedPrice) ? $verifiedReducedPrice '€' null),
  282.                 'products' => $menuInnerCategories,
  283.                 'image' => $document,
  284.             ];
  285.         }
  286.     }
  287.     /**
  288.      * @param MenuInnerCategory|null $menuInnerCategory
  289.      * @return array|null
  290.      */
  291.     public function serializeMenuInnerCategoryOverview(?MenuInnerCategory $menuInnerCategory)
  292.     {
  293.         if (!$menuInnerCategory) {
  294.             return null;
  295.         }
  296.         $list = [];
  297.         foreach ($menuInnerCategory->getMenuInnerCategoryDishes() as $menuInnerCategoryDish) {
  298.             $dish $menuInnerCategoryDish->getDish();
  299.             if ($dish && $dish->isActive()) {
  300.                 $list[] = [
  301.                     'id' => $dish->getId(),
  302.                     'type' => 'DISH',
  303.                     'position' => $menuInnerCategoryDish->getPosition(),
  304.                 ];
  305.             }
  306.         }
  307.         foreach ($menuInnerCategory->getMenuInnerCategoryWines() as $menuInnerCategoryWine) {
  308.             $wine $menuInnerCategoryWine->getWine();
  309.             if ($wine && $wine->isActive()) {
  310.                 $list[] = [
  311.                     'id' => $wine->getId(),
  312.                     'type' => 'WINE',
  313.                     'position' => $menuInnerCategoryWine->getPosition(),
  314.                 ];
  315.             }
  316.         }
  317.         foreach ($menuInnerCategory->getMenuInnerCategoryBeers() as $menuInnerCategoryBeer) {
  318.             $beer $menuInnerCategoryBeer->getBeer();
  319.             if ($beer && $beer->isActive()) {
  320.                 $list[] = [
  321.                     'id' => $beer->getId(),
  322.                     'type' => 'BEER',
  323.                     'position' => $menuInnerCategoryBeer->getPosition(),
  324.                 ];
  325.             }
  326.         }
  327.         foreach ($menuInnerCategory->getMenuInnerCategoryDrinks() as $menuInnerCategoryDrink) {
  328.             $drink $menuInnerCategoryDrink->getDrink();
  329.             if ($drink && $drink->isActive()) {
  330.                 $list[] = [
  331.                     'id' => $drink->getId(),
  332.                     'type' => 'DRINK',
  333.                     'position' => $menuInnerCategoryDrink->getPosition(),
  334.                 ];
  335.             }
  336.         }
  337.         //sort list by item position
  338.         usort($list, function ($item1$item2) {
  339.             return $item1['position'] <=> $item2['position'];
  340.         });
  341.         return [
  342.             'key' => $menuInnerCategory->getId(),
  343.             'title' => $this->getTranslation($menuInnerCategory->getMenuInnerCategoryTranslations())->getName(),
  344.             'data' => $list,
  345.         ];
  346.     }
  347.     /**
  348.      * @param MenuInnerCategory|null $menuInnerCategory
  349.      * @return array|null
  350.      */
  351.     public function serializeMenuInnerCategory(?MenuInnerCategory $menuInnerCategory)
  352.     {
  353.         if (!$menuInnerCategory) {
  354.             return null;
  355.         }
  356.         $list = [];
  357.         foreach ($menuInnerCategory->getMenuInnerCategoryDishes() as $menuInnerCategoryDish) {
  358.             $dish $menuInnerCategoryDish->getDish();
  359.             if ($dish && $dish->isActive()) {
  360.                 $arrayDish $this->serializeDishOverview($dish);
  361.                 $arrayDish['position'] = $menuInnerCategoryDish->getPosition();
  362.                 $list[] = $arrayDish;
  363.             }
  364.         }
  365.         foreach ($menuInnerCategory->getMenuInnerCategoryWines() as $menuInnerCategoryWine) {
  366.             $wine $menuInnerCategoryWine->getWine();
  367.             if ($wine && $wine->isActive()) {
  368.                 $arrayWine $this->serializeWineOverview($menuInnerCategoryWine->getWine());
  369.                 $arrayWine['position'] = $menuInnerCategoryWine->getPosition();
  370.                 $list[] = $arrayWine;
  371.             }
  372.         }
  373.         foreach ($menuInnerCategory->getMenuInnerCategoryBeers() as $menuInnerCategoryBeer) {
  374.             $beer $menuInnerCategoryBeer->getBeer();
  375.             if ($beer && $beer->isActive()) {
  376.                 $arrayBeer $this->serializeBeerOverview($menuInnerCategoryBeer->getBeer());
  377.                 $arrayBeer['position'] = $menuInnerCategoryBeer->getPosition();
  378.                 $list[] = $arrayBeer;
  379.             }
  380.         }
  381.         foreach ($menuInnerCategory->getMenuInnerCategoryDrinks() as $menuInnerCategoryDrink) {
  382.             $drink $menuInnerCategoryDrink->getDrink();
  383.             if ($drink && $drink->isActive()) {
  384.                 $arrayDrink $this->serializeDrinkOverview($menuInnerCategoryDrink->getDrink());
  385.                 $arrayDrink['position'] = $menuInnerCategoryDrink->getPosition();
  386.                 $list[] = $arrayDrink;
  387.             }
  388.         }
  389.         //sort list by item position
  390.         usort($list, function ($item1$item2) {
  391.             return $item1['position'] <=> $item2['position'];
  392.         });
  393.         return [
  394.             'key' => $menuInnerCategory->getId(),
  395.             'title' => $this->getTranslation($menuInnerCategory->getMenuInnerCategoryTranslations())->getName(),
  396.             'data' => $list,
  397.         ];
  398.     }
  399.     /**
  400.      * @param Dish|null $dish
  401.      * @param string $language
  402.      * @return array|null
  403.      */
  404.     public function serializeDish(?Dish $dish, ?string $language)
  405.     {
  406.         if (!$dish) {
  407.             return null;
  408.         }
  409.         // Set the language
  410.         $this->setLanguage($language);
  411.         if ($dish->isActive() && $this->appLanguage) {
  412.             $dishTranslations $dish->getDishTranslations();
  413.             $allergens = [];
  414.             foreach ($dish->getAllergens() as $allergen) {
  415.                 $allergens[] = $this->serializeAllergen($allergen);
  416.             }
  417.             $labels = [];
  418.             foreach ($dish->getLabels() as $label) {
  419.                 $labels[] = $this->serializeLabel($label);
  420.             }
  421.             $winePairings = [];
  422.             foreach ($dish->getWinePairings() as $winePairing) {
  423.                 $serializedWinePairing $this->serializeWinePairing($winePairingfalsetruenull);
  424.                 if (!is_null($serializedWinePairing)) {
  425.                     $winePairings[] = $serializedWinePairing;
  426.                 }
  427.             }
  428.             // Sort pairings from best to worst association
  429.             usort($winePairings, function ($item1$item2) {
  430.                 return $item1['value'] <= $item2['value'];
  431.             });
  432.             $beerPairings = [];
  433.             foreach ($dish->getBeerPairings() as $beerPairing) {
  434.                 $serializedBeerPairing $this->serializeBeerPairing($beerPairingfalsetruenull);
  435.                 if (!is_null($serializedBeerPairing)) {
  436.                     $beerPairings[] = $serializedBeerPairing;
  437.                 }
  438.             }
  439.             // Sort pairings from best to worst association
  440.             usort($beerPairings, function ($item1$item2) {
  441.                 return $item1['value'] <= $item2['value'];
  442.             });
  443.             $document $this->serializeService->serializeDocument($dish->getImage(), true, [
  444.                 ['width' => 1000'height' => 1000],
  445.             ]);
  446.             $document['src'] = strstr($document['thumbnails']['1000_1000'], '/media');
  447.             $verifiedReducedPrice Tools::getVerifiedReducedPrice(
  448.                 $dish->getReducedPrice(),
  449.                 $dish->getReducedPriceStart(),
  450.                 $dish->getReducedPriceEnd()
  451.             );
  452.             return [
  453.                 'id' => $dish->getId(),
  454.                 'type' => "DISH",
  455.                 'name' => $this->getTranslation($dishTranslations)->getName(),
  456.                 'price' => $dish->getSellingPriceTtc(),
  457.                 'reducedPrice' => $verifiedReducedPrice,
  458.                 'category' => $this->getTranslation($dish->getDishCategory()->getDishCategoryTranslations())->getName(),
  459.                 'description' => $this->getTranslation($dishTranslations)->getDescription(),
  460.                 'additionalInformation' => $this->getTranslation($dishTranslations)->getAdditionalInformation(),
  461.                 'ingredients' => [],
  462.                 'allergens' => $allergens,
  463.                 'labels' => $labels,
  464.                 'image' => $document,
  465.                 'winePairings' => $winePairings,
  466.                 'beerPairings' => $beerPairings,
  467.             ];
  468.         }
  469.     }
  470.     /**
  471.      * @param Wine|null $wine
  472.      * @param string $language
  473.      * @return array|null
  474.      */
  475.     public function serializeWine(?Wine $wine, ?string $language)
  476.     {
  477.         if (!$wine) {
  478.             return null;
  479.         }
  480.         // Set the language
  481.         $this->setLanguage($language);
  482.         if ($wine->isActive() && $this->appLanguage) {
  483.             $wineTranslations $wine->getWineTranslations();
  484.             $appName null;
  485.             $name $this->getTranslation($wineTranslations)->getName();
  486.             $appelation $this->getTranslation($wineTranslations)->getAppellation();
  487.             !is_null($name) ? $appName $name ' - ' $appelation $appName $appelation;
  488.             $grapes = [];
  489.             foreach ($wine->getGrapes() as $grape) {
  490.                 $grapes[] = $this->serializeGrape($grape);
  491.             }
  492.             $labels = [];
  493.             foreach ($wine->getLabels() as $label) {
  494.                 $labels[] = $this->serializeLabel($label);
  495.             }
  496.             $winePrices = [];
  497.             foreach ($wine->getWinePrices() as $winePrice) {
  498.                 $winePrices[] = $this->serializeService->serializeWinePrice($winePrice);
  499.             }
  500.             $winePairings = [];
  501.             foreach ($wine->getWinePairings() as $winePairing) {
  502.                 $serializedWinePairing $this->serializeWinePairing($winePairingtruefalsenull);
  503.                 if (!is_null($serializedWinePairing)) {
  504.                     $winePairings[] = $serializedWinePairing;
  505.                 }
  506.             }
  507.             // Sort pairings from best to worst association
  508.             usort($winePairings, function ($item1$item2) {
  509.                 return $item1['value'] <= $item2['value'];
  510.             });
  511.             $wineColor null;
  512.             if ($wine->getWineColor() !== null) {
  513.                 $wineColor = [
  514.                     'id' => $wine->getWineColor()->getId(),
  515.                     'name' => $this->getTranslation($wine->getWineColor()->getWineColorTranslations())->getName(),
  516.                 ];
  517.             }
  518.             $document $this->serializeService->serializeDocument($wine->getImage(), true, [
  519.                 ['width' => 1000'height' => 1000],
  520.             ]);
  521.             $document['src'] = strstr($document['thumbnails']['1000_1000'], '/media');
  522.             return [
  523.                 'id' => $wine->getId(),
  524.                 'type' => 'WINE',
  525.                 'name' => $appName,
  526.                 'winery' => $this->getTranslation($wineTranslations)->getWinery(),
  527.                 'category' => $this->getTranslation($wine->getWineCategory()->getWineCategoryTranslations())->getName(),
  528.                 'grapes' => $grapes,
  529.                 'labels' => $labels,
  530.                 'color' => $wineColor,
  531.                 'vintage' => $wine->getVintage(),
  532.                 'description' => $this->getTranslation($wineTranslations)->getDescription(),
  533.                 'strength' => $wine->getStrength(),
  534.                 'sweetness' => $wine->getSweetness(),
  535.                 'tannins' => $wine->getTannins(),
  536.                 'fruity' => $wine->getFruity(),
  537.                 'video' => $wine->getVideo(),
  538.                 'review' => $this->getTranslation($wineTranslations)->getReview(),
  539.                 'reviewFile' => $this->serializeService->serializeDocument($wine->getReviewFile()),
  540.                 'image' => $document,
  541.                 'organic' => $wine->getOrganic(),
  542.                 'local' => $wine->getLocal(),
  543.                 'prices' => $winePrices,
  544.                 'winePairings' => $winePairings,
  545.             ];
  546.         }
  547.     }
  548.     /**
  549.      * @param Beer|null $beer
  550.      * @param string $language
  551.      * @return array|null
  552.      */
  553.     public function serializeBeer(?Beer $beer, ?string $language)
  554.     {
  555.         if (!$beer) {
  556.             return null;
  557.         }
  558.         // Set the language
  559.         $this->setLanguage($language);
  560.         if ($beer->isActive() && $this->appLanguage) {
  561.             $beerTranslations $beer->getBeerTranslations();
  562.             $labels = [];
  563.             foreach ($beer->getLabels() as $label) {
  564.                 $labels[] = $this->serializeLabel($label);
  565.             }
  566.             $beerPrices = [];
  567.             foreach ($beer->getBeerPrices() as $beerPrice) {
  568.                 $beerPrices[] = $this->serializeService->serializeBeerPrice($beerPrice);
  569.             }
  570.             $beerPairings = [];
  571.             foreach ($beer->getBeerPairings() as $beerPairing) {
  572.                 $serializedBeerPairing $this->serializeBeerPairing($beerPairingtruefalsenull);
  573.                 if (!is_null($serializedBeerPairing)) {
  574.                     $beerPairings[] = $serializedBeerPairing;
  575.                 }
  576.             }
  577.             // Sort pairings from best to worst association
  578.             usort($beerPairings, function ($item1$item2) {
  579.                 return $item1['value'] <= $item2['value'];
  580.             });
  581.             $beerColor null;
  582.             if ($beer->getBeerColor() !== null) {
  583.                 $beerColor = [
  584.                     'id' => $beer->getBeerColor()->getId(),
  585.                     'name' => $this->getTranslation($beer->getBeerColor()->getBeerColorTranslations())->getName(),
  586.                 ];
  587.             }
  588.             $document $this->serializeService->serializeDocument($beer->getImage(), true, [
  589.                 ['width' => 1000'height' => 1000],
  590.             ]);
  591.             $document['src'] = strstr($document['thumbnails']['1000_1000'], '/media');
  592.             return [
  593.                 'id' => $beer->getId(),
  594.                 'type' => 'BEER',
  595.                 'name' => $this->getTranslation($beerTranslations)->getName(),
  596.                 'category' => $this->getTranslation($beer->getBeerCategory()->getBeerCategoryTranslations())->getName(),
  597.                 'color' => $beerColor,
  598.                 'description' => $this->getTranslation($beerTranslations)->getDescription(),
  599.                 'review' => $this->getTranslation($beerTranslations)->getReview(),
  600.                 'reviewFile' => $this->serializeService->serializeDocument($beer->getReviewFile()),
  601.                 'image' => $document,
  602.                 'prices' => $beerPrices,
  603.                 'labels' => $labels,
  604.                 'beerPairings' => $beerPairings,
  605.             ];
  606.         }
  607.     }
  608.     /**
  609.      * @param Drink|null $drink
  610.      * @param string $language
  611.      * @return array|null
  612.      */
  613.     public function serializeDrink(?Drink $drink, ?string $language)
  614.     {
  615.         if (!$drink) {
  616.             return null;
  617.         }
  618.         // Set the language
  619.         $this->setLanguage($language);
  620.         if ($drink->isActive() && $this->appLanguage) {
  621.             $drinkTranslations $drink->getDrinkTranslations();
  622.             $labels = [];
  623.             foreach ($drink->getLabels() as $label) {
  624.                 $labels[] = $this->serializeLabel($label);
  625.             }
  626.             $drinkPrices = [];
  627.             foreach ($drink->getDrinkPrices() as $drinkPrice) {
  628.                 $drinkPrices[] = $this->serializeService->serializeDrinkPrice($drinkPrice);
  629.             }
  630.             $document $this->serializeService->serializeDocument($drink->getImage(), true, [
  631.                 ['width' => 1000'height' => 1000],
  632.             ]);
  633.             $document['src'] = strstr($document['thumbnails']['1000_1000'], '/media');
  634.             return [
  635.                 'id' => $drink->getId(),
  636.                 'type' => 'DRINK',
  637.                 'name' => $this->getTranslation($drinkTranslations)->getName(),
  638.                 'category' => $this->getTranslation($drink->getDrinkCategory()->getDrinkCategoryTranslations())->getName(),
  639.                 'description' => $this->getTranslation($drinkTranslations)->getDescription(),
  640.                 'image' => $document,
  641.                 'active' => $drink->isActive(),
  642.                 'prices' => $drinkPrices,
  643.                 'labels' => $labels,
  644.             ];
  645.         }
  646.     }
  647.     /**
  648.      * @param Menu|null $menu
  649.      * @param string $language
  650.      * @return array|null
  651.      */
  652.     public function serializeMenu(?Menu $menu, ?string $language)
  653.     {
  654.         if (!$menu) {
  655.             return null;
  656.         }
  657.         // Set the language
  658.         $this->setLanguage($language);
  659.         $menuInnerCategories = [];
  660.         foreach ($menu->getInnerCategories() as $innerCategory) {
  661.             $serializedMenuInnerCategory $this->serializeMenuInnerCategory($innerCategory);
  662.             if (count($serializedMenuInnerCategory['data']) > 0) {
  663.                 $menuInnerCategories[] = $serializedMenuInnerCategory;
  664.             }
  665.         }
  666.         $document $this->serializeService->serializeDocument($menu->getImage(), true, [
  667.             ['width' => 1000'height' => 1000],
  668.         ]);
  669.         $document['src'] = strstr($document['thumbnails']['1000_1000'], '/media');
  670.         $verifiedReducedPrice Tools::getVerifiedReducedPrice(
  671.             $menu->getReducedPrice(),
  672.             $menu->getReducedPriceStart(),
  673.             $menu->getReducedPriceEnd()
  674.         );
  675.         if ($menu->isActive() && $this->appLanguage) {
  676.             $menuTranslations $menu->getMenuTranslations();
  677.             return [
  678.                 'id' => $menu->getId(),
  679.                 'type' => "MENU",
  680.                 'name' => $this->getTranslation($menuTranslations)->getName(),
  681.                 'description' => $this->getTranslation($menuTranslations)->getDescription(),
  682.                 'price' => (!is_null($menu->getSellingPriceTtc()) ? $menu->getSellingPriceTtc() . '€' null),
  683.                 'reducedPrice' => (!is_null($verifiedReducedPrice) ? $verifiedReducedPrice '€' null),
  684.                 'products' => $menuInnerCategories,
  685.                 'image' => $document,
  686.             ];
  687.         }
  688.     }
  689.     /**
  690.      * @param Institution|null $institution
  691.      * @return array|null
  692.      */
  693.     public function serializeInstitution(?Institution $institution)
  694.     {
  695.         if (!$institution) {
  696.             return null;
  697.         }
  698.         $openingHours = [];
  699.         foreach ($institution->getOpeningHours() as $openingHour) {
  700.             $openingHours[] = $this->serializeService->serializeInstitutionOpeningHour($openingHour);
  701.         }
  702.         $document $this->serializeService->serializeDocument($institution->getLogo(), true, [
  703.             ['width' => 1000'height' => 1000],
  704.         ]);
  705.         $document['src'] = strstr($document['thumbnails']['1000_1000'], '/media');
  706.         $gallery = [];
  707.         foreach ($institution->getGallery() as $galleryImage) {
  708.             $serializedGalleryImage $this->serializeService->serializeDocument($galleryImagetrue, [
  709.                 ['width' => 1000'height' => 1000],
  710.             ]);
  711.             $serializedGalleryImage['src'] = strstr($serializedGalleryImage['thumbnails']['1000_1000'], '/media');
  712.             $gallery[] = $serializedGalleryImage;
  713.         }
  714.         return [
  715.             'id' => $institution->getId(),
  716.             'name' => $institution->getName(),
  717.             'url' => $institution->getUrl(),
  718.             'phone' => $institution->getPhone(),
  719.             'logo' => $document,
  720.             'facebook' => $institution->getFacebook(),
  721.             'instagram' => $institution->getInstagram(),
  722.             'description' => $institution->getDescription(),
  723.             'openingHours' => $openingHours,
  724.             'gallery' => $gallery,
  725.         ];
  726.     }
  727.     /**
  728.      * @param Institution|null $institution
  729.      * @param string $language
  730.      * @return array|null
  731.      */
  732.     public function serializeInstitutionOverview(?Institution $institution, ?string $language)
  733.     {
  734.         if (!$institution) {
  735.             return null;
  736.         }
  737.         // Set the language
  738.         $this->setLanguage($language);
  739.         if ($this->appLanguage) {
  740.             // Array of every products of the institution (dishes, wines, drinks, menus)
  741.             $products = [];
  742.             $sectionId = -1;
  743.             // Gather every products (dish, wine, beer, drink and menu) into one and only array
  744.             $productCategories = [];
  745.             // Only add non empty categories
  746.             foreach ($institution->getDishCategories() as $dishCategory) {
  747.                 if (count($dishCategory->getDishes()) > 0) {
  748.                     $productCategories[] = $dishCategory;
  749.                 }
  750.             }
  751.             foreach ($institution->getWineCategories() as $wineCategory) {
  752.                 if (count($wineCategory->getWines()) > 0) {
  753.                     $productCategories[] = $wineCategory;
  754.                 }
  755.             }
  756.             foreach ($institution->getBeerCategories() as $beerCategory) {
  757.                 if (count($beerCategory->getBeers()) > 0) {
  758.                     $productCategories[] = $beerCategory;
  759.                 }
  760.             }
  761.             foreach ($institution->getDrinkCategories() as $drinkCategory) {
  762.                 if (count($drinkCategory->getDrinks()) > 0) {
  763.                     $productCategories[] = $drinkCategory;
  764.                 }
  765.             }
  766.             foreach ($institution->getMenuCategories() as $menuCategory) {
  767.                 if (count($menuCategory->getMenus()) > 0) {
  768.                     $productCategories[] = $menuCategory;
  769.                 }
  770.             }
  771.             // Retrieves products category positions
  772.             $productCategoryPositions $institution->getProductCategoryPositions();
  773.             // Function that retrieves the position of a product category (based on its type)
  774.             // (N.B. position 0 is weirdly put at the end... + 1 to each of them easily solves the matter)
  775.             $getProductCategoryPosition = function($productCategory) use ($productCategoryPositions) {
  776.                 switch ($productCategory) {
  777.                     case ($productCategory instanceof DishCategory):
  778.                         foreach($productCategoryPositions as $productCategoryPosition) {
  779.                             if ('DISH-CATEGORY-' $productCategory->getId() == $productCategoryPosition->getProductCategory()) {
  780.                                 return $productCategoryPosition->getPosition() + 1;
  781.                             }
  782.                         }
  783.                         return null;
  784.                     case ($productCategory instanceof WineCategory):
  785.                         foreach($productCategoryPositions as $productCategoryPosition) {
  786.                             if ('WINE-CATEGORY-' $productCategory->getId() == $productCategoryPosition->getProductCategory()) {
  787.                                 return $productCategoryPosition->getPosition() + 1;
  788.                             }
  789.                         }
  790.                         return null;
  791.                     case ($productCategory instanceof BeerCategory):
  792.                         foreach($productCategoryPositions as $productCategoryPosition) {
  793.                             if ('BEER-CATEGORY-' $productCategory->getId() == $productCategoryPosition->getProductCategory()) {
  794.                                 return $productCategoryPosition->getPosition() + 1;
  795.                             }
  796.                         }
  797.                         return null;
  798.                     case ($productCategory instanceof DrinkCategory):
  799.                         foreach($productCategoryPositions as $productCategoryPosition) {
  800.                             if ('DRINK-CATEGORY-' $productCategory->getId() == $productCategoryPosition->getProductCategory()) {
  801.                                 return $productCategoryPosition->getPosition() + 1;
  802.                             }
  803.                         }
  804.                         return null;
  805.                     case ($productCategory instanceof MenuCategory):
  806.                         foreach($productCategoryPositions as $productCategoryPosition) {
  807.                             if ('MENU-CATEGORY-' $productCategory->getId() == $productCategoryPosition->getProductCategory()) {
  808.                                 return $productCategoryPosition->getPosition() + 1;
  809.                             }
  810.                         }
  811.                         return null;
  812.                 }
  813.             };
  814.             // Sorting function (each categories do not necessarely have a position)
  815.             $sortByProductCategoryPosition = function ($a$b) use ($getProductCategoryPosition) {
  816.                 $aPos $getProductCategoryPosition($a);
  817.                 $bPos $getProductCategoryPosition($b);
  818.                 if ($aPos && $bPos) {
  819.                     return $aPos $bPos;
  820.                 } else if ($aPos && $bPos == null) {
  821.                     return -1;
  822.                 } else if ($aPos == null && $bPos) {
  823.                     return 1;
  824.                 } else {
  825.                     return 0;
  826.                 }
  827.             };
  828.             usort($productCategories$sortByProductCategoryPosition);
  829.             // Function that retrieves the name of the product category (based on its type)
  830.             function getProductCategoryName($productCategory$globalContext) {
  831.                 switch ($productCategory) {
  832.                     case ($productCategory instanceof DishCategory):
  833.                         return $globalContext->getTranslation($productCategory->getDishCategoryTranslations())->getName();
  834.                     case ($productCategory instanceof WineCategory):
  835.                         return $globalContext->getTranslation($productCategory->getWineCategoryTranslations())->getName();
  836.                     case ($productCategory instanceof BeerCategory):
  837.                         return $globalContext->getTranslation($productCategory->getBeerCategoryTranslations())->getName();
  838.                     case ($productCategory instanceof DrinkCategory):
  839.                         return $globalContext->getTranslation($productCategory->getDrinkCategoryTranslations())->getName();
  840.                     case ($productCategory instanceof MenuCategory):
  841.                         return $globalContext->getTranslation($productCategory->getMenuCategoryTranslations())->getName();
  842.                 }
  843.             }
  844.             $imageOnEachHighlightedProduct true;
  845.             $serializedHighlightedProducts = [];
  846.             foreach ($productCategories as $productCategory) {
  847.                 $serializedProducts = [];
  848.                 $capacities = [];
  849.                 $sectionId++;
  850.                 switch ($productCategory) {
  851.                     case ($productCategory instanceof DishCategory):
  852.                         foreach ($productCategory->getDishes() as $dish) {
  853.                             $serializedProduct $this->serializeDish($dish$language);
  854.                             if (!is_null($serializedProduct)) {
  855.                                 $serializedProduct['sectionId'] = $sectionId;
  856.                                 $serializedProducts[] = $serializedProduct;
  857.                                 if ($dish->getHighlight()) {
  858.                                     $serializedProduct['type'] = "OFFER-DISH";
  859.                                     $serializedHighlightedProducts[] = $serializedProduct;
  860.                                     if (!isset($serializedProduct['image']) || is_null($serializedProduct['image']['id'])) {
  861.                                         $imageOnEachHighlightedProduct false;
  862.                                     }
  863.                                 }
  864.                             }
  865.                         }
  866.                         break;
  867.                     case ($productCategory instanceof WineCategory):
  868.                         foreach ($productCategory->getWines() as $wine) {
  869.                             foreach ($wine->getWinePrices() as $winePrice) {
  870.                                 $capacity $winePrice->getCapacity();
  871.                                 if (!is_null($capacity)) {
  872.                                     if (!isset($capacities[$capacity->getId()])) {
  873.                                         $capacities[$capacity->getId()] = $capacity->getName();
  874.                                     }
  875.                                 }
  876.                             }
  877.                             $serializedProduct $this->serializeWine($wine$language);
  878.                             if (!is_null($serializedProduct)) {
  879.                                 $serializedProduct['sectionId'] = $sectionId;
  880.                                 $serializedProducts[] = $serializedProduct;
  881.                                 if ($wine->getHighlight()) {
  882.                                     $serializedProduct['type'] = "OFFER-WINE";
  883.                                     $serializedHighlightedProducts[] = $serializedProduct;
  884.                                     if (!isset($serializedProduct['image']) || is_null($serializedProduct['image']['id'])) {
  885.                                         $imageOnEachHighlightedProduct false;
  886.                                     }
  887.                                 }
  888.                             }
  889.                         }
  890.                         break;
  891.                     case ($productCategory instanceof BeerCategory):
  892.                         foreach ($productCategory->getBeers() as $beer) {
  893.                             foreach ($beer->getBeerPrices() as $beerPrice) {
  894.                                 $capacity $beerPrice->getCapacity();
  895.                                 if (!is_null($capacity)) {
  896.                                     if (!isset($capacities[$capacity->getId()])) {
  897.                                         $capacities[$capacity->getId()] = $capacity->getName();
  898.                                     }
  899.                                 }
  900.                             }
  901.                             $serializedProduct $this->serializeBeer($beer$language);
  902.                             if (!is_null($serializedProduct)) {
  903.                                 $serializedProduct['sectionId'] = $sectionId;
  904.                                 $serializedProducts[] = $serializedProduct;
  905.                                 if ($beer->getHighlight()) {
  906.                                     $serializedProduct['type'] = "OFFER-BEER";
  907.                                     $serializedHighlightedProducts[] = $serializedProduct;
  908.                                     if (!isset($serializedProduct['image']) || is_null($serializedProduct['image']['id'])) {
  909.                                         $imageOnEachHighlightedProduct false;
  910.                                     }
  911.                                 }
  912.                             }
  913.                         }
  914.                         break;
  915.                     case ($productCategory instanceof DrinkCategory):
  916.                         foreach ($productCategory->getDrinks() as $drink) {
  917.                             foreach ($drink->getDrinkPrices() as $drinkPrice) {
  918.                                 $capacity $drinkPrice->getCapacity();
  919.                                 if (!is_null($capacity)) {
  920.                                     if (!isset($capacities[$capacity->getId()])) {
  921.                                         $capacities[$capacity->getId()] = $capacity->getName();
  922.                                     }
  923.                                 }
  924.                             }
  925.                             $serializedProduct $this->serializeDrink($drink$language);
  926.                             if (!is_null($serializedProduct)) {
  927.                                 $serializedProduct['sectionId'] = $sectionId;
  928.                                 $serializedProducts[] = $serializedProduct;
  929.                                 if ($drink->getHighlight()) {
  930.                                     $serializedProduct['type'] = "OFFER-DRINK";
  931.                                     $serializedHighlightedProducts[] = $serializedProduct;
  932.                                     if (!isset($serializedProduct['image']) || is_null($serializedProduct['image']['id'])) {
  933.                                         $imageOnEachHighlightedProduct false;
  934.                                     }
  935.                                 }
  936.                             }
  937.                         }
  938.                         break;
  939.                     case ($productCategory instanceof MenuCategory):
  940.                         foreach ($productCategory->getMenus() as $menu) {
  941.                             $serializedProduct $this->serializeMenuOverview($menu);
  942.                             if (!is_null($serializedProduct)) {
  943.                                 $serializedProduct['sectionId'] = $sectionId;
  944.                                 $serializedProducts[] = $serializedProduct;
  945.                                 if ($menu->getHighlight()) {
  946.                                     $serializedProduct['type'] = "OFFER-MENU";
  947.                                     $serializedHighlightedProducts[] = $serializedProduct;
  948.                                     if (!isset($serializedProduct['image']) || is_null($serializedProduct['image']['id'])) {
  949.                                         $imageOnEachHighlightedProduct false;
  950.                                     }
  951.                                 }
  952.                             }
  953.                         }
  954.                         break;
  955.                 }
  956.                 // Check if the serializedProducts is not empty before adding anything (each product can be deactivated)
  957.                 if (count($serializedProducts) > 0) {
  958.                     $product = [
  959.                         'id' => $sectionId,
  960.                         'type' => 'SECTION',
  961.                         'name' => getProductCategoryName($productCategory$this),
  962.                     ];
  963.                     if (count($capacities) > 0) {
  964.                         $product['capacities'] = $capacities;
  965.                     }
  966.                     $products[] = $product;
  967.                     array_push($products, ...$serializedProducts);
  968.                 } else {
  969.                     $sectionId--;
  970.                 }
  971.             }
  972.             if (count($serializedHighlightedProducts) > 0) {
  973.                 // Increments sections id (only when the institution has highlighted products)
  974.                 foreach($products as $key => $product){
  975.                     $products[$key]['type'] == 'SECTION' $products[$key]['id']++ : $products[$key]['sectionId']++;
  976.                 }
  977.                 // Different display depending on whether all the items have an image attached or not
  978.                 // Therefore, different data construction to avoid unecessary processing in the app
  979.                 if ($imageOnEachHighlightedProduct) {
  980.                     array_unshift($products, [
  981.                         'id' => 0,
  982.                         'type' => "OFFERS",
  983.                         'sectionId' => 0,
  984.                         'items' => $serializedHighlightedProducts,
  985.                     ]);
  986.                 } else {
  987.                     for ($i = (count($serializedHighlightedProducts) - 1); $i >= 0$i--) {
  988.                         $serializedHighlightedProducts[$i]['sectionId'] = 0;
  989.                         array_unshift($products$serializedHighlightedProducts[$i]);
  990.                     }
  991.                 }
  992.                 array_unshift($products, [
  993.                     'id' => 0,
  994.                     'type' => 'SECTION',
  995.                     'name' => 'Offres du jour',
  996.                 ]);
  997.             }
  998.             $document $this->serializeService->serializeDocument($institution->getLogo(), true, [
  999.                 ['width' => 1000'height' => 1000],
  1000.             ]);
  1001.             $document['src'] = strstr($document['thumbnails']['1000_1000'], '/media');
  1002.             // Allergens
  1003.             $institutionAllergens = [];
  1004.             $allergens $this->em->getRepository(Allergen::class)->findBy([], ['id' => 'ASC']);
  1005.             foreach ($allergens as $allergen) {
  1006.                 $institutionAllergens[] = $this->serializeAllergen($allergen);
  1007.             }
  1008.             // Languages
  1009.             $languages = [];
  1010.             $localeFronts $this->em->getRepository(LocaleFront::class)->findBy([], ['originalName' => 'ASC']);
  1011.             $institutionLocaleFronts $institution->getInstitutionLocaleFronts();
  1012.             foreach ($localeFronts as $localeFront) {
  1013.                 $languages[] = $this->serializeLanguage($localeFront$institutionLocaleFronts);
  1014.             }
  1015.             //functionalities
  1016.             $functionalities = [];
  1017.             $currentSubscription $institution->getUser()->getCurrentSubscription();
  1018.             //check if user got free trial premium sub
  1019.             $isFreePremiumTrialValid false;
  1020.             foreach ($institution->getUser()->getUserSubscriptions() as $subscription) {
  1021.                 if ($subscription->isFree() && $subscription->getFinishedAt() > new \DateTime()) {
  1022.                     $isFreePremiumTrialValid true;
  1023.                 }
  1024.             }
  1025.             //if user still has free premium trial valid : get premium subscription functionalities
  1026.             if ($isFreePremiumTrialValid) {
  1027.                 $freeSubscription $this->em->getRepository(Subscription::class)->findOneBy(['constName' => 'ADVANCED']);
  1028.                 if ($freeSubscription) {
  1029.                     $functionalities $freeSubscription->getFunctionalities();
  1030.                 }
  1031.             }
  1032.             //else get current subscription functionalities
  1033.             else if ($currentSubscription && $currentSubscription->isValid()) {
  1034.                 $functionalities $currentSubscription->getSubscription()->getFunctionalities();
  1035.             }
  1036.             return [
  1037.                 'id' => $institution->getId(),
  1038.                 'name' => $institution->getName(),
  1039.                 'logo' => $document,
  1040.                 'description' => $institution->getDescription(),
  1041.                 'products' => $products,
  1042.                 'allergens' => $institutionAllergens,
  1043.                 'languages' => $languages,
  1044.                 'functionalities' => $functionalities,
  1045.             ];
  1046.         }
  1047.     }
  1048.     /**
  1049.      * @param Institution|null $institution
  1050.      * @return array|null
  1051.      */
  1052.     public function serializeSupportedDataLanguages(Institution $institution)
  1053.     {
  1054.         $languages = [];
  1055.         $localeFronts $this->em->getRepository(LocaleFront::class)->findBy([], ['originalName' => 'ASC']);
  1056.         $institutionLocaleFronts $institution->getInstitutionLocaleFronts();
  1057.         foreach ($localeFronts as $localeFront) {
  1058.             $languages[] = $this->serializeLanguage($localeFront$institutionLocaleFronts);
  1059.         }
  1060.         return $languages;
  1061.     }
  1062.     /**
  1063.      * @param Institution|null $institution
  1064.      * @return array|null
  1065.      */
  1066.     public function serializeFunctionalities(Institution $institution)
  1067.     {
  1068.         $functionalities = [];
  1069.         $currentSubscription $institution->getUser()->getCurrentSubscription();
  1070.         //check if user got free trial premium sub
  1071.         $isFreePremiumTrialValid false;
  1072.         foreach ($institution->getUser()->getUserSubscriptions() as $subscription) {
  1073.             if ($subscription->isFree() && $subscription->getFinishedAt() > new \DateTime()) {
  1074.                 $isFreePremiumTrialValid true;
  1075.             }
  1076.         }
  1077.         //if user still has free premium trial valid : get premium subscription functionalities
  1078.         if ($isFreePremiumTrialValid) {
  1079.             $freeSubscription $this->em->getRepository(Subscription::class)->findOneBy(['constName' => 'ADVANCED']);
  1080.             if ($freeSubscription) {
  1081.                 $functionalities $freeSubscription->getFunctionalities();
  1082.             }
  1083.         }
  1084.         //else get current subscription functionalities
  1085.         else if ($currentSubscription && $currentSubscription->isValid()) {
  1086.             $functionalities $currentSubscription->getSubscription()->getFunctionalities();
  1087.         }
  1088.         return $functionalities;
  1089.     }
  1090.     /**
  1091.      * @param Institution|null $institution
  1092.      * @return array|null
  1093.      */
  1094.     public function serializeInstitutionPersonalization(?Institution $institution)
  1095.     {
  1096.         if (!$institution) {
  1097.             return null;
  1098.         }
  1099.         $institutionAppPersonalization $institution->getInstitutionAppPersonalization();
  1100.         if (!$institutionAppPersonalization) {
  1101.             return null;
  1102.         }
  1103.         return [
  1104.             'id' => $institutionAppPersonalization->getId(),
  1105.             'primaryColor' => $institutionAppPersonalization->getPrimaryColor(),
  1106.             'primaryVariantColor' => $institutionAppPersonalization->getPrimaryVariantColor(),
  1107.             'textOverPrimaryColor' => $institutionAppPersonalization->getTextOverPrimaryColor(),
  1108.         ];
  1109.     }
  1110.     /**
  1111.      * @param Allergen|null $allergen
  1112.      * @return array|null
  1113.      */
  1114.     public function serializeAllergen(?Allergen $allergen)
  1115.     {
  1116.         return [
  1117.             'id' => $allergen->getId(),
  1118.             'name' => $this->getTranslation($allergen->getAllergenTranslations())->getName(),
  1119.         ];
  1120.     }
  1121.     /**
  1122.      * @param Label|null $label
  1123.      * @return array|null
  1124.      */
  1125.     public function serializeLabel(?Label $label)
  1126.     {
  1127.         return [
  1128.             'id' => $label->getId(),
  1129.             'name' => $this->getTranslation($label->getLabelTranslations())->getName(),
  1130.         ];
  1131.     }
  1132.     /**
  1133.      * @param Grape|null $grape
  1134.      * @return array|null
  1135.      */
  1136.     public function serializeGrape(?Grape $grape)
  1137.     {
  1138.         if (!$grape) {
  1139.             return null;
  1140.         }
  1141.         return [
  1142.             'id' => $grape->getId(),
  1143.             'name' => $this->getTranslation($grape->getGrapeTranslations())->getName(),
  1144.         ];
  1145.     }
  1146.     /**
  1147.      * @param WinePairing $winePairing
  1148.      * @param bool|null $dish
  1149.      * @param bool|null $wine
  1150.      * @param bool|null $language
  1151.      * @return array|null
  1152.      */
  1153.     public function serializeWinePairing(WinePairing $winePairing, ?bool $dish, ?bool $wine, ?string $language)
  1154.     {
  1155.         if (!$winePairing) {
  1156.             return null;
  1157.         }
  1158.         // Set the language if not already defined
  1159.         if ($this->appLanguage == null) {
  1160.             $this->setLanguage($language);
  1161.         }
  1162.         if ($this->appLanguage) {
  1163.             $pairing = [
  1164.                 'id' => $winePairing->getId(),
  1165.                 'value' => $winePairing->getValue(),
  1166.             ];
  1167.             if (!is_null($dish) && $dish) {
  1168.                 $dish $this->em->getRepository(Dish::class)->find($winePairing->getDish()->getId());
  1169.                 if ($dish && $dish->isActive()) {
  1170.                     $pairing['dish'] = $this->serializeDishOverview($dish);
  1171.                 }
  1172.                 else $pairing null;
  1173.             }
  1174.             if (!is_null($wine) && $wine) {
  1175.                 $wine $this->em->getRepository(Wine::class)->find($winePairing->getWine()->getId());
  1176.                 if ($wine && $wine->isActive()) {
  1177.                     $pairing['wine'] = $this->serializeWineOverview($wine);
  1178.                 }
  1179.                 else $pairing null;
  1180.             }
  1181.             return $pairing;
  1182.         }
  1183.     }
  1184.     /**
  1185.      * @param BeerPairing $beerPairing
  1186.      * @param bool|null $dish
  1187.      * @param bool|null $beer
  1188.      * @param bool|null $language
  1189.      * @return array|null
  1190.      */
  1191.     public function serializeBeerPairing(BeerPairing $beerPairing, ?bool $dish, ?bool $beer, ?string $language)
  1192.     {
  1193.         if (!$beerPairing) {
  1194.             return null;
  1195.         }
  1196.         // Set the language if not already defined
  1197.         if ($this->appLanguage == null) {
  1198.             $this->setLanguage($language);
  1199.         }
  1200.         if ($this->appLanguage) {
  1201.             $pairing = [
  1202.                 'id' => $beerPairing->getId(),
  1203.                 'value' => $beerPairing->getValue(),
  1204.             ];
  1205.             if (!is_null($dish) && $dish) {
  1206.                 $dish $this->em->getRepository(Dish::class)->find($beerPairing->getDish()->getId());
  1207.                 if ($dish && $dish->isActive()) {
  1208.                     $pairing['dish'] = $this->serializeDishOverview($dish);
  1209.                 }
  1210.                 else $pairing null;
  1211.             }
  1212.             if (!is_null($beer) && $beer) {
  1213.                 $beer $this->em->getRepository(Beer::class)->find($beerPairing->getBeer()->getId());
  1214.                 if ($beer && $beer->isActive()) {
  1215.                     $pairing['beer'] = $this->serializeBeerOverview($beer);
  1216.                 }
  1217.                 else $pairing null;
  1218.             }
  1219.             return $pairing;
  1220.         }
  1221.     }
  1222.     /**
  1223.      * @param InstitutionReview|null $institutionReview
  1224.      * @return array|null
  1225.      */
  1226.     public function serializeInstitutionReview(InstitutionReview $institutionReview)
  1227.     {
  1228.         if (!$institutionReview) {
  1229.             return null;
  1230.         }
  1231.         return [
  1232.             'id' => $institutionReview->getId(),
  1233.             'institutionId' => $institutionReview->getInstitution()->getId(),
  1234.             'userAppId' => $institutionReview->getUserAppId(),
  1235.             'rating' => $institutionReview->getRating(),
  1236.             'comments' => $institutionReview->getComments(),
  1237.             'date' => $institutionReview->getDate(),
  1238.         ];
  1239.     }
  1240.     /**
  1241.      * @param ContestPrize|null $prize
  1242.      * @return array|null
  1243.      */
  1244.     public function serializePrize(?ContestPrize $prize)
  1245.     {
  1246.         if (!$prize) {
  1247.             return null;
  1248.         }
  1249.         return [
  1250.             'id' => $prize->getId(),
  1251.             'name' => $prize->getName(),
  1252.             'ticketNumber' => $prize->getTicketNumber(),
  1253.             'value' => $prize->getValue()
  1254.         ];
  1255.     }
  1256.     /**
  1257.      * @param ContestCompany|null $company
  1258.      * @return array|null
  1259.      */
  1260.     public function serializeCompany(?ContestCompany $company)
  1261.     {
  1262.         if (!$company) {
  1263.             return null;
  1264.         }
  1265.         return [
  1266.             'id' => $company->getId(),
  1267.             'status' => $company->getStatus(),
  1268.             'name' => $company->getName(),
  1269.             'shareCapital' => $company->getShareCapital(),
  1270.             'siren' => $company->getSiren(),
  1271.             'registerCity' => $company->getRegisterCity(),
  1272.             'address' => $company->getAddress(),
  1273.             'postalCode' => $company->getPostalCode(),
  1274.             'city' => $company->getCity(),
  1275.             'country' => $company->getCountry()
  1276.         ];
  1277.     }
  1278.     /**
  1279.      * @param Contest|null $contest
  1280.      * @return array|null
  1281.      */
  1282.     public function serializeContest(?Contest $contest)
  1283.     {
  1284.         if (!$contest) {
  1285.             return null;
  1286.         }
  1287.         $prizes = [];
  1288.         foreach ($contest->getContestPrizes() as $prize) {
  1289.             $prizes[] = $this->serializePrize($prize);
  1290.         }
  1291.         return [
  1292.             'id' => $contest->getId(),
  1293.             'startDate' => $contest->getStartDate()->getTimestamp(),
  1294.             'endDate' => $contest->getEndDate()->getTimestamp(),
  1295.             'startTime' => $contest->getStartTime()->getTimestamp(),
  1296.             'endTime' => $contest->getEndTime()->getTimestamp(),
  1297.             'expirationDate' => $contest->getExpirationDate()->getTimestamp(),
  1298.             'entryLimitTime' => $contest->getEntryLimitTime(),
  1299.             'prizes' => $prizes,
  1300.             'automaticDistribution' => $contest->getAutomaticDistribution(),
  1301.             'company' => $this->serializeCompany($contest->getContestCompany())
  1302.         ];
  1303.     }
  1304.     /**
  1305.      * @param ContestTicket|null $contestTicket
  1306.      * @return array|null
  1307.      */
  1308.     public function serializeTicket(?ContestTicket $contestTicket)
  1309.     {
  1310.         if (!$contestTicket) {
  1311.             return null;
  1312.         }
  1313.         $now = new \DateTime('now');
  1314.         $expirationDate $contestTicket->getContest()->getExpirationDate();
  1315.         if ($now $expirationDate) {
  1316.             $isExpired true;
  1317.         }
  1318.         else {
  1319.             $isExpired false;
  1320.         }
  1321.         return [
  1322.             'id' => $contestTicket->getId(),
  1323.             'hash' => $contestTicket->getHash(),
  1324.             'isClaimed' => $contestTicket->getIsClaimed(),
  1325.             'wonAt' => $contestTicket->getWonAt() ? $contestTicket->getWonAt()->getTimestamp() : null,
  1326.             'claimedAt' => $contestTicket->getClaimedAt() ? $contestTicket->getClaimedAt()->getTimestamp() : null,
  1327.             'expirationDate' => $contestTicket->getContest()->getExpirationDate()->getTimestamp(),
  1328.             'isExpired' => $isExpired,
  1329.             'prize' => $contestTicket->getContestPrize()->getName(),
  1330.             'description' => $contestTicket->getContestPrize()->getDescription(),
  1331.             'value' => $contestTicket->getContestPrize()->getValue(),
  1332.             'userAppId' => $contestTicket->getUserAppId(),
  1333.             'contestId' => $contestTicket->getContest()->getId(),
  1334.             'institutionId' => $contestTicket->getContest()->getInstitution()->getId(),
  1335.         ];
  1336.     }
  1337. }