vendor/sulu/sulu/src/Sulu/Component/Content/Document/Structure/ManagedStructure.php line 58

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\Document\Structure;
  11. use PHPCR\NodeInterface;
  12. use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
  13. use Sulu\Component\Content\Compat\PropertyInterface;
  14. use Sulu\Component\Content\Compat\Structure\LegacyPropertyFactory;
  15. use Sulu\Component\Content\Compat\Structure\StructureBridge;
  16. use Sulu\Component\Content\ContentTypeManagerInterface;
  17. use Sulu\Component\Content\Metadata\StructureMetadata;
  18. /**
  19. * Lazy loading container for content properties.
  20. */
  21. class ManagedStructure extends Structure
  22. {
  23. /**
  24. * @var StructureMetadata
  25. */
  26. private $structureMetadata;
  27. /**
  28. * @var NodeInterface
  29. */
  30. private $node;
  31. /**
  32. * @var array<string, PropertyInterface>
  33. */
  34. private $legacyProperties = [];
  35. /**
  36. * @var array<string, PropertyValue>
  37. */
  38. private $propertyValues = [];
  39. /**
  40. * @param object $document
  41. */
  42. public function __construct(
  43. private ContentTypeManagerInterface $contentTypeManager,
  44. private LegacyPropertyFactory $legacyPropertyFactory,
  45. private DocumentInspector $inspector,
  46. private $document,
  47. ) {
  48. }
  49. public function getProperty($name)
  50. {
  51. $this->init();
  52. if (isset($this->properties[$name])) {
  53. return $this->properties[$name];
  54. }
  55. if (!$this->node) {
  56. $this->node = $this->inspector->getNode($this->document);
  57. }
  58. $structureProperty = $this->structureMetadata->getProperty($name);
  59. $contentTypeName = $structureProperty->getType();
  60. $bridge = new StructureBridge(
  61. $this->structureMetadata,
  62. $this->inspector,
  63. $this->legacyPropertyFactory,
  64. $this->document
  65. );
  66. if ($structureProperty->isLocalized()) {
  67. $locale = $this->inspector->getLocale($this->document);
  68. $property = $this->legacyPropertyFactory->createTranslatedProperty($structureProperty, $locale, $bridge);
  69. } else {
  70. $property = $this->legacyPropertyFactory->createProperty($structureProperty);
  71. }
  72. $this->legacyProperties[$name] = $property;
  73. $property->setStructure($bridge);
  74. $contentType = $this->contentTypeManager->get($contentTypeName);
  75. $contentType->read(
  76. $this->node,
  77. $property,
  78. $bridge->getWebspaceKey(),
  79. $bridge->getLanguageCode(),
  80. null
  81. );
  82. $valueProperty = new PropertyValue($name, $property->getValue());
  83. $this->properties[$name] = $valueProperty;
  84. return $valueProperty;
  85. }
  86. public function getContentViewProperty($name)
  87. {
  88. if (isset($this->propertyValues[$name])) {
  89. return $this->propertyValues[$name];
  90. }
  91. // initialize the legacy property
  92. $this->getProperty($name);
  93. $legacyProperty = $this->legacyProperties[$name];
  94. $structureProperty = $this->structureMetadata->getProperty($name);
  95. $contentTypeName = $structureProperty->getType();
  96. $contentType = $this->contentTypeManager->get($contentTypeName);
  97. $propertyValue = new PropertyValue(
  98. $name,
  99. $contentType->getContentData($legacyProperty)
  100. );
  101. $this->propertyValues[$name] = $propertyValue;
  102. return $propertyValue;
  103. }
  104. /**
  105. * Update the structure.
  106. */
  107. public function setStructureMetadata(StructureMetadata $structure)
  108. {
  109. $this->structureMetadata = $structure;
  110. }
  111. /**
  112. * Return an array copy of the property data.
  113. *
  114. * @return array
  115. */
  116. public function toArray()
  117. {
  118. $this->init();
  119. $values = [];
  120. foreach (\array_keys($this->structureMetadata->getProperties()) as $childName) {
  121. $values[$childName] = $this->normalize($this->getProperty($childName)->getValue());
  122. }
  123. return $values;
  124. }
  125. #[\ReturnTypeWillChange]
  126. public function offsetExists($offset)
  127. {
  128. $this->init();
  129. return $this->structureMetadata->hasProperty($offset);
  130. }
  131. public function bind($data, $clearMissing = true)
  132. {
  133. $this->init();
  134. foreach ($this->structureMetadata->getProperties() as $childName => $child) {
  135. if (false === $clearMissing && !\array_key_exists($childName, $data)) {
  136. continue;
  137. }
  138. $value = isset($data[$childName]) ? $data[$childName] : null;
  139. $property = $this->getProperty($childName);
  140. $property->setValue($value);
  141. }
  142. }
  143. private function init()
  144. {
  145. if (!$this->structureMetadata) {
  146. $this->structureMetadata = $this->inspector->getStructureMetadata($this->document);
  147. }
  148. }
  149. }