vendor/sulu/form-bundle/Entity/Dynamic.php line 17

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\Bundle\FormBundle\Entity;
  11. use Sulu\Component\Persistence\Model\AuditableInterface;
  12. use Sulu\Component\Persistence\Model\AuditableTrait;
  13. class Dynamic implements AuditableInterface
  14. {
  15. use AuditableTrait;
  16. public const TYPE_ATTACHMENT = 'attachment';
  17. public const TYPE_EMAIL = 'email';
  18. /**
  19. * @var string[]
  20. */
  21. protected static $ARRAY_TYPES = [
  22. 'checkboxMultiple',
  23. 'dropdownMultiple',
  24. self::TYPE_ATTACHMENT,
  25. ];
  26. /**
  27. * @var string[]
  28. */
  29. public static $HIDDEN_TYPES = [
  30. 'spacer',
  31. 'headline',
  32. 'freeText',
  33. 'recaptcha',
  34. 'hidden',
  35. ];
  36. /**
  37. * @var null|int
  38. */
  39. private $id;
  40. /**
  41. * @var string
  42. */
  43. private $type;
  44. /**
  45. * @var string
  46. */
  47. private $typeId;
  48. /**
  49. * @var string|null
  50. */
  51. private $typeName;
  52. /**
  53. * @var string
  54. */
  55. private $locale;
  56. /**
  57. * @var null|Form
  58. */
  59. private $form;
  60. /**
  61. * @var string
  62. */
  63. private $webspaceKey;
  64. /**
  65. * @var string|null
  66. */
  67. private $data;
  68. /**
  69. * Dynamic constructor.
  70. *
  71. * @param mixed[] $data
  72. */
  73. public function __construct(string $type, string $typeId, string $locale, ?Form $form, array $data = [], ?string $webspaceKey = null, ?string $typeName = null)
  74. {
  75. $this->type = $type;
  76. $this->typeId = $typeId;
  77. $this->locale = $locale;
  78. $this->form = $form;
  79. $this->webspaceKey = $webspaceKey;
  80. $this->typeName = $typeName;
  81. $this->setData($data);
  82. }
  83. /**
  84. * @return mixed[]
  85. */
  86. public function getData(): array
  87. {
  88. return \json_decode($this->data ?: '[]', true);
  89. }
  90. /**
  91. * @param mixed[] $data
  92. */
  93. public function setData(array $data): self
  94. {
  95. $this->data = \json_encode($data, \JSON_UNESCAPED_UNICODE);
  96. return $this;
  97. }
  98. /**
  99. * @param mixed $value
  100. */
  101. public function setField(string $key, $value): self
  102. {
  103. $array = $this->getData();
  104. $array[$key] = $value;
  105. $this->data = \json_encode($array, \JSON_UNESCAPED_UNICODE);
  106. return $this;
  107. }
  108. /**
  109. * @return string|mixed|null
  110. */
  111. public function getField(string $key)
  112. {
  113. $array = $this->getData();
  114. if (isset($array[$key])) {
  115. return $array[$key];
  116. }
  117. return null;
  118. }
  119. /**
  120. * @return mixed[]
  121. */
  122. public function getFields(bool $hideHidden = false): array
  123. {
  124. $entry = [];
  125. if (!$this->form) {
  126. return [];
  127. }
  128. foreach ($this->form->getFields() as $field) {
  129. if ($hideHidden && \in_array($field->getType(), self::$HIDDEN_TYPES)) {
  130. continue;
  131. }
  132. $entry[$field->getKey()] = $this->getField($field->getKey());
  133. }
  134. return $entry;
  135. }
  136. /**
  137. * @return mixed[]
  138. */
  139. public function getFieldsByType(string $type): array
  140. {
  141. $entry = [];
  142. if (!$this->form) {
  143. return [];
  144. }
  145. foreach ($this->form->getFieldsByType($type) as $field) {
  146. $entry[$field->getKey()] = $this->getField($field->getKey());
  147. }
  148. return $entry;
  149. }
  150. public function getFieldType(string $key): ?string
  151. {
  152. if (!$this->form) {
  153. return null;
  154. }
  155. return $this->form->getFieldType($key);
  156. }
  157. public function getId(): ?int
  158. {
  159. return $this->id;
  160. }
  161. public function getForm(): Form
  162. {
  163. return $this->form;
  164. }
  165. public function getLocale(): string
  166. {
  167. return $this->locale;
  168. }
  169. public function setLocale(string $locale): self
  170. {
  171. $this->locale = $locale;
  172. return $this;
  173. }
  174. public function getType(): string
  175. {
  176. return $this->type;
  177. }
  178. public function getTypeId(): string
  179. {
  180. return $this->typeId;
  181. }
  182. public function getTypeName(): ?string
  183. {
  184. return $this->typeName;
  185. }
  186. public function getWebspaceKey(): ?string
  187. {
  188. return $this->webspaceKey;
  189. }
  190. public function getSalutation(): ?string
  191. {
  192. return $this->getField('salutation');
  193. }
  194. public function getTitle(): ?string
  195. {
  196. return $this->getField('title');
  197. }
  198. public function getFirstName(): ?string
  199. {
  200. return $this->getField('firstName');
  201. }
  202. public function getLastName(): ?string
  203. {
  204. return $this->getField('lastName');
  205. }
  206. public function getEmail(): ?string
  207. {
  208. return $this->getField('email');
  209. }
  210. public function getPhone(): ?string
  211. {
  212. return $this->getField('phone');
  213. }
  214. public function getFax(): ?string
  215. {
  216. return $this->getField('fax');
  217. }
  218. public function getStreet(): ?string
  219. {
  220. return $this->getField('street');
  221. }
  222. public function getZip(): ?string
  223. {
  224. return $this->getField('zip');
  225. }
  226. public function getCity(): ?string
  227. {
  228. return $this->getField('city');
  229. }
  230. public function getState(): ?string
  231. {
  232. return $this->getField('state');
  233. }
  234. public function getCountry(): ?string
  235. {
  236. return $this->getField('country');
  237. }
  238. public function getFunction(): ?string
  239. {
  240. return $this->getField('function');
  241. }
  242. public function getCompany(): ?string
  243. {
  244. return $this->getField('company');
  245. }
  246. public function getText(): ?string
  247. {
  248. return $this->getField('text');
  249. }
  250. public function getTextarea(): ?string
  251. {
  252. return $this->getField('textarea');
  253. }
  254. public function getDate(): ?string
  255. {
  256. return $this->getField('data');
  257. }
  258. /**
  259. * @return int[]|null
  260. */
  261. public function getAttachment(): ?array
  262. {
  263. return $this->getField('attachment');
  264. }
  265. public function getCheckbox(): ?string
  266. {
  267. return $this->getField('checkbox');
  268. }
  269. public function getCheckboxMultiple(): ?string
  270. {
  271. return $this->getField('checkboxMultiple');
  272. }
  273. public function getDropdown(): ?string
  274. {
  275. return $this->getField('dropdown');
  276. }
  277. public function getDropdownMultiple(): ?string
  278. {
  279. return $this->getField('dropdownMultiple');
  280. }
  281. public function getRadioButtons(): ?string
  282. {
  283. return $this->getField('radioButtons');
  284. }
  285. }