Converter.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\ObjectManager\Code\Generator;
  7. /**
  8. * Class Converter
  9. * @package Magento\Framework\ObjectManager\Code\Generator
  10. */
  11. class Converter extends \Magento\Framework\Code\Generator\EntityAbstract
  12. {
  13. /**
  14. * Entity type
  15. */
  16. const ENTITY_TYPE = 'converter';
  17. /**
  18. * Retrieve class properties
  19. *
  20. * @return array
  21. */
  22. protected function _getClassProperties()
  23. {
  24. return [
  25. [
  26. 'name' => $this->_getFactoryPropertyName(),
  27. 'visibility' => 'protected',
  28. 'docblock' => [
  29. 'shortDescription' => $this->_getFactoryPropertyName(),
  30. 'tags' => [
  31. [
  32. 'name' => 'var',
  33. 'description' => $this->_getFactoryClass(),
  34. ],
  35. ],
  36. ],
  37. ]
  38. ];
  39. }
  40. /**
  41. * Returns factory name
  42. *
  43. * @return string
  44. */
  45. protected function _getFactoryPropertyName()
  46. {
  47. return lcfirst($this->getSourceClassNameWithoutNamespace()) . 'Factory';
  48. }
  49. /**
  50. * Returns factory class
  51. *
  52. * @return string
  53. */
  54. protected function _getFactoryClass()
  55. {
  56. return $this->getSourceClassName() . 'Factory';
  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->_getFactoryPropertyName(),
  70. 'type' => $this->_getFactoryClass(),
  71. ],
  72. ],
  73. 'body' => "\$this->"
  74. . $this->_getFactoryPropertyName()
  75. . " = \$" . $this->_getFactoryPropertyName() . ';',
  76. 'docblock' => [
  77. 'shortDescription' => ucfirst(static::ENTITY_TYPE) . ' constructor',
  78. 'tags' => [
  79. [
  80. 'name' => 'param',
  81. 'description' => $this->getSourceClassName()
  82. . " \$" . $this->_getFactoryPropertyName(),
  83. ],
  84. ],
  85. ]
  86. ];
  87. }
  88. /**
  89. * Returns list of methods for class generator
  90. *
  91. * @return array
  92. */
  93. protected function _getClassMethods()
  94. {
  95. $construct = $this->_getDefaultConstructorDefinition();
  96. $paramName = 'dataObject';
  97. $body = 'return $this->' . $this->_getFactoryPropertyName()
  98. . '->create()->setData($' . $paramName . '->__toArray());';
  99. $getModel = [
  100. 'name' => 'getModel',
  101. 'parameters' => [
  102. [
  103. 'name' => $paramName,
  104. 'type' => \Magento\Framework\Api\AbstractExtensibleObject::class,
  105. ],
  106. ],
  107. 'body' => $body,
  108. 'docblock' => [
  109. 'shortDescription' => 'Extract data object from model',
  110. 'tags' => [
  111. [
  112. 'name' => 'param',
  113. 'description' => '\Magento\Framework\Api\AbstractExtensibleObject $' . $paramName,
  114. ],
  115. [
  116. 'name' => 'return',
  117. 'description' => $this->getSourceClassName()
  118. ],
  119. ],
  120. ],
  121. ];
  122. return [$construct, $getModel];
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. protected function _validateData()
  128. {
  129. if (!parent::_validateData()) {
  130. return false;
  131. }
  132. $sourceClassName = $this->getSourceClassName();
  133. $resultClassName = $this->_getResultClassName();
  134. if ($resultClassName !== $sourceClassName . 'Converter') {
  135. $this->_addError(
  136. 'Invalid Converter class name [' . $resultClassName . ']. Use ' . $sourceClassName . 'Converter'
  137. );
  138. return false;
  139. }
  140. return true;
  141. }
  142. }