packages/translatable-bundle/Entity/TranslatableValue.php line 149

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 11.10.2019
  5.  * Time: 14:59
  6.  */
  7. namespace AngelGamez\TranslatableBundle\Entity;
  8. use Symfony\Component\Intl\Intl;
  9. use Symfony\Component\Intl\Languages;
  10. /**
  11.  * @method TranslatableValue en(string $translation)
  12.  * @method TranslatableValue ru(string $translation)
  13.  */
  14. final class TranslatableValue implements \JsonSerializable\ArrayAccess
  15. {
  16.     /**
  17.      * @var string[]
  18.      */
  19.     protected $translations;
  20.     /**
  21.      * @var string[]
  22.      */
  23.     private static $locales;
  24.     /**
  25.      * TranslatableValue constructor.
  26.      *
  27.      * @param string[] $translations
  28.      */
  29.     public function __construct(array $translations = [])
  30.     {
  31.         foreach ($translations as $locale => $translation) {
  32.             $this->translations[$locale] = (string) $translation;
  33.         }
  34.     }
  35.     /**
  36.      * @param string $locale
  37.      * @param string $translation
  38.      *
  39.      * @return TranslatableValue
  40.      */
  41.     public function withTranslation(string $localestring $translation): self
  42.     {
  43.         $translations $this->translations;
  44.         $translations[$locale] = $translation;
  45.         return new static($translations);
  46.     }
  47.     /**
  48.      * @param string $locale
  49.      *
  50.      * @return bool
  51.      */
  52.     public function hasTranslation(string $locale): bool
  53.     {
  54.         return \array_key_exists($locale$this->translations);
  55.     }
  56.     /**
  57.      * @param string $locale
  58.      *
  59.      * @return string|null
  60.      */
  61.     public function getTranslation(string $locale): ?string
  62.     {
  63.         return $this->translations[$locale] ?? null;
  64.     }
  65.     /**
  66.      * @param TranslatableValue $other
  67.      *
  68.      * @return bool
  69.      */
  70.     public function equals(self $other): bool
  71.     {
  72.         return $this->translations === $other->translations;
  73.     }
  74.     /**
  75.      * @inheritDoc
  76.      */
  77.     public function __call($name$arguments)
  78.     {
  79.         if (!$this->isValidLocale($name)) {
  80.             throw new \DomainException(sprintf('%s is not a valid locale.'$name));
  81.         }
  82.         return $this->withTranslation($name$arguments[0]);
  83.     }
  84.     /**
  85.      * @inheritDoc
  86.      */
  87.     public function jsonSerialize()
  88.     {
  89.         return $this->translations;
  90.     }
  91.     /**
  92.      * @inheritDoc
  93.      */
  94.     public function __toString()
  95.     {
  96.         @trigger_error(sprintf('Casting translatable value to string, %s.'\json_encode($this->translations)), E_USER_WARNING);
  97.         if (empty($this->translations)) {
  98.             return '';
  99.         }
  100.         $defaultLocale 'ru';
  101.         $default $this->getTranslation($defaultLocale) ?? reset($this->translations);
  102.         return $default;
  103.     }
  104.     /**
  105.      * @inheritDoc
  106.      */
  107.     public function offsetExists($offset)
  108.     {
  109.         return $this->hasTranslation($offset);
  110.     }
  111.     /**
  112.      * @inheritDoc
  113.      */
  114.     public function offsetGet($offset)
  115.     {
  116.         return $this->getTranslation($offset);
  117.     }
  118.     /**
  119.      * @inheritDoc
  120.      */
  121.     public function offsetSet($offset$value)
  122.     {
  123.         throw new \LogicException(__METHOD__.' is not implemented.');
  124.     }
  125.     /**
  126.      * @inheritDoc
  127.      */
  128.     public function offsetUnset($offset)
  129.     {
  130.         throw new \LogicException(__METHOD__.' is not implemented.');
  131.     }
  132.     private function isValidLocale(string $locale): bool
  133.     {
  134.         if (null === self::$locales) {
  135.             self::$locales Languages::getLanguageCodes();
  136.         }
  137.         return \in_array($localeself::$localestrue);
  138.     }
  139. }