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

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