/models/DataObject/ClassDefinition/Layout.php

https://github.com/pimcore/pimcore · PHP · 481 lines · 200 code · 72 blank · 209 comment · 7 complexity · 4cb5857715fc2ebcad212cb29a9909b0 MD5 · raw file

  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * This source file is available under two different licenses:
  6. * - GNU General Public License version 3 (GPLv3)
  7. * - Pimcore Commercial License (PCL)
  8. * Full copyright and license information is available in
  9. * LICENSE.md which is distributed with this source code.
  10. *
  11. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12. * @license http://www.pimcore.org/license GPLv3 and PCL
  13. */
  14. namespace Pimcore\Model\DataObject\ClassDefinition;
  15. use Pimcore\Model;
  16. use Pimcore\Model\Element;
  17. class Layout implements Model\DataObject\ClassDefinition\Data\VarExporterInterface
  18. {
  19. use Model\DataObject\ClassDefinition\Helper\VarExport {
  20. __set_state as private _VarExport__set_state;
  21. }
  22. use Element\ChildsCompatibilityTrait;
  23. /**
  24. * @internal
  25. *
  26. * @var string
  27. */
  28. public $name;
  29. /**
  30. * @internal
  31. *
  32. * @var string
  33. */
  34. public $type;
  35. /**
  36. * @internal
  37. *
  38. * @var string
  39. */
  40. public $region;
  41. /**
  42. * @internal
  43. *
  44. * @var string
  45. */
  46. public $title;
  47. /**
  48. * @internal
  49. *
  50. * @var string|int
  51. */
  52. public $width = 0;
  53. /**
  54. * @internal
  55. *
  56. * @var string|int
  57. */
  58. public $height = 0;
  59. /**
  60. * @internal
  61. *
  62. * @var bool
  63. */
  64. public $collapsible = false;
  65. /**
  66. * @internal
  67. *
  68. * @var bool
  69. */
  70. public $collapsed = false;
  71. /**
  72. * @internal
  73. *
  74. * @var string
  75. */
  76. public $bodyStyle;
  77. /**
  78. * @internal
  79. *
  80. * @var string
  81. */
  82. public $datatype = 'layout';
  83. /**
  84. * @internal
  85. *
  86. * @var array
  87. */
  88. public $permissions;
  89. /**
  90. * @internal
  91. *
  92. * @var array
  93. */
  94. public $children = [];
  95. /**
  96. * @internal
  97. *
  98. * @var bool
  99. */
  100. public $locked = false;
  101. /**
  102. * @return string
  103. */
  104. public function getName()
  105. {
  106. return $this->name;
  107. }
  108. /**
  109. * @return string
  110. */
  111. public function getType()
  112. {
  113. return $this->type;
  114. }
  115. /**
  116. * @return string
  117. */
  118. public function getRegion()
  119. {
  120. return $this->region;
  121. }
  122. /**
  123. * @return string
  124. */
  125. public function getTitle()
  126. {
  127. return $this->title;
  128. }
  129. /**
  130. * @return int
  131. */
  132. public function getWidth()
  133. {
  134. return $this->width;
  135. }
  136. /**
  137. * @return int
  138. */
  139. public function getHeight()
  140. {
  141. return $this->height;
  142. }
  143. /**
  144. * @return bool
  145. */
  146. public function getCollapsible()
  147. {
  148. return $this->collapsible;
  149. }
  150. /**
  151. * @return array
  152. */
  153. public function getPermissions()
  154. {
  155. return $this->permissions;
  156. }
  157. /**
  158. * @param string $name
  159. *
  160. * @return $this
  161. */
  162. public function setName($name)
  163. {
  164. $this->name = $name;
  165. return $this;
  166. }
  167. /**
  168. * @param string $type
  169. *
  170. * @return $this
  171. */
  172. public function setType($type)
  173. {
  174. $this->type = $type;
  175. return $this;
  176. }
  177. /**
  178. * @param string $region
  179. *
  180. * @return $this
  181. */
  182. public function setRegion($region)
  183. {
  184. $this->region = $region;
  185. return $this;
  186. }
  187. /**
  188. * @param string $title
  189. *
  190. * @return $this
  191. */
  192. public function setTitle($title)
  193. {
  194. $this->title = $title;
  195. return $this;
  196. }
  197. /**
  198. * @param string|int $width
  199. *
  200. * @return $this
  201. */
  202. public function setWidth($width)
  203. {
  204. if (is_numeric($width)) {
  205. $width = (int)$width;
  206. }
  207. $this->width = $width;
  208. return $this;
  209. }
  210. /**
  211. * @param string|int $height
  212. *
  213. * @return $this
  214. */
  215. public function setHeight($height)
  216. {
  217. if (is_numeric($height)) {
  218. $height = (int)$height;
  219. }
  220. $this->height = $height;
  221. return $this;
  222. }
  223. /**
  224. * @param bool $collapsible
  225. *
  226. * @return $this
  227. */
  228. public function setCollapsible($collapsible)
  229. {
  230. $this->collapsible = (bool) $collapsible;
  231. $this->filterCollapsibleValue();
  232. return $this;
  233. }
  234. /**
  235. * @param array $permissions
  236. *
  237. * @return $this
  238. */
  239. public function setPermissions($permissions)
  240. {
  241. $this->permissions = $permissions;
  242. return $this;
  243. }
  244. /**
  245. * @return array
  246. */
  247. public function getChildren()
  248. {
  249. return $this->children;
  250. }
  251. /**
  252. * @internal
  253. *
  254. * @return array
  255. */
  256. public function &getChildrenByRef()
  257. {
  258. return $this->children;
  259. }
  260. /**
  261. * @param array $children
  262. *
  263. * @return $this
  264. */
  265. public function setChildren($children)
  266. {
  267. $this->children = $children;
  268. return $this;
  269. }
  270. /**
  271. * @return bool
  272. */
  273. public function hasChildren()
  274. {
  275. if (is_array($this->children) && count($this->children) > 0) {
  276. return true;
  277. }
  278. return false;
  279. }
  280. /**
  281. * @param Data|Layout $child
  282. */
  283. public function addChild($child)
  284. {
  285. $this->children[] = $child;
  286. }
  287. /**
  288. * @param array $data
  289. * @param array $blockedKeys
  290. *
  291. * @return $this
  292. */
  293. public function setValues($data = [], $blockedKeys = [])
  294. {
  295. foreach ($data as $key => $value) {
  296. if (!in_array($key, $blockedKeys)) {
  297. $method = 'set' . ucfirst($key);
  298. if (method_exists($this, $method)) {
  299. $this->$method($value);
  300. }
  301. }
  302. }
  303. return $this;
  304. }
  305. /**
  306. * @return string
  307. */
  308. public function getDatatype()
  309. {
  310. return $this->datatype;
  311. }
  312. /**
  313. * @param string $datatype
  314. *
  315. * @return $this
  316. */
  317. public function setDatatype($datatype)
  318. {
  319. $this->datatype = $datatype;
  320. return $this;
  321. }
  322. /**
  323. *
  324. * @return bool
  325. */
  326. public function getLocked()
  327. {
  328. return $this->locked;
  329. }
  330. /**
  331. * @param bool $locked
  332. *
  333. * @return $this
  334. */
  335. public function setLocked($locked)
  336. {
  337. $this->locked = (bool) $locked;
  338. return $this;
  339. }
  340. /**
  341. * @param bool $collapsed
  342. *
  343. * @return $this
  344. */
  345. public function setCollapsed($collapsed)
  346. {
  347. $this->collapsed = (bool) $collapsed;
  348. $this->filterCollapsibleValue();
  349. return $this;
  350. }
  351. /**
  352. * @return bool
  353. */
  354. public function getCollapsed()
  355. {
  356. return $this->collapsed;
  357. }
  358. /**
  359. * @param string $bodyStyle
  360. *
  361. * @return $this
  362. */
  363. public function setBodyStyle($bodyStyle)
  364. {
  365. $this->bodyStyle = $bodyStyle;
  366. return $this;
  367. }
  368. /**
  369. * @return string
  370. */
  371. public function getBodyStyle()
  372. {
  373. return $this->bodyStyle;
  374. }
  375. /**
  376. * @return Layout
  377. */
  378. protected function filterCollapsibleValue()
  379. {
  380. //if class definition set as collapsed the code below forces collapsible, issue: #778
  381. $this->collapsible = $this->getCollapsed() || $this->getCollapsible();
  382. return $this;
  383. }
  384. /**
  385. * @return array
  386. */
  387. public function getBlockedVarsForExport(): array
  388. {
  389. return ['blockedVarsForExport', 'childs'];
  390. }
  391. public function __sleep(): array
  392. {
  393. $vars = get_object_vars($this);
  394. foreach ($this->getBlockedVarsForExport() as $blockedVar) {
  395. unset($vars[$blockedVar]);
  396. }
  397. return array_keys($vars);
  398. }
  399. /**
  400. * {@inheritdoc}
  401. */
  402. public static function __set_state($data)
  403. {
  404. $obj = new static();
  405. $obj->setValues($data);
  406. $obj->childs = $obj->children; // @phpstan-ignore-line
  407. return $obj;
  408. }
  409. }