Skip to content

Commit c05c39a

Browse files
author
Yu Tang
committed
MAGETWO-33031: Change base data objects and interfaces to be mutable
1 parent 68a48d0 commit c05c39a

File tree

4 files changed

+50
-66
lines changed

4 files changed

+50
-66
lines changed

app/code/Magento/Customer/Model/Data/Address.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,22 @@
88
namespace Magento\Customer\Model\Data;
99

1010
use Magento\Customer\Api\Data\RegionInterface;
11-
use \Magento\Framework\Api\DataObjectHelper;
1211
use \Magento\Framework\Api\AttributeValueFactory;
1312

1413
class Address extends \Magento\Framework\Api\AbstractExtensibleObject implements
1514
\Magento\Customer\Api\Data\AddressInterface
1615
{
1716
/**
18-
* @param DataObjectHelper $dataObjectHelper
1917
* @param \Magento\Customer\Api\AddressMetadataInterface $metadataService
2018
* @param AttributeValueFactory $attributeValueFactory
2119
* @param array $data
2220
*/
2321
public function __construct(
24-
DataObjectHelper $dataObjectHelper,
2522
\Magento\Customer\Api\AddressMetadataInterface $metadataService,
2623
AttributeValueFactory $attributeValueFactory,
2724
$data = []
2825
) {
29-
parent::__construct($dataObjectHelper, $metadataService, $attributeValueFactory, $data);
26+
parent::__construct($metadataService, $attributeValueFactory, $data);
3027
}
3128

3229
/**

app/code/Magento/Customer/Model/Data/Customer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,22 @@
77
namespace Magento\Customer\Model\Data;
88

99
use Magento\Customer\Api\Data\CustomerInterface;
10-
use \Magento\Framework\Api\DataObjectHelper;
1110
use \Magento\Framework\Api\AttributeValueFactory;
1211

1312
class Customer extends \Magento\Framework\Api\AbstractExtensibleObject implements
1413
\Magento\Customer\Api\Data\CustomerInterface
1514
{
1615
/**
17-
* @param DataObjectHelper $dataObjectHelper
1816
* @param \Magento\Customer\Api\CustomerMetadataInterface $metadataService
1917
* @param AttributeValueFactory $attributeValueFactory
2018
* @param array $data
2119
*/
2220
public function __construct(
23-
DataObjectHelper $dataObjectHelper,
2421
\Magento\Customer\Api\CustomerMetadataInterface $metadataService,
2522
AttributeValueFactory $attributeValueFactory,
2623
$data = []
2724
) {
28-
parent::__construct($dataObjectHelper, $metadataService, $attributeValueFactory, $data);
25+
parent::__construct($metadataService, $attributeValueFactory, $data);
2926
}
3027

3128
/**

lib/internal/Magento/Framework/Api/AbstractExtensibleObject.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,27 @@ abstract class AbstractExtensibleObject extends AbstractSimpleObject implements
2424
protected $attributeValueFactory;
2525

2626
/**
27-
* @var DataObjectHelper
27+
* @var MetadataServiceInterface
2828
*/
29-
protected $dataObjectHelper;
29+
protected $metadataService;
3030

3131
/**
32-
* @var MetadataServiceInterface
32+
* @var string[]
3333
*/
34-
protected $metadataService;
34+
protected $customAttributesCodes;
3535

3636
/**
3737
* Initialize internal storage
3838
*
39-
* @param DataObjectHelper $dataObjectHelper
4039
* @param MetadataServiceInterface $metadataService
4140
* @param AttributeValueFactory $attributeValueFactory
4241
* @param array $data
4342
*/
4443
public function __construct(
45-
DataObjectHelper $dataObjectHelper,
4644
MetadataServiceInterface $metadataService,
4745
AttributeValueFactory $attributeValueFactory,
4846
$data = []
4947
) {
50-
$this->dataObjectHelper = $dataObjectHelper;
5148
$this->metadataService = $metadataService;
5249
$this->attributeValueFactory = $attributeValueFactory;
5350
parent::__construct($data);
@@ -86,7 +83,7 @@ public function getCustomAttributes()
8683
*/
8784
public function setCustomAttributes(array $attributes)
8885
{
89-
$customAttributesCodes = $this->dataObjectHelper->getCustomAttributesCodes($this, $this->metadataService);
86+
$customAttributesCodes = $this->getCustomAttributesCodes();
9087
foreach ($attributes as $attribute) {
9188
if (!$attribute instanceof AttributeValue) {
9289
throw new \LogicException('Custom Attribute array elements can only be type of AttributeValue');
@@ -108,7 +105,7 @@ public function setCustomAttributes(array $attributes)
108105
*/
109106
public function setCustomAttribute($attributeCode, $attributeValue)
110107
{
111-
$customAttributesCodes = $this->dataObjectHelper->getCustomAttributesCodes($this, $this->metadataService);
108+
$customAttributesCodes = $this->getCustomAttributesCodes();
112109
/* If key corresponds to custom attribute code, populate custom attributes */
113110
if (in_array($attributeCode, $customAttributesCodes)) {
114111
/** @var AttributeValue $attribute */
@@ -119,4 +116,25 @@ public function setCustomAttribute($attributeCode, $attributeValue)
119116
}
120117
return $this;
121118
}
119+
120+
/**
121+
*
122+
* @return string[]
123+
*/
124+
protected function getCustomAttributesCodes()
125+
{
126+
if (!is_null($this->customAttributesCodes)) {
127+
return $this->customAttributesCodes;
128+
}
129+
$attributeCodes = [];
130+
$customAttributesMetadata = $this->metadataService->getCustomAttributesMetadata(get_class($this));
131+
if (is_array($customAttributesMetadata)) {
132+
/** @var $attribute \Magento\Framework\Api\MetadataObjectInterface */
133+
foreach ($customAttributesMetadata as $attribute) {
134+
$attributeCodes[] = $attribute->getAttributeCode();
135+
}
136+
}
137+
$this->customAttributesCodes = $attributeCodes;
138+
return $attributeCodes;
139+
}
122140
}

lib/internal/Magento/Framework/Api/DataObjectHelper.php

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ class DataObjectHelper
1313
*/
1414
protected $objectFactory;
1515

16-
/**
17-
* Cached custom attribute codes by metadataServiceInterface
18-
*
19-
* @var array
20-
*/
21-
protected $customAttributesCodes = [];
22-
2316
/**
2417
* @var \Magento\Framework\Reflection\DataObjectProcessor
2518
*/
@@ -48,50 +41,24 @@ public function __construct(
4841
/**
4942
* @param ExtensibleDataInterface $dataObject
5043
* @param array $data
44+
* @param string $interfaceName
5145
* @return $this
5246
*/
53-
public function populateWithArray(ExtensibleDataInterface $dataObject, array $data)
47+
public function populateWithArray(ExtensibleDataInterface $dataObject, array $data, $interfaceName = null)
5448
{
55-
$this->_setDataValues($dataObject, $data);
49+
$this->_setDataValues($dataObject, $data, $interfaceName);
5650
return $this;
5751
}
5852

59-
/**
60-
* Template method used to configure the attribute codes for the custom attributes
61-
*
62-
* @param ExtensibleDataInterface $dataObject
63-
* @param MetadataServiceInterface metadataService
64-
* @return string[]
65-
*/
66-
public function getCustomAttributesCodes(
67-
ExtensibleDataInterface $dataObject,
68-
MetadataServiceInterface $metadataService
69-
) {
70-
$metadataServiceClass = get_class($metadataService);
71-
if (isset($this->customAttributesCodes[$metadataServiceClass])) {
72-
return $this->customAttributesCodes[$metadataServiceClass];
73-
}
74-
$attributeCodes = [];
75-
/** @var \Magento\Framework\Api\MetadataObjectInterface[] $customAttributesMetadata */
76-
$customAttributesMetadata = $metadataService
77-
->getCustomAttributesMetadata(get_class($dataObject));
78-
if (is_array($customAttributesMetadata)) {
79-
foreach ($customAttributesMetadata as $attribute) {
80-
$attributeCodes[] = $attribute->getAttributeCode();
81-
}
82-
}
83-
$this->customAttributesCodes[$metadataServiceClass] = $attributeCodes;
84-
return $attributeCodes;
85-
}
86-
8753
/**
8854
* Update Data Object with the data from array
8955
*
9056
* @param ExtensibleDataInterface $dataObject
9157
* @param array $data
58+
* @param string $interfaceName
9259
* @return $this
9360
*/
94-
protected function _setDataValues(ExtensibleDataInterface $dataObject, array $data)
61+
protected function _setDataValues(ExtensibleDataInterface $dataObject, array $data, $interfaceName)
9562
{
9663
$dataObjectMethods = get_class_methods(get_class($dataObject));
9764
foreach ($data as $key => $value) {
@@ -117,7 +84,7 @@ protected function _setDataValues(ExtensibleDataInterface $dataObject, array $da
11784
$dataObject->$methodName($value);
11885
} else {
11986
$getterMethodName = 'get' . $camelCaseKey;
120-
$this->setComplexValue($dataObject, $getterMethodName, $methodName, $value);
87+
$this->setComplexValue($dataObject, $getterMethodName, $methodName, $value, $interfaceName);
12188
}
12289
} else {
12390
$dataObject->setCustomAttribute($key, $value);
@@ -132,15 +99,20 @@ protected function _setDataValues(ExtensibleDataInterface $dataObject, array $da
13299
* @param string $getterMethodName
133100
* @param string $methodName
134101
* @param array $value
102+
* @param string $interfaceName
135103
* @return $this
136104
*/
137105
protected function setComplexValue(
138106
ExtensibleDataInterface $dataObject,
139107
$getterMethodName,
140108
$methodName,
141-
array $value
109+
array $value,
110+
$interfaceName
142111
) {
143-
$returnType = $this->objectProcessor->getMethodReturnType(get_class($dataObject), $getterMethodName);
112+
if ($interfaceName == null) {
113+
$interfaceName = get_class($dataObject);
114+
}
115+
$returnType = $this->objectProcessor->getMethodReturnType($interfaceName, $getterMethodName);
144116
if ($this->typeProcessor->isTypeSimple($returnType)) {
145117
$dataObject->$methodName($value);
146118
return $this;
@@ -151,7 +123,7 @@ protected function setComplexValue(
151123
$objects = [];
152124
foreach ($value as $arrayElementData) {
153125
$object = $this->objectFactory->create($type, []);
154-
$this->populateWithArray($object, $arrayElementData);
126+
$this->populateWithArray($object, $arrayElementData, $type);
155127
$objects[] = $object;
156128
}
157129
$dataObject->$methodName($objects);
@@ -160,7 +132,7 @@ protected function setComplexValue(
160132

161133
if (is_subclass_of($returnType, '\Magento\Framework\Api\ExtensibleDataInterface')) {
162134
$object = $this->objectFactory->create($returnType, []);
163-
$this->populateWithArray($object, $value);
135+
$this->populateWithArray($object, $value, $returnType);
164136
} else {
165137
$object = $this->objectFactory->create($returnType, $value);
166138
}
@@ -171,22 +143,22 @@ protected function setComplexValue(
171143
/**
172144
* Merges second object onto the first
173145
*
174-
* @param string $className
146+
* @param string $interfaceName
175147
* @param ExtensibleDataInterface $firstDataObject
176148
* @param ExtensibleDataInterface $secondDataObject
177149
* @return $this
178150
* @throws \LogicException
179151
*/
180152
public function mergeDataObjects(
181-
$className,
153+
$interfaceName,
182154
ExtensibleDataInterface $firstDataObject,
183155
ExtensibleDataInterface $secondDataObject
184156
) {
185-
if (!$firstDataObject instanceof $className || !$secondDataObject instanceof $className) {
186-
throw new \LogicException('Wrong prototype object given. It can only be of "' . $className . '" type.');
157+
if (!$firstDataObject instanceof $interfaceName || !$secondDataObject instanceof $interfaceName) {
158+
throw new \LogicException('Wrong prototype object given. It can only be of "' . $interfaceName . '" type.');
187159
}
188-
$secondObjectArray = $this->objectProcessor->buildOutputDataArray($secondDataObject, $className);
189-
$this->_setDataValues($firstDataObject, $secondObjectArray);
160+
$secondObjectArray = $this->objectProcessor->buildOutputDataArray($secondDataObject, $interfaceName);
161+
$this->_setDataValues($firstDataObject, $secondObjectArray, $interfaceName);
190162
return $this;
191163
}
192164
}

0 commit comments

Comments
 (0)