|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?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;
- }
-
- }
|