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.      *
  21.      * @Type("string")
  22.      * @Groups({"frontend"})
  23.      */
  24.     private $name;
  25.     /**
  26.      * @var string|bool|array
  27.      *
  28.      * @Groups({"frontend"})
  29.      */
  30.     private $value;
  31.     /**
  32.      * @var string
  33.      *
  34.      * @Type("string")
  35.      */
  36.     private $type;
  37.     /**
  38.      * @var Metadata
  39.      *
  40.      * @Type("Sulu\Component\Content\Compat\Metadata")
  41.      */
  42.     private $metadata;
  43.     /**
  44.      * Constructor.
  45.      *
  46.      * @param string $name
  47.      * @param string|null $type
  48.      * @param string|bool|array $value
  49.      * @param array $metadata
  50.      */
  51.     public function __construct($name$value$type null$metadata = [])
  52.     {
  53.         $this->name $name;
  54.         $this->value $value;
  55.         $this->type $type;
  56.         $this->metadata = new Metadata($metadata);
  57.     }
  58.     /**
  59.      * Returns name of property param.
  60.      *
  61.      * @return string
  62.      */
  63.     public function getName()
  64.     {
  65.         return $this->name;
  66.     }
  67.     /**
  68.      * Returns value of property param.
  69.      *
  70.      * @return array|bool|string
  71.      */
  72.     public function getValue()
  73.     {
  74.         return $this->value;
  75.     }
  76.     /**
  77.      * Returns type of property param.
  78.      *
  79.      * @return string
  80.      */
  81.     public function getType()
  82.     {
  83.         return $this->type;
  84.     }
  85.     /**
  86.      * Returns title of property param.
  87.      *
  88.      * @param string $languageCode
  89.      *
  90.      * @return string
  91.      */
  92.     public function getTitle($languageCode)
  93.     {
  94.         return $this->metadata->get('title'$languageCode, \ucfirst($this->name));
  95.     }
  96.     /**
  97.      * Returns TRUE if parameter has a localized title the given language.
  98.      *
  99.      * @param string $languageCode
  100.      *
  101.      * @return bool
  102.      */
  103.     public function hasTitle($languageCode)
  104.     {
  105.         return null !== $this->metadata->get('title'$languageCode);
  106.     }
  107.     /**
  108.      * Returns infoText of property param.
  109.      *
  110.      * @param string $languageCode
  111.      *
  112.      * @return string
  113.      */
  114.     public function getInfoText($languageCode)
  115.     {
  116.         return $this->metadata->get('info_text'$languageCode'');
  117.     }
  118.     /**
  119.      * Returns placeholder of property param.
  120.      *
  121.      * @param string $languageCode
  122.      *
  123.      * @return string
  124.      */
  125.     public function getPlaceholder($languageCode)
  126.     {
  127.         return $this->metadata->get('placeholder'$languageCode'');
  128.     }
  129.     public function __toString()
  130.     {
  131.         $value $this->getValue();
  132.         if (\is_string($value)) {
  133.             return $value;
  134.         } elseif (\is_bool($value)) {
  135.             return $value 'true' 'false';
  136.         } else {
  137.             return '';
  138.         }
  139.     }
  140.     #[\ReturnTypeWillChange]
  141.     public function jsonSerialize()
  142.     {
  143.         return [
  144.             'value' => $this->getValue(),
  145.             'type' => $this->getType(),
  146.         ];
  147.     }
  148. }