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