123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\ObjectManager\Code\Generator;
- /**
- * Class Converter
- * @package Magento\Framework\ObjectManager\Code\Generator
- */
- class Converter extends \Magento\Framework\Code\Generator\EntityAbstract
- {
- /**
- * Entity type
- */
- const ENTITY_TYPE = 'converter';
- /**
- * Retrieve class properties
- *
- * @return array
- */
- protected function _getClassProperties()
- {
- return [
- [
- 'name' => $this->_getFactoryPropertyName(),
- 'visibility' => 'protected',
- 'docblock' => [
- 'shortDescription' => $this->_getFactoryPropertyName(),
- 'tags' => [
- [
- 'name' => 'var',
- 'description' => $this->_getFactoryClass(),
- ],
- ],
- ],
- ]
- ];
- }
- /**
- * Returns factory name
- *
- * @return string
- */
- protected function _getFactoryPropertyName()
- {
- return lcfirst($this->getSourceClassNameWithoutNamespace()) . 'Factory';
- }
- /**
- * Returns factory class
- *
- * @return string
- */
- protected function _getFactoryClass()
- {
- return $this->getSourceClassName() . 'Factory';
- }
- /**
- * Get default constructor definition for generated class
- *
- * @return array
- */
- protected function _getDefaultConstructorDefinition()
- {
- return [
- 'name' => '__construct',
- 'parameters' => [
- [
- 'name' => $this->_getFactoryPropertyName(),
- 'type' => $this->_getFactoryClass(),
- ],
- ],
- 'body' => "\$this->"
- . $this->_getFactoryPropertyName()
- . " = \$" . $this->_getFactoryPropertyName() . ';',
- 'docblock' => [
- 'shortDescription' => ucfirst(static::ENTITY_TYPE) . ' constructor',
- 'tags' => [
- [
- 'name' => 'param',
- 'description' => $this->getSourceClassName()
- . " \$" . $this->_getFactoryPropertyName(),
- ],
- ],
- ]
- ];
- }
- /**
- * Returns list of methods for class generator
- *
- * @return array
- */
- protected function _getClassMethods()
- {
- $construct = $this->_getDefaultConstructorDefinition();
- $paramName = 'dataObject';
- $body = 'return $this->' . $this->_getFactoryPropertyName()
- . '->create()->setData($' . $paramName . '->__toArray());';
- $getModel = [
- 'name' => 'getModel',
- 'parameters' => [
- [
- 'name' => $paramName,
- 'type' => \Magento\Framework\Api\AbstractExtensibleObject::class,
- ],
- ],
- 'body' => $body,
- 'docblock' => [
- 'shortDescription' => 'Extract data object from model',
- 'tags' => [
- [
- 'name' => 'param',
- 'description' => '\Magento\Framework\Api\AbstractExtensibleObject $' . $paramName,
- ],
- [
- 'name' => 'return',
- 'description' => $this->getSourceClassName()
- ],
- ],
- ],
- ];
- return [$construct, $getModel];
- }
- /**
- * {@inheritdoc}
- */
- protected function _validateData()
- {
- if (!parent::_validateData()) {
- return false;
- }
- $sourceClassName = $this->getSourceClassName();
- $resultClassName = $this->_getResultClassName();
- if ($resultClassName !== $sourceClassName . 'Converter') {
- $this->_addError(
- 'Invalid Converter class name [' . $resultClassName . ']. Use ' . $sourceClassName . 'Converter'
- );
- return false;
- }
- return true;
- }
- }
|