vendor/sulu/sulu/src/Sulu/Component/Content/Compat/Property.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 Sulu\Component\Content\Document\Structure\PropertyValue;
  12. use Sulu\Component\Util\ArrayableInterface;
  13. /**
  14.  * Property of Structure generated from Structure Manager to map a template.
  15.  */
  16. class Property implements PropertyInterface\JsonSerializable
  17. {
  18.     /**
  19.      * name of property.
  20.      *
  21.      * @var string
  22.      */
  23.     private $name;
  24.     /**
  25.      * @var Metadata
  26.      */
  27.     private $metadata;
  28.     /**
  29.      * is property mandatory.
  30.      *
  31.      * @var bool
  32.      */
  33.     private $mandatory;
  34.     /**
  35.      * is property multilingual.
  36.      *
  37.      * @var bool
  38.      */
  39.     private $multilingual;
  40.     /**
  41.      * min occurs of property value.
  42.      *
  43.      * @var int
  44.      */
  45.     private $minOccurs;
  46.     /**
  47.      * max occurs of property value.
  48.      *
  49.      * @var int
  50.      */
  51.     private $maxOccurs;
  52.     /**
  53.      * name of content type.
  54.      *
  55.      * @var string
  56.      */
  57.     private $contentTypeName;
  58.     /**
  59.      * parameter of property to merge with parameter of content type.
  60.      *
  61.      * @var array
  62.      */
  63.     private $params;
  64.     /**
  65.      * tags defined in xml.
  66.      *
  67.      * @var PropertyTag[]
  68.      */
  69.     private $tags;
  70.     /**
  71.      * column span.
  72.      *
  73.      * @var string
  74.      */
  75.     private $colSpan;
  76.     /**
  77.      * value of property.
  78.      *
  79.      * @var mixed
  80.      */
  81.     private $value;
  82.     /**
  83.      * @var StructureInterface
  84.      */
  85.     private $structure;
  86.     /**
  87.      * Constructor.
  88.      *
  89.      * @var PropertyValue
  90.      */
  91.     protected $propertyValue;
  92.     /**
  93.      * properties managed by this block.
  94.      *
  95.      * @var PropertyType[]
  96.      */
  97.     protected $types = [];
  98.     /**
  99.      * @var PropertyType[]
  100.      */
  101.     protected $properties = [];
  102.     /**
  103.      * @var string|null
  104.      */
  105.     protected $defaultTypeName;
  106.     public function __construct(
  107.         $name,
  108.         $metaData,
  109.         $contentTypeName,
  110.         $mandatory false,
  111.         $multilingual true,
  112.         $maxOccurs 1,
  113.         $minOccurs 1,
  114.         $params = [],
  115.         $tags = [],
  116.         $colSpan null,
  117.         $defaultTypeName null
  118.     ) {
  119.         $this->contentTypeName $contentTypeName;
  120.         $this->mandatory $mandatory;
  121.         $this->maxOccurs $maxOccurs;
  122.         $this->minOccurs $minOccurs;
  123.         $this->multilingual $multilingual;
  124.         $this->name $name;
  125.         $this->metadata = new Metadata($metaData);
  126.         $this->params $params;
  127.         $this->tags $tags;
  128.         $this->colSpan $colSpan;
  129.         $this->defaultTypeName $defaultTypeName;
  130.     }
  131.     public function setPropertyValue(PropertyValue $propertyValue)
  132.     {
  133.         $this->propertyValue $propertyValue;
  134.     }
  135.     /**
  136.      * returns name of template.
  137.      *
  138.      * @return string
  139.      */
  140.     public function getName()
  141.     {
  142.         return $this->name;
  143.     }
  144.     /**
  145.      * returns mandatory.
  146.      *
  147.      * @return bool
  148.      */
  149.     public function isMandatory()
  150.     {
  151.         return $this->mandatory;
  152.     }
  153.     /**
  154.      * returns multilingual.
  155.      *
  156.      * @return bool
  157.      */
  158.     public function isMultilingual()
  159.     {
  160.         return $this->multilingual;
  161.     }
  162.     /**
  163.      * return min occurs.
  164.      *
  165.      * @return int
  166.      */
  167.     public function getMinOccurs()
  168.     {
  169.         return $this->minOccurs;
  170.     }
  171.     /**
  172.      * return max occurs.
  173.      *
  174.      * @return int
  175.      */
  176.     public function getMaxOccurs()
  177.     {
  178.         return $this->maxOccurs;
  179.     }
  180.     /**
  181.      * returns field is mandatory.
  182.      *
  183.      * @return bool
  184.      */
  185.     public function getMandatory()
  186.     {
  187.         return $this->mandatory;
  188.     }
  189.     /**
  190.      * returns field is multilingual.
  191.      *
  192.      * @return bool
  193.      */
  194.     public function getMultilingual()
  195.     {
  196.         return $this->multilingual;
  197.     }
  198.     /**
  199.      * returns tags defined in xml.
  200.      *
  201.      * @return PropertyTag[]
  202.      */
  203.     public function getTags()
  204.     {
  205.         return $this->tags;
  206.     }
  207.     /**
  208.      * returns tag with given name.
  209.      *
  210.      * @param string $tagName
  211.      *
  212.      * @return PropertyTag
  213.      */
  214.     public function getTag($tagName)
  215.     {
  216.         return $this->tags[$tagName];
  217.     }
  218.     /**
  219.      * add a property tag.
  220.      *
  221.      * @return PropertyTag
  222.      */
  223.     public function addTag(PropertyTag $tag)
  224.     {
  225.         return $this->tags[$tag->getName()] = $tag;
  226.     }
  227.     /**
  228.      * return true if a tag with the given name exists.
  229.      *
  230.      * @return bool
  231.      */
  232.     public function hasTag($tagName)
  233.     {
  234.         return isset($this->tags[$tagName]);
  235.     }
  236.     /**
  237.      * returns column span.
  238.      *
  239.      * @return string
  240.      */
  241.     public function getColSpan()
  242.     {
  243.         return $this->colSpan;
  244.     }
  245.     /**
  246.      * returns title of property.
  247.      *
  248.      * @param string $languageCode
  249.      *
  250.      * @return string
  251.      */
  252.     public function getTitle($languageCode)
  253.     {
  254.         return $this->metadata->get('title'$languageCode\ucfirst($this->name));
  255.     }
  256.     /**
  257.      * returns infoText of property.
  258.      *
  259.      * @param string $languageCode
  260.      *
  261.      * @return string
  262.      */
  263.     public function getInfoText($languageCode)
  264.     {
  265.         return $this->metadata->get('info_text'$languageCode'');
  266.     }
  267.     /**
  268.      * returns placeholder of property.
  269.      *
  270.      * @param string $languageCode
  271.      *
  272.      * @return string
  273.      */
  274.     public function getPlaceholder($languageCode)
  275.     {
  276.         return $this->metadata->get('placeholder'$languageCode'');
  277.     }
  278.     /**
  279.      * sets the value from property.
  280.      */
  281.     public function setValue($value)
  282.     {
  283.         if ($this->propertyValue) {
  284.             $this->propertyValue->setValue($value);
  285.         }
  286.         $this->value $value;
  287.     }
  288.     public function setValueByReference(&$value)
  289.     {
  290.         $this->value $value;
  291.     }
  292.     /**
  293.      * gets the value from property.
  294.      */
  295.     public function getValue()
  296.     {
  297.         if ($this->propertyValue) {
  298.             return $this->propertyValue->getValue();
  299.         }
  300.         return $this->value;
  301.     }
  302.     /**
  303.      * returns name of content type.
  304.      *
  305.      * @return string
  306.      */
  307.     public function getContentTypeName()
  308.     {
  309.         return $this->contentTypeName;
  310.     }
  311.     /**
  312.      * parameter of property.
  313.      *
  314.      * @return array
  315.      */
  316.     public function getParams()
  317.     {
  318.         return $this->params;
  319.     }
  320.     /**
  321.      * returns TRUE if property is a block.
  322.      *
  323.      * @return bool
  324.      */
  325.     public function getIsBlock()
  326.     {
  327.         return false;
  328.     }
  329.     /**
  330.      * returns TRUE if property is multiple.
  331.      *
  332.      * @return bool
  333.      */
  334.     public function getIsMultiple()
  335.     {
  336.         $minOccurs $this->getMinOccurs();
  337.         $maxOccurs $this->getMaxOccurs();
  338.         if (\is_null($minOccurs) && \is_null($maxOccurs)) {
  339.             // if no occurs attributes are set it defaults to false
  340.             return false;
  341.         }
  342.         if (=== $minOccurs && === $maxOccurs) {
  343.             // this one allows to have an optional field
  344.             return true;
  345.         }
  346.         if (=== $minOccurs && === $maxOccurs) {
  347.             // if the occurences have a high and low limit of 1 it should be displayed as single
  348.             return false;
  349.         }
  350.         // if minOccurs is set a value of 1 is enough, because maxOccurs would be considered "unbound" when null
  351.         return $minOccurs >= || $maxOccurs 1;
  352.     }
  353.     /**
  354.      * @return Metadata
  355.      */
  356.     public function getMetadata()
  357.     {
  358.         return $this->metadata;
  359.     }
  360.     /**
  361.      * returns structure.
  362.      *
  363.      * @return StructureInterface
  364.      */
  365.     public function getStructure()
  366.     {
  367.         return $this->structure;
  368.     }
  369.     /**
  370.      * @param StructureInterface $structure
  371.      */
  372.     public function setStructure($structure)
  373.     {
  374.         $this->structure $structure;
  375.     }
  376.     /**
  377.      * magic getter for twig templates.
  378.      *
  379.      * @param string $property
  380.      */
  381.     public function __get($property)
  382.     {
  383.         if (\method_exists($this'get' \ucfirst($property))) {
  384.             return $this->{'get' \ucfirst($property)}();
  385.         } else {
  386.             return;
  387.         }
  388.     }
  389.     #[\ReturnTypeWillChange]
  390.     public function jsonSerialize()
  391.     {
  392.         $result = [
  393.             'name' => $this->getName(),
  394.             'metadata' => $this->getMetadata()->getData(),
  395.             'mandatory' => $this->getMandatory(),
  396.             'multilingual' => $this->getMultilingual(),
  397.             'minOccurs' => $this->getMinOccurs(),
  398.             'maxOccurs' => $this->getMaxOccurs(),
  399.             'contentTypeName' => $this->getContentTypeName(),
  400.             'params' => $this->getParams(),
  401.             'tags' => [],
  402.         ];
  403.         foreach ($this->getTags() as $tag) {
  404.             $result['tags'][] = [
  405.                 'name' => $tag->getName(),
  406.                 'priority' => $tag->getPriority(),
  407.             ];
  408.         }
  409.         return $result;
  410.     }
  411.     public function __clone()
  412.     {
  413.         $clone = new self(
  414.             $this->getName(),
  415.             $this->getMetadata(),
  416.             $this->getContentTypeName(),
  417.             $this->getMandatory(),
  418.             $this->getMultilingual(),
  419.             $this->getMaxOccurs(),
  420.             $this->getMinOccurs(),
  421.             $this->getParams(),
  422.             $this->getTags(),
  423.             $this->getColSpan(),
  424.             $this->getDefaultTypeName()
  425.         );
  426.         $clone->types = [];
  427.         foreach ($this->types as $type) {
  428.             $clone->addType(clone $type);
  429.         }
  430.         $clone->setValue($this->getValue());
  431.         return $clone;
  432.     }
  433.     public function toArray($depth null)
  434.     {
  435.         if ($this->getValue() instanceof ArrayableInterface) {
  436.             return $this->getValue()->toArray($depth);
  437.         } else {
  438.             return $this->getValue();
  439.         }
  440.     }
  441.     public function getTypes()
  442.     {
  443.         return $this->types;
  444.     }
  445.     public function addType($type)
  446.     {
  447.         $this->types[$type->getName()] = $type;
  448.     }
  449.     public function getType($name)
  450.     {
  451.         if (!$this->hasType($name)) {
  452.             throw new \InvalidArgumentException(
  453.                 \sprintf(
  454.                     'The block type "%s" has not been registered. Known block types are: [%s]',
  455.                     $name,
  456.                     \implode(', '\array_keys($this->types))
  457.                 )
  458.             );
  459.         }
  460.         return $this->types[$name];
  461.     }
  462.     public function hasType($name)
  463.     {
  464.         return isset($this->types[$name]);
  465.     }
  466.     public function getDefaultTypeName()
  467.     {
  468.         return $this->defaultTypeName;
  469.     }
  470.     /**
  471.      * returns child properties of given Type.
  472.      *
  473.      * @param string $typeName
  474.      *
  475.      * @return PropertyInterface[]
  476.      */
  477.     public function getTypeChildProperties($typeName)
  478.     {
  479.         return $this->getType($typeName)->getChildProperties();
  480.     }
  481.     public function initProperties($index$typeName)
  482.     {
  483.         $type $this->getType($typeName);
  484.         $this->properties[$index] = clone $type;
  485.         return $this->properties[$index];
  486.     }
  487.     public function clearProperties()
  488.     {
  489.         $this->properties = [];
  490.     }
  491.     public function getProperties($index)
  492.     {
  493.         if (!isset($this->properties[$index])) {
  494.             throw new \OutOfRangeException(\sprintf(
  495.                 'No properties at index "%s" in block "%s". Valid indexes: [%s]',
  496.                 $index$this->getName(), \implode(', '\array_keys($this->properties))
  497.             ));
  498.         }
  499.         return $this->properties[$index];
  500.     }
  501.     public function getLength()
  502.     {
  503.         return \count($this->properties);
  504.     }
  505. }