Mapper.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Api\Code\Generator;
  7. /**
  8. * Class Repository
  9. */
  10. class Mapper extends \Magento\Framework\Code\Generator\EntityAbstract
  11. {
  12. /**
  13. * Entity type
  14. */
  15. const ENTITY_TYPE = 'mapper';
  16. /**
  17. * Retrieve class properties
  18. *
  19. * @return array
  20. */
  21. protected function _getClassProperties()
  22. {
  23. $properties = [
  24. [
  25. 'name' => $this->_getSourceBuilderPropertyName(),
  26. 'visibility' => 'protected',
  27. 'docblock' => [
  28. 'shortDescription' => $this->_getSourceBuilderPropertyName(),
  29. 'tags' => [
  30. [
  31. 'name' => 'var',
  32. 'description' => $this->getSourceClassName() . 'Builder',
  33. ],
  34. ],
  35. ],
  36. ],
  37. [
  38. 'name' => 'registry',
  39. 'visibility' => 'protected',
  40. 'defaultValue' => [],
  41. 'docblock' => [
  42. 'shortDescription' => $this->getSourceClassName() . '[]',
  43. 'tags' => [['name' => 'var', 'description' => 'array']],
  44. ]
  45. ],
  46. ];
  47. return $properties;
  48. }
  49. /**
  50. * Returns source factory property Name
  51. *
  52. * @return string
  53. */
  54. protected function _getSourceBuilderPropertyName()
  55. {
  56. return lcfirst($this->getSourceClassNameWithoutNamespace()) . 'Builder';
  57. }
  58. /**
  59. * Get default constructor definition for generated class
  60. *
  61. * @return array
  62. */
  63. protected function _getDefaultConstructorDefinition()
  64. {
  65. return [
  66. 'name' => '__construct',
  67. 'parameters' => [
  68. [
  69. 'name' => $this->_getSourceBuilderPropertyName(),
  70. 'type' => $this->getSourceClassName() . 'Builder',
  71. ],
  72. ],
  73. 'body' => "\$this->"
  74. . $this->_getSourceBuilderPropertyName()
  75. . " = \$" . $this->_getSourceBuilderPropertyName() . ';',
  76. 'docblock' => [
  77. 'shortDescription' => ucfirst(static::ENTITY_TYPE) . ' constructor',
  78. 'tags' => [
  79. [
  80. 'name' => 'param',
  81. 'description' => $this->getSourceClassName() . " \$" . $this->_getSourceBuilderPropertyName(),
  82. ],
  83. ],
  84. ]
  85. ];
  86. }
  87. /**
  88. * Returns list of methods for class generator
  89. *
  90. * @return array
  91. */
  92. protected function _getClassMethods()
  93. {
  94. $construct = $this->_getDefaultConstructorDefinition();
  95. $body = "\$this->" . $this->_getSourceBuilderPropertyName() . "->populateWithArray(\$object->getData());"
  96. . "\nreturn \$this->" . $this->_getSourceBuilderPropertyName() . "->create();";
  97. $extract = [
  98. 'name' => 'extractDto',
  99. 'parameters' => [
  100. [
  101. 'name' => 'object',
  102. 'type' => '\\' . \Magento\Framework\Model\AbstractModel::class,
  103. ],
  104. ],
  105. 'body' => $body,
  106. 'docblock' => [
  107. 'shortDescription' => 'Extract data object from model',
  108. 'tags' => [
  109. [
  110. 'name' => 'param',
  111. 'description' => '\\Magento\Framework\Model\AbstractModel $object',
  112. ],
  113. [
  114. 'name' => 'return',
  115. 'description' => $this->getSourceClassName(),
  116. ],
  117. ],
  118. ],
  119. ];
  120. return [$construct, $extract];
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. protected function _validateData()
  126. {
  127. $result = parent::_validateData();
  128. if ($result) {
  129. $sourceClassName = $this->getSourceClassName();
  130. $resultClassName = $this->_getResultClassName();
  131. if ($resultClassName !== $sourceClassName . 'Mapper') {
  132. $this->_addError(
  133. 'Invalid Mapper class name [' . $resultClassName . ']. Use ' . $sourceClassName . 'Mapper'
  134. );
  135. $result = false;
  136. }
  137. }
  138. return $result;
  139. }
  140. }