Structure.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Page\Config;
  7. /**
  8. * Page config structure model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Structure
  14. {
  15. /**
  16. * Map of class properties.
  17. *
  18. * @var array
  19. */
  20. private $serializableProperties = [
  21. 'assets',
  22. 'removeAssets',
  23. 'title',
  24. 'metadata',
  25. 'elementAttributes',
  26. 'removeElementAttributes',
  27. 'bodyClasses',
  28. 'isBodyClassesDeleted',
  29. ];
  30. /**
  31. * Information assets elements on page
  32. *
  33. * @var array
  34. */
  35. protected $assets = [];
  36. /**
  37. * List asset which will be removed
  38. *
  39. * @var array
  40. */
  41. protected $removeAssets = [];
  42. /**
  43. * @var string
  44. */
  45. protected $title;
  46. /**
  47. * @var string[]
  48. */
  49. protected $metadata = [];
  50. /**
  51. * @var array
  52. */
  53. protected $elementAttributes = [];
  54. /**
  55. * @var array
  56. */
  57. protected $removeElementAttributes = [];
  58. /**
  59. * @var array
  60. */
  61. protected $bodyClasses = [];
  62. /**
  63. * @var bool
  64. */
  65. protected $isBodyClassesDeleted = false;
  66. /**
  67. * @param string $element
  68. * @param string $attributeName
  69. * @param string $attributeValue
  70. * @return $this
  71. */
  72. public function setElementAttribute($element, $attributeName, $attributeValue)
  73. {
  74. if (empty($attributeValue)) {
  75. $this->removeElementAttributes[$element][] = $attributeName;
  76. } else {
  77. $this->elementAttributes[$element][$attributeName] = (string)$attributeValue;
  78. }
  79. return $this;
  80. }
  81. /**
  82. * @return $this
  83. */
  84. public function processRemoveElementAttributes()
  85. {
  86. foreach ($this->removeElementAttributes as $element => $attributes) {
  87. foreach ($attributes as $attributeName) {
  88. unset($this->elementAttributes[$element][$attributeName]);
  89. }
  90. if (empty($this->elementAttributes[$element])) {
  91. unset($this->elementAttributes[$element]);
  92. }
  93. }
  94. $this->removeElementAttributes = [];
  95. return $this;
  96. }
  97. /**
  98. * @param string $value
  99. * @return $this
  100. */
  101. public function setBodyClass($value)
  102. {
  103. if (empty($value)) {
  104. $this->isBodyClassesDeleted = true;
  105. } else {
  106. $this->bodyClasses[] = $value;
  107. }
  108. return $this;
  109. }
  110. /**
  111. * @return array
  112. */
  113. public function getBodyClasses()
  114. {
  115. return $this->isBodyClassesDeleted ? [] : $this->bodyClasses;
  116. }
  117. /**
  118. * @return array
  119. */
  120. public function getElementAttributes()
  121. {
  122. return $this->elementAttributes;
  123. }
  124. /**
  125. * @param string $title
  126. * @return $this
  127. */
  128. public function setTitle($title)
  129. {
  130. $this->title = (string)$title;
  131. return $this;
  132. }
  133. /**
  134. * @return string
  135. */
  136. public function getTitle()
  137. {
  138. return $this->title;
  139. }
  140. /**
  141. * @param string $name
  142. * @param string $content
  143. * @return $this
  144. */
  145. public function setMetadata($name, $content)
  146. {
  147. $this->metadata[$name] = (string)$content;
  148. return $this;
  149. }
  150. /**
  151. * @return string[]
  152. */
  153. public function getMetadata()
  154. {
  155. return $this->metadata;
  156. }
  157. /**
  158. * @param string $name
  159. * @param array $attributes
  160. * @return $this
  161. */
  162. public function addAssets($name, $attributes)
  163. {
  164. $this->assets[$name] = $attributes;
  165. return $this;
  166. }
  167. /**
  168. * @param string $name
  169. * @return $this
  170. */
  171. public function removeAssets($name)
  172. {
  173. $this->removeAssets[$name] = $name;
  174. return $this;
  175. }
  176. /**
  177. * @return $this
  178. */
  179. public function processRemoveAssets()
  180. {
  181. $this->assets = array_diff_key($this->assets, $this->removeAssets);
  182. $this->removeAssets = [];
  183. return $this;
  184. }
  185. /**
  186. * @return array
  187. */
  188. public function getAssets()
  189. {
  190. return $this->assets;
  191. }
  192. /**
  193. * Reformat 'Page config structure' to array.
  194. *
  195. * @return array
  196. * @since 101.0.0
  197. */
  198. public function __toArray()
  199. {
  200. $result = [];
  201. foreach ($this->serializableProperties as $property) {
  202. $result[$property] = $this->{$property};
  203. }
  204. return $result;
  205. }
  206. /**
  207. * Update 'Page config structure' data.
  208. *
  209. * @param array $data
  210. * @return void
  211. * @since 101.0.0
  212. */
  213. public function populateWithArray(array $data)
  214. {
  215. foreach ($this->serializableProperties as $property) {
  216. $this->{$property} = $this->getArrayValueByKey($property, $data);
  217. }
  218. }
  219. /**
  220. * Get value from array by key.
  221. *
  222. * @param string $key
  223. * @param array $array
  224. * @return array
  225. */
  226. private function getArrayValueByKey($key, array $array)
  227. {
  228. return $array[$key] ?? [];
  229. }
  230. }