AbstractSimpleObjectBuilder.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Api;
  7. /**
  8. * Base Builder Class for simple data Objects
  9. * @SuppressWarnings(PHPMD.NumberOfChildren)
  10. */
  11. abstract class AbstractSimpleObjectBuilder implements SimpleBuilderInterface
  12. {
  13. /**
  14. * @var array
  15. */
  16. protected $data;
  17. /**
  18. * @var ObjectFactory
  19. */
  20. protected $objectFactory;
  21. /**
  22. * @param ObjectFactory $objectFactory
  23. */
  24. public function __construct(ObjectFactory $objectFactory)
  25. {
  26. $this->data = [];
  27. $this->objectFactory = $objectFactory;
  28. }
  29. /**
  30. * Builds the Data Object
  31. *
  32. * @return AbstractSimpleObject
  33. */
  34. public function create()
  35. {
  36. $dataObjectType = $this->_getDataObjectType();
  37. $dataObject = $this->objectFactory->create($dataObjectType, ['data' => $this->data]);
  38. $this->data = [];
  39. return $dataObject;
  40. }
  41. /**
  42. * @param string $key
  43. * @param mixed $value
  44. *
  45. * @return $this
  46. */
  47. protected function _set($key, $value)
  48. {
  49. $this->data[$key] = $value;
  50. return $this;
  51. }
  52. /**
  53. * Return the Data type class name
  54. *
  55. * @return string
  56. */
  57. protected function _getDataObjectType()
  58. {
  59. $currentClass = get_class($this);
  60. $builderSuffix = 'Builder';
  61. $dataObjectType = substr($currentClass, 0, -strlen($builderSuffix));
  62. return $dataObjectType;
  63. }
  64. /**
  65. * Return data Object data.
  66. *
  67. * @return array
  68. */
  69. public function getData()
  70. {
  71. return $this->data;
  72. }
  73. }