vendor/pimcore/pimcore/models/DataObject/Data/QuantityValue.php line 63

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model\DataObject\Data;
  15. use Pimcore\Localization\LocaleServiceInterface;
  16. use Pimcore\Model\DataObject\QuantityValue\Unit;
  17. use Pimcore\Model\DataObject\Traits\ObjectVarTrait;
  18. class QuantityValue extends AbstractQuantityValue
  19. {
  20.     use ObjectVarTrait;
  21.     /**
  22.      * @var float|int|null|string
  23.      */
  24.     protected $value;
  25.     /**
  26.      * @param float|int|string|null $value
  27.      * @param string|Unit|null $unit
  28.      */
  29.     public function __construct($value null$unit null)
  30.     {
  31.         $this->value $value;
  32.         parent::__construct($unit);
  33.     }
  34.     /**
  35.      * @param float|int|string|null $value
  36.      */
  37.     public function setValue($value)
  38.     {
  39.         $this->value $value;
  40.         $this->markMeDirty();
  41.     }
  42.     /**
  43.      * @return float|int|string|null
  44.      */
  45.     public function getValue()
  46.     {
  47.         return $this->value;
  48.     }
  49.     /**
  50.      * @return string
  51.      *
  52.      * @throws \Exception
  53.      */
  54.     public function __toString()
  55.     {
  56.         $value $this->getValue();
  57.         if (is_numeric($value)) {
  58.             $locale \Pimcore::getContainer()->get(LocaleServiceInterface::class)->findLocale();
  59.             if ($locale) {
  60.                 $formatter = new \NumberFormatter($locale\NumberFormatter::DECIMAL);
  61.                 $value $formatter->format($value);
  62.             }
  63.         }
  64.         if ($this->getUnit() instanceof Unit) {
  65.             $translator \Pimcore::getContainer()->get('translator');
  66.             $value .= ' ' $translator->trans($this->getUnit()->getAbbreviation(), [], 'admin');
  67.         }
  68.         return $value ? (string)$value '';
  69.     }
  70. }