vendor/sulu/sulu/src/Sulu/Component/Content/Compat/PropertyParameter.php line 20

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Component\Content\Compat;
  11. use JMS\Serializer\Annotation\Groups;
  12. use JMS\Serializer\Annotation\Type;
  13. /**
  14.  * Represents a parameter of a property.
  15.  */
  16. class PropertyParameter implements \JsonSerializable
  17. {
  18.     /**
  19.      * @var string
  20.      * @Type("string")
  21.      * @Groups({"frontend"})
  22.      */
  23.     private $name;
  24.     /**
  25.      * @var string|bool|array
  26.      * @Groups({"frontend"})
  27.      */
  28.     private $value;
  29.     /**
  30.      * @var string
  31.      * @Type("string")
  32.      */
  33.     private $type;
  34.     /**
  35.      * @var Metadata
  36.      * @Type("Sulu\Component\Content\Compat\Metadata")
  37.      */
  38.     private $metadata;
  39.     /**
  40.      * Constructor.
  41.      *
  42.      * @param string $name
  43.      * @param string|null $type
  44.      * @param string|bool|array $value
  45.      * @param array $metadata
  46.      */
  47.     public function __construct($name$value$type null$metadata = [])
  48.     {
  49.         $this->name $name;
  50.         $this->value $value;
  51.         $this->type $type;
  52.         $this->metadata = new Metadata($metadata);
  53.     }
  54.     /**
  55.      * Returns name of property param.
  56.      *
  57.      * @return string
  58.      */
  59.     public function getName()
  60.     {
  61.         return $this->name;
  62.     }
  63.     /**
  64.      * Returns value of property param.
  65.      *
  66.      * @return array|bool|string
  67.      */
  68.     public function getValue()
  69.     {
  70.         return $this->value;
  71.     }
  72.     /**
  73.      * Returns type of property param.
  74.      *
  75.      * @return string
  76.      */
  77.     public function getType()
  78.     {
  79.         return $this->type;
  80.     }
  81.     /**
  82.      * Returns title of property param.
  83.      *
  84.      * @param string $languageCode
  85.      *
  86.      * @return string
  87.      */
  88.     public function getTitle($languageCode)
  89.     {
  90.         return $this->metadata->get('title'$languageCode\ucfirst($this->name));
  91.     }
  92.     /**
  93.      * Returns TRUE if parameter has a localized title the given language.
  94.      *
  95.      * @param string $languageCode
  96.      *
  97.      * @return bool
  98.      */
  99.     public function hasTitle($languageCode)
  100.     {
  101.         return null !== $this->metadata->get('title'$languageCode);
  102.     }
  103.     /**
  104.      * Returns infoText of property param.
  105.      *
  106.      * @param string $languageCode
  107.      *
  108.      * @return string
  109.      */
  110.     public function getInfoText($languageCode)
  111.     {
  112.         return $this->metadata->get('info_text'$languageCode'');
  113.     }
  114.     /**
  115.      * Returns placeholder of property param.
  116.      *
  117.      * @param string $languageCode
  118.      *
  119.      * @return string
  120.      */
  121.     public function getPlaceholder($languageCode)
  122.     {
  123.         return $this->metadata->get('placeholder'$languageCode'');
  124.     }
  125.     public function __toString()
  126.     {
  127.         $value $this->getValue();
  128.         if (\is_string($value)) {
  129.             return $value;
  130.         } elseif (\is_bool($value)) {
  131.             return $value 'true' 'false';
  132.         } else {
  133.             return '';
  134.         }
  135.     }
  136.     #[\ReturnTypeWillChange]
  137.     public function jsonSerialize()
  138.     {
  139.         return [
  140.             'value' => $this->getValue(),
  141.             'type' => $this->getType(),
  142.         ];
  143.     }
  144. }