Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Address.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Customer\Model;
7 
10 use Magento\Customer\Api\Data\AddressInterfaceFactory;
11 use Magento\Customer\Api\Data\RegionInterfaceFactory;
13 
23 class Address extends \Magento\Customer\Model\Address\AbstractAddress
24 {
30  protected $_customer;
31 
35  protected $_customerFactory;
36 
40  protected $dataProcessor;
41 
45  protected $dataObjectHelper;
46 
50  protected $indexerRegistry;
51 
55  private $attributeList;
56 
80  public function __construct(
81  \Magento\Framework\Model\Context $context,
82  \Magento\Framework\Registry $registry,
83  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
85  \Magento\Directory\Helper\Data $directoryData,
86  \Magento\Eav\Model\Config $eavConfig,
87  \Magento\Customer\Model\Address\Config $addressConfig,
88  \Magento\Directory\Model\RegionFactory $regionFactory,
89  \Magento\Directory\Model\CountryFactory $countryFactory,
91  AddressInterfaceFactory $addressDataFactory,
92  RegionInterfaceFactory $regionDataFactory,
93  \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
94  CustomerFactory $customerFactory,
95  \Magento\Framework\Reflection\DataObjectProcessor $dataProcessor,
96  \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
97  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
98  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
99  array $data = []
100  ) {
101  $this->dataProcessor = $dataProcessor;
102  $this->_customerFactory = $customerFactory;
103  $this->indexerRegistry = $indexerRegistry;
104  parent::__construct(
105  $context,
106  $registry,
107  $extensionFactory,
109  $directoryData,
110  $eavConfig,
111  $addressConfig,
112  $regionFactory,
113  $countryFactory,
118  $resource,
119  $resourceCollection,
120  $data
121  );
122  }
123 
127  protected function _construct()
128  {
129  $this->_init(\Magento\Customer\Model\ResourceModel\Address::class);
130  }
131 
140  {
141  // Set all attributes
142  $attributes = $this->dataProcessor
143  ->buildOutputDataArray($address, \Magento\Customer\Api\Data\AddressInterface::class);
144 
145  foreach ($attributes as $attributeCode => $attributeData) {
147  $this->setRegion($address->getRegion()->getRegion());
148  $this->setRegionCode($address->getRegion()->getRegionCode());
149  $this->setRegionId($address->getRegion()->getRegionId());
150  } else {
152  }
153  }
154  // Need to explicitly set this due to discrepancy in the keys between model and data object
155  $this->setIsDefaultBilling($address->isDefaultBilling());
156  $this->setIsDefaultShipping($address->isDefaultShipping());
157  if (!$this->getAttributeSetId()) {
158  $this->setAttributeSetId(AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS);
159  }
160  $customAttributes = $address->getCustomAttributes();
161  if ($customAttributes !== null) {
162  foreach ($customAttributes as $attribute) {
163  $this->setData($attribute->getAttributeCode(), $attribute->getValue());
164  }
165  }
166 
167  return $this;
168  }
169 
173  public function getDataModel($defaultBillingAddressId = null, $defaultShippingAddressId = null)
174  {
175  if ($this->getCustomerId() || $this->getParentId()) {
176  if ($this->getCustomer()->getDefaultBillingAddress()) {
177  $defaultBillingAddressId = $this->getCustomer()->getDefaultBillingAddress()->getId();
178  }
179  if ($this->getCustomer()->getDefaultShippingAddress()) {
180  $defaultShippingAddressId = $this->getCustomer()->getDefaultShippingAddress()->getId();
181  }
182  }
183  return parent::getDataModel($defaultBillingAddressId, $defaultShippingAddressId);
184  }
185 
191  public function getCustomerId()
192  {
193  return $this->_getData('customer_id') ? $this->_getData('customer_id') : $this->getParentId();
194  }
195 
202  public function setCustomerId($id)
203  {
204  $this->setParentId($id);
205  $this->setData('customer_id', $id);
206  return $this;
207  }
208 
214  public function getCustomer()
215  {
216  if (!$this->getCustomerId()) {
217  return false;
218  }
219  if (empty($this->_customer)) {
220  $this->_customer = $this->_createCustomer()->load($this->getCustomerId());
221  }
222  return $this->_customer;
223  }
224 
231  public function setCustomer(Customer $customer)
232  {
233  $this->_customer = $customer;
234  $this->setCustomerId($customer->getId());
235  return $this;
236  }
237 
243  public function getAttributes()
244  {
245  $attributes = $this->getData('attributes');
246  if ($attributes === null) {
247  $attributes = $this->_getResource()->loadAllAttributes($this)->getSortedAttributes();
248  $this->setData('attributes', $attributes);
249  }
250  return $attributes;
251  }
252 
258  public function getDefaultAttributeCodes()
259  {
260  return $this->_getResource()->getDefaultAttributes();
261  }
262 
266  public function __clone()
267  {
268  $this->setId(null);
269  }
270 
276  public function getEntityType()
277  {
278  return $this->_getResource()->getEntityType();
279  }
280 
286  public function getRegionId()
287  {
288  return (int)$this->getData('region_id');
289  }
290 
297  public function setRegionId($regionId)
298  {
299  $this->setData('region_id', (int)$regionId);
300  return $this;
301  }
302 
306  protected function _createCustomer()
307  {
308  return $this->_customerFactory->create();
309  }
310 
316  public function getEntityTypeId()
317  {
318  return $this->getEntityType()->getId();
319  }
320 
326  public function afterSave()
327  {
328  $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
329  if ($indexer->getState()->getStatus() == StateInterface::STATUS_VALID) {
330  $this->_getResource()->addCommitCallback([$this, 'reindex']);
331  }
332  return parent::afterSave();
333  }
334 
340  public function afterDeleteCommit()
341  {
342  $this->reindex();
343  return parent::afterDeleteCommit();
344  }
345 
351  public function reindex()
352  {
354  $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
355  $indexer->reindexRow($this->getCustomerId());
356  }
357 
362  protected function getCustomAttributesCodes()
363  {
364  return array_keys($this->getAttributeList()->getAttributes());
365  }
366 
372  private function getAttributeList()
373  {
374  if (!$this->attributeList) {
375  $this->attributeList = \Magento\Framework\App\ObjectManager::getInstance()->get(
376  \Magento\Customer\Model\Address\CustomAttributeListInterface::class
377  );
378  }
379  return $this->attributeList;
380  }
381 
388  public function getAttributeSetId()
389  {
390  return parent::getAttributeSetId() ?: AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS;
391  }
392 }
$customer
Definition: customers.php:11
$id
Definition: fieldset.phtml:14
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory, \Magento\Directory\Helper\Data $directoryData, \Magento\Eav\Model\Config $eavConfig, \Magento\Customer\Model\Address\Config $addressConfig, \Magento\Directory\Model\RegionFactory $regionFactory, \Magento\Directory\Model\CountryFactory $countryFactory, AddressMetadataInterface $metadataService, AddressInterfaceFactory $addressDataFactory, RegionInterfaceFactory $regionDataFactory, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper, CustomerFactory $customerFactory, \Magento\Framework\Reflection\DataObjectProcessor $dataProcessor, \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Address.php:80
$resource
Definition: bulk.php:12
$address
Definition: customer.php:38
updateData(AddressInterface $address)
Definition: Address.php:139
$attributeCode
Definition: extend.phtml:12
getDataModel($defaultBillingAddressId=null, $defaultShippingAddressId=null)
Definition: Address.php:173
$attributes
Definition: matrix.phtml:13
setDataUsingMethod($key, $args=[])
Definition: DataObject.php:204
setCustomer(Customer $customer)
Definition: Address.php:231