vendor/easycorp/easyadmin-bundle/src/Dto/FieldDto.php line 14

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Dto;
  3. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  4. use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
  5. use EasyCorp\Bundle\EasyAdminBundle\Config\Option\TextAlign;
  6. use function Symfony\Component\String\u;
  7. use Symfony\Component\Uid\Ulid;
  8. /**
  9. * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  10. */
  11. final class FieldDto
  12. {
  13. private $fieldFqcn;
  14. private $propertyName;
  15. private $value;
  16. private $formattedValue;
  17. private $formatValueCallable;
  18. private $label;
  19. private $formType;
  20. private $formTypeOptions;
  21. private $sortable;
  22. private $virtual;
  23. private $permission;
  24. private $textAlign;
  25. private $help;
  26. private $cssClass;
  27. // how many columns the field takes when rendering
  28. // (defined as Bootstrap 5 grid classes; e.g. 'col-md-6 col-xxl-3')
  29. private $columns;
  30. // same as $columns but used when the user doesn't define columns explicitly
  31. private $defaultColumns;
  32. private $translationParameters;
  33. private $templateName;
  34. private $templatePath;
  35. /** @var AssetsDto */
  36. private $assets;
  37. private $customOptions;
  38. private $doctrineMetadata;
  39. /** @internal */
  40. private $uniqueId;
  41. private $displayedOn;
  42. public function __construct()
  43. {
  44. $this->uniqueId = new Ulid();
  45. $this->textAlign = TextAlign::LEFT;
  46. $this->cssClass = '';
  47. $this->columns = null;
  48. $this->defaultColumns = '';
  49. $this->templateName = 'crud/field/text';
  50. $this->assets = new AssetsDto();
  51. $this->translationParameters = [];
  52. $this->formTypeOptions = KeyValueStore::new();
  53. $this->customOptions = KeyValueStore::new();
  54. $this->doctrineMetadata = KeyValueStore::new();
  55. $this->displayedOn = KeyValueStore::new([
  56. Crud::PAGE_INDEX => Crud::PAGE_INDEX,
  57. Crud::PAGE_DETAIL => Crud::PAGE_DETAIL,
  58. Crud::PAGE_EDIT => Crud::PAGE_EDIT,
  59. Crud::PAGE_NEW => Crud::PAGE_NEW,
  60. ]);
  61. }
  62. public function __clone()
  63. {
  64. $this->uniqueId = new Ulid();
  65. $this->assets = clone $this->assets;
  66. $this->formTypeOptions = clone $this->formTypeOptions;
  67. $this->customOptions = clone $this->customOptions;
  68. $this->doctrineMetadata = clone $this->doctrineMetadata;
  69. $this->displayedOn = clone $this->displayedOn;
  70. }
  71. public function getUniqueId(): string
  72. {
  73. return $this->uniqueId;
  74. }
  75. public function setUniqueId(string $uniqueId): void
  76. {
  77. $this->uniqueId = $uniqueId;
  78. }
  79. public function isFormDecorationField(): bool
  80. {
  81. return null !== u($this->getCssClass())->containsAny(['field-form_panel', 'field-form_tab']);
  82. }
  83. public function getFieldFqcn(): ?string
  84. {
  85. return $this->fieldFqcn;
  86. }
  87. /**
  88. * @internal Don't use this method yourself. EasyAdmin uses it internally
  89. * to set the field FQCN. It's OK to use getFieldFqcn() to get this value.
  90. */
  91. public function setFieldFqcn(string $fieldFqcn): void
  92. {
  93. $this->fieldFqcn = $fieldFqcn;
  94. }
  95. public function getProperty(): string
  96. {
  97. return $this->propertyName;
  98. }
  99. public function setProperty(string $propertyName): void
  100. {
  101. $this->propertyName = $propertyName;
  102. }
  103. /**
  104. * Returns the original unmodified value stored in the entity field.
  105. */
  106. public function getValue()
  107. {
  108. return $this->value;
  109. }
  110. public function setValue($value): void
  111. {
  112. $this->value = $value;
  113. }
  114. /**
  115. * Returns the value to be displayed for the field (it could be the
  116. * same as the value stored in the field or not).
  117. */
  118. public function getFormattedValue()
  119. {
  120. return $this->formattedValue;
  121. }
  122. public function setFormattedValue($formattedValue): void
  123. {
  124. $this->formattedValue = $formattedValue;
  125. }
  126. public function getFormatValueCallable(): ?callable
  127. {
  128. return $this->formatValueCallable;
  129. }
  130. public function setFormatValueCallable(?callable $callable): void
  131. {
  132. $this->formatValueCallable = $callable;
  133. }
  134. /**
  135. * @return string|false|null
  136. */
  137. public function getLabel()
  138. {
  139. return $this->label;
  140. }
  141. /**
  142. * @param string|false|null $label
  143. */
  144. public function setLabel($label): void
  145. {
  146. $this->label = $label;
  147. }
  148. public function getFormType(): ?string
  149. {
  150. return $this->formType;
  151. }
  152. public function setFormType(string $formTypeFqcn): void
  153. {
  154. $this->formType = $formTypeFqcn;
  155. }
  156. public function getFormTypeOptions(): array
  157. {
  158. return $this->formTypeOptions->all();
  159. }
  160. public function getFormTypeOption(string $optionName)
  161. {
  162. return $this->formTypeOptions->get($optionName);
  163. }
  164. public function setFormTypeOptions(array $formTypeOptions): void
  165. {
  166. foreach ($formTypeOptions as $optionName => $optionValue) {
  167. $this->setFormTypeOption($optionName, $optionValue);
  168. }
  169. }
  170. /**
  171. * @param string $optionName You can use "dot" notation to set nested options (e.g. 'attr.class')
  172. */
  173. public function setFormTypeOption(string $optionName, $optionValue): void
  174. {
  175. $this->formTypeOptions->set($optionName, $optionValue);
  176. }
  177. /**
  178. * @param string $optionName You can use "dot" notation to set nested options (e.g. 'attr.class')
  179. */
  180. public function setFormTypeOptionIfNotSet(string $optionName, $optionValue): void
  181. {
  182. $this->formTypeOptions->setIfNotSet($optionName, $optionValue);
  183. }
  184. public function isSortable(): ?bool
  185. {
  186. return $this->sortable;
  187. }
  188. public function setSortable(bool $isSortable): void
  189. {
  190. $this->sortable = $isSortable;
  191. }
  192. public function isVirtual(): ?bool
  193. {
  194. return $this->virtual;
  195. }
  196. public function setVirtual(bool $isVirtual): void
  197. {
  198. $this->virtual = $isVirtual;
  199. }
  200. public function getTextAlign(): string
  201. {
  202. return $this->textAlign;
  203. }
  204. public function setTextAlign(string $textAlign): void
  205. {
  206. $this->textAlign = $textAlign;
  207. }
  208. public function getPermission(): ?string
  209. {
  210. return $this->permission;
  211. }
  212. public function setPermission(string $permission): void
  213. {
  214. $this->permission = $permission;
  215. }
  216. public function getHelp(): ?string
  217. {
  218. return $this->help;
  219. }
  220. public function setHelp(string $help): void
  221. {
  222. $this->help = $help;
  223. }
  224. public function getCssClass(): string
  225. {
  226. return $this->cssClass;
  227. }
  228. public function setCssClass(string $cssClass): void
  229. {
  230. $this->cssClass = trim($cssClass);
  231. }
  232. public function getColumns(): ?string
  233. {
  234. return $this->columns;
  235. }
  236. public function setColumns(?string $columnCssClasses): void
  237. {
  238. $this->columns = $columnCssClasses;
  239. }
  240. public function getDefaultColumns(): string
  241. {
  242. return $this->defaultColumns;
  243. }
  244. public function setDefaultColumns(string $columnCssClasses): void
  245. {
  246. $this->defaultColumns = $columnCssClasses;
  247. }
  248. public function getTranslationParameters(): array
  249. {
  250. return $this->translationParameters;
  251. }
  252. public function setTranslationParameters(array $translationParameters): void
  253. {
  254. $this->translationParameters = $translationParameters;
  255. }
  256. public function getTemplateName(): ?string
  257. {
  258. return $this->templateName;
  259. }
  260. public function setTemplateName(?string $templateName): void
  261. {
  262. $this->templateName = $templateName;
  263. }
  264. public function getTemplatePath(): ?string
  265. {
  266. return $this->templatePath;
  267. }
  268. public function setTemplatePath(?string $templatePath): void
  269. {
  270. $this->templatePath = $templatePath;
  271. }
  272. public function getAssets(): AssetsDto
  273. {
  274. return $this->assets;
  275. }
  276. public function setAssets(AssetsDto $assets): void
  277. {
  278. $this->assets = $assets;
  279. }
  280. public function addWebpackEncoreAsset(AssetDto $assetDto): void
  281. {
  282. $this->assets->addWebpackEncoreAsset($assetDto);
  283. }
  284. public function addCssAsset(AssetDto $assetDto): void
  285. {
  286. $this->assets->addCssAsset($assetDto);
  287. }
  288. public function addJsAsset(AssetDto $assetDto): void
  289. {
  290. $this->assets->addJsAsset($assetDto);
  291. }
  292. public function addHtmlContentToHead(string $htmlContent): void
  293. {
  294. $this->assets->addHtmlContentToHead($htmlContent);
  295. }
  296. public function addHtmlContentToBody(string $htmlContent): void
  297. {
  298. $this->assets->addHtmlContentToBody($htmlContent);
  299. }
  300. public function getCustomOptions(): KeyValueStore
  301. {
  302. return $this->customOptions;
  303. }
  304. public function getCustomOption(string $optionName)
  305. {
  306. return $this->customOptions->get($optionName);
  307. }
  308. public function setCustomOptions(array $customOptions): void
  309. {
  310. $this->customOptions = KeyValueStore::new($customOptions);
  311. }
  312. public function setCustomOption(string $optionName, $optionValue): void
  313. {
  314. $this->customOptions->set($optionName, $optionValue);
  315. }
  316. public function getDoctrineMetadata(): KeyValueStore
  317. {
  318. return $this->doctrineMetadata;
  319. }
  320. public function setDoctrineMetadata(array $metadata): void
  321. {
  322. $this->doctrineMetadata = KeyValueStore::new($metadata);
  323. }
  324. public function getDisplayedOn(): KeyValueStore
  325. {
  326. return $this->displayedOn;
  327. }
  328. public function setDisplayedOn(KeyValueStore $displayedOn): void
  329. {
  330. $this->displayedOn = $displayedOn;
  331. }
  332. public function isDisplayedOn(string $pageName): bool
  333. {
  334. return $this->displayedOn->has($pageName);
  335. }
  336. }