Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Mapper.php
Go to the documentation of this file.
1 <?php
7 
11 class Mapper extends \Magento\Framework\Code\Generator\EntityAbstract
12 {
16  const ENTITY_TYPE = 'mapper';
17 
23  protected function _getClassProperties()
24  {
25  $properties = [
26  [
27  'name' => $this->_getSourceBuilderPropertyName(),
28  'visibility' => 'protected',
29  'docblock' => [
30  'shortDescription' => $this->_getSourceBuilderPropertyName(),
31  'tags' => [
32  [
33  'name' => 'var',
34  'description' => $this->getSourceClassName() . 'Builder',
35  ],
36  ],
37  ],
38  ],
39  [
40  'name' => 'registry',
41  'visibility' => 'protected',
42  'defaultValue' => [],
43  'docblock' => [
44  'shortDescription' => $this->getSourceClassName() . '[]',
45  'tags' => [['name' => 'var', 'description' => 'array']],
46  ]
47  ],
48  ];
49  return $properties;
50  }
51 
57  protected function _getSourceBuilderPropertyName()
58  {
59  return lcfirst($this->getSourceClassNameWithoutNamespace()) . 'Builder';
60  }
61 
67  protected function _getDefaultConstructorDefinition()
68  {
69  return [
70  'name' => '__construct',
71  'parameters' => [
72  [
73  'name' => $this->_getSourceBuilderPropertyName(),
74  'type' => $this->getSourceClassName() . 'Builder',
75  ],
76  ],
77  'body' => "\$this->"
79  . " = \$" . $this->_getSourceBuilderPropertyName() . ';',
80  'docblock' => [
81  'shortDescription' => ucfirst(static::ENTITY_TYPE) . ' constructor',
82  'tags' => [
83  [
84  'name' => 'param',
85  'description' => $this->getSourceClassName() . " \$" . $this->_getSourceBuilderPropertyName(),
86  ],
87  ],
88  ]
89  ];
90  }
91 
97  protected function _getClassMethods()
98  {
99  $construct = $this->_getDefaultConstructorDefinition();
100  $body = "\$this->" . $this->_getSourceBuilderPropertyName() . "->populateWithArray(\$object->getData());"
101  . "\nreturn \$this->" . $this->_getSourceBuilderPropertyName() . "->create();";
102  $extract = [
103  'name' => 'extractDto',
104  'parameters' => [
105  [
106  'name' => 'object',
107  'type' => '\\' . \Magento\Framework\Model\AbstractModel::class,
108  ],
109  ],
110  'body' => $body,
111  'docblock' => [
112  'shortDescription' => 'Extract data object from model',
113  'tags' => [
114  [
115  'name' => 'param',
116  'description' => '\\Magento\Framework\Model\AbstractModel $object',
117  ],
118  [
119  'name' => 'return',
120  'description' => $this->getSourceClassName(),
121  ],
122  ],
123  ],
124  ];
125  return [$construct, $extract];
126  }
127 
131  protected function _validateData()
132  {
133  $result = parent::_validateData();
134 
135  if ($result) {
136  $sourceClassName = $this->getSourceClassName();
137  $resultClassName = $this->_getResultClassName();
138 
139  if ($resultClassName !== $sourceClassName . 'Mapper') {
140  $this->_addError(
141  'Invalid Mapper class name [' . $resultClassName . ']. Use ' . $sourceClassName . 'Mapper'
142  );
143  $result = false;
144  }
145  }
146  return $result;
147  }
148 }
$properties
Definition: categories.php:26