<?php namespace Lc\ShopBundle\Services; use Doctrine\ORM\EntityManagerInterface; use Lc\ShopBundle\Context\PageInterface; class Utils { protected $em ; protected $unitsArray = [ 'piece' => [ 'unit' => 'piece', 'wording_unit' => 'la pièce', 'wording' => 'pièce(s)', 'wording_short' => 'p.', 'coefficient' => 1, 'ref' => 'piece' ], 'g' => [ 'unit' => 'g', 'wording_unit' => 'le g', 'wording' => 'g', 'wording_short' => 'g', 'coefficient' => 1000, 'ref' => 'kg' ], 'kg' => [ 'unit' => 'kg', 'wording_unit' => 'le kg', 'wording' => 'kg', 'wording_short' => 'kg', 'coefficient' => 1, 'ref' => 'kg' ], 'mL' => [ 'unit' => 'mL', 'wording_unit' => 'le mL', 'wording' => 'mL', 'wording_short' => 'mL', 'coefficient' => 1000, 'ref' => 'L' ], 'cL' => [ 'unit' => 'cL', 'wording_unit' => 'le cL', 'wording' => 'cL', 'wording_short' => 'cL', 'coefficient' => 100, 'ref' => 'L' ], 'L' => [ 'unit' => 'L', 'wording_unit' => 'le litre', 'wording' => 'L', 'wording_short' => 'L', 'coefficient' => 1, 'ref' => 'L' ], ]; public function __construct(EntityManagerInterface $em) { $this->em = $em ; } public static function getDayByNumber($number, $lang = 'fr') { if($lang == 'fr') { $daysArray = [ 1 => 'Lundi', 2 => 'Mardi', 3 => 'Mercredi', 4 => 'Jeudi', 5 => 'Vendredi', 6 => 'Samedi', 7 => 'Dimanche' ] ; } else { $daysArray = [ 1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 7 => 'Sunday', ] ; } if(isset($daysArray[$number])) { return $daysArray[$number] ; } return '' ; } public function getUnitsListFormatedForType() { $choices = [] ; foreach ($this->unitsArray as $unit=>$unitInfo){ $choices[$unitInfo['wording_unit']] = $unit; }; return $choices; } public function getUnitsList() { return $this->unitsArray; } public function getUnitProperty($unit, $property) { $unitsArray = $this->getUnitsList() ; if(isset($unitsArray[$unit][$property])) { return $unitsArray[$unit][$property] ; } return false ; } public function getUnitWording($unit, $type = 'wording_short') { return $this->getUnitProperty($unit, $type) ; } public function getUnitRef($unit) { return $this->getUnitProperty($unit, 'ref') ; } public function getUnitCoefficient($unit) { return $this->getUnitProperty($unit, 'coefficient') ; } public function getElementByDevAlias($devAlias, $class = PageInterface::class) { $class = $this->em->getClassMetadata($class)->getName(); return $this->em->getRepository($class)->findOneByDevAlias($devAlias) ; } public function isServerLocalhost() { return in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1']) ; } function limitText($text, $limit) { if (str_word_count($text, 0) > $limit) { $words = str_word_count($text, 2); $pos = array_keys($words); $text = substr($text, 0, $pos[$limit]) . '...'; } return $text; } }