forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractObjectBuilder.php
197 lines (184 loc) · 5.76 KB
/
AbstractObjectBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
namespace Magento\Framework\Service\Data;
/**
* @SuppressWarnings(PHPMD.NumberOfChildren)
*/
abstract class AbstractObjectBuilder
{
/**
* @var array
*/
protected $_data;
/**
* @var ObjectFactory
*/
protected $objectFactory;
/**
* @param ObjectFactory $objectFactory
*/
public function __construct(ObjectFactory $objectFactory)
{
$this->_data = array();
$this->objectFactory = $objectFactory;
}
/**
* Populates the fields with an existing entity.
*
* @param AbstractObject $prototype the prototype to base on
* @return $this
* @throws \LogicException If $prototype object class is not the same type as object that is constructed
*/
public function populate(AbstractObject $prototype)
{
$objectType = $this->_getDataObjectType();
if (get_class($prototype) != $objectType) {
throw new \LogicException('Wrong prototype object given. It can only be of "' . $objectType . '" type.');
}
return $this->populateWithArray($prototype->__toArray());
}
/**
* Template method used to configure the attribute codes for the custom attributes
*
* @return array
*/
public function getCustomAttributesCodes()
{
return array();
}
/**
* Populates the fields with data from the array.
*
* Keys for the map are snake_case attribute/field names.
*
* @param array $data
* @return $this
*/
public function populateWithArray(array $data)
{
$this->_data = array();
$this->_setDataValues($data);
return $this;
}
/**
* Initializes Data Object with the data from array
*
* @param array $data
* @return $this
*/
protected function _setDataValues(array $data)
{
$dataObjectMethods = get_class_methods($this->_getDataObjectType());
foreach ($data as $key => $value) {
/* First, verify is there any getter for the key on the Service Data Object */
$possibleMethods = array(
'get' . \Magento\Framework\Service\DataObjectConverter::snakeCaseToCamelCase($key),
'is' . \Magento\Framework\Service\DataObjectConverter::snakeCaseToCamelCase($key)
);
if (array_intersect($possibleMethods, $dataObjectMethods)) {
$this->_data[$key] = $value;
}
}
return $this;
}
/**
* Merge second Data Object data with first Data Object data and create new Data Object object based on merge
* result.
*
* @param AbstractObject $firstDataObject
* @param AbstractObject $secondDataObject
* @return AbstractObject
* @throws \LogicException
*/
public function mergeDataObjects(AbstractObject $firstDataObject, AbstractObject $secondDataObject)
{
$objectType = $this->_getDataObjectType();
if (get_class($firstDataObject) != $objectType || get_class($secondDataObject) != $objectType) {
throw new \LogicException('Wrong prototype object given. It can only be of "' . $objectType . '" type.');
}
$this->_setDataValues($firstDataObject->__toArray());
$this->_setDataValues($secondDataObject->__toArray());
return $this->create();
}
/**
* Merged data provided in array format with Data Object data and create new Data Object object based on merge
* result.
*
* @param AbstractObject $dataObject
* @param array $data
* @return AbstractObject
* @throws \LogicException
*/
public function mergeDataObjectWithArray(AbstractObject $dataObject, array $data)
{
$objectType = $this->_getDataObjectType();
if (get_class($dataObject) != $objectType) {
throw new \LogicException('Wrong prototype object given. It can only be of "' . $objectType . '" type.');
}
$this->_setDataValues($dataObject->__toArray());
$this->_setDataValues($data);
return $this->create();
}
/**
* Builds the Data Object
*
* @return AbstractObject
*/
public function create()
{
$dataObjectType = $this->_getDataObjectType();
$dataObject = $this->objectFactory->create($dataObjectType, ['builder' => $this]);
$this->_data = array();
return $dataObject;
}
/**
* @param string $key
* @param mixed $value
*
* @return $this
*/
protected function _set($key, $value)
{
$this->_data[$key] = $value;
return $this;
}
/**
* Return the Data type class name
*
* @return string
*/
protected function _getDataObjectType()
{
return substr(get_class($this), 0, -7);
}
/**
* Return data Object data.
*
* @return array
*/
public function getData()
{
return $this->_data;
}
}