ElementFactory.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Customer Form Element Factory
  8. */
  9. namespace Magento\Customer\Model\Metadata;
  10. class ElementFactory
  11. {
  12. const OUTPUT_FORMAT_JSON = 'json';
  13. const OUTPUT_FORMAT_TEXT = 'text';
  14. const OUTPUT_FORMAT_HTML = 'html';
  15. const OUTPUT_FORMAT_PDF = 'pdf';
  16. const OUTPUT_FORMAT_ONELINE = 'oneline';
  17. const OUTPUT_FORMAT_ARRAY = 'array';
  18. // available only for multiply attributes
  19. // available only for multiply attributes
  20. protected $_objectManager;
  21. /**
  22. * @var \Magento\Framework\Stdlib\StringUtils
  23. */
  24. protected $_string;
  25. /**
  26. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  27. * @param \Magento\Framework\Stdlib\StringUtils $string
  28. */
  29. public function __construct(
  30. \Magento\Framework\ObjectManagerInterface $objectManager,
  31. \Magento\Framework\Stdlib\StringUtils $string
  32. ) {
  33. $this->_objectManager = $objectManager;
  34. $this->_string = $string;
  35. }
  36. /**
  37. * Create Form Element
  38. *
  39. * @param \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute
  40. * @param string|int|bool $value
  41. * @param string $entityTypeCode
  42. * @param bool $isAjax
  43. * @return \Magento\Customer\Model\Metadata\Form\AbstractData
  44. */
  45. public function create(
  46. \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute,
  47. $value,
  48. $entityTypeCode,
  49. $isAjax = false
  50. ) {
  51. $dataModelClass = $attribute->getDataModel();
  52. $params = [
  53. 'entityTypeCode' => $entityTypeCode,
  54. 'value' => $value === null ? false : $value,
  55. 'isAjax' => $isAjax,
  56. 'attribute' => $attribute,
  57. ];
  58. /** TODO fix when Validation is implemented MAGETWO-17341 */
  59. if ($dataModelClass == \Magento\Customer\Model\Attribute\Data\Postcode::class) {
  60. $dataModelClass = \Magento\Customer\Model\Metadata\Form\Postcode::class;
  61. }
  62. if (!empty($dataModelClass)) {
  63. $dataModel = $this->_objectManager->create($dataModelClass, $params);
  64. } else {
  65. $dataModelClass = sprintf(
  66. 'Magento\Customer\Model\Metadata\Form\%s',
  67. $this->_string->upperCaseWords($attribute->getFrontendInput())
  68. );
  69. $dataModel = $this->_objectManager->create($dataModelClass, $params);
  70. }
  71. return $dataModel;
  72. }
  73. }