Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractAddress.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Customer\Api\Data\AddressInterfaceFactory;
13 use Magento\Customer\Api\Data\RegionInterfaceFactory;
14 use Magento\Customer\Model\Data\Address as AddressData;
17 
39 {
43  const TYPE_BILLING = 'billing';
44 
45  const TYPE_SHIPPING = 'shipping';
46 
52  protected $_eventPrefix = 'customer_address';
53 
59  protected $_eventObject = 'customer_address';
60 
66  protected static $_countryModels = [];
67 
73  protected static $_regionModels = [];
74 
80  protected $_directoryData = null;
81 
85  protected $_eavConfig;
86 
90  protected $_addressConfig;
91 
95  protected $_regionFactory;
96 
100  protected $_countryFactory;
101 
105  protected $metadataService;
106 
111 
116 
120  protected $dataObjectHelper;
121 
123  private $compositeValidator;
124 
146  public function __construct(
147  \Magento\Framework\Model\Context $context,
148  \Magento\Framework\Registry $registry,
149  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
151  \Magento\Directory\Helper\Data $directoryData,
152  \Magento\Eav\Model\Config $eavConfig,
153  Config $addressConfig,
154  \Magento\Directory\Model\RegionFactory $regionFactory,
155  \Magento\Directory\Model\CountryFactory $countryFactory,
157  AddressInterfaceFactory $addressDataFactory,
158  RegionInterfaceFactory $regionDataFactory,
159  \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
160  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
161  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
162  array $data = [],
163  CompositeValidator $compositeValidator = null
164  ) {
165  $this->_directoryData = $directoryData;
166  $data = $this->_implodeArrayField($data);
167  $this->_eavConfig = $eavConfig;
168  $this->_addressConfig = $addressConfig;
169  $this->_regionFactory = $regionFactory;
170  $this->_countryFactory = $countryFactory;
171  $this->metadataService = $metadataService;
172  $this->addressDataFactory = $addressDataFactory;
173  $this->regionDataFactory = $regionDataFactory;
174  $this->dataObjectHelper = $dataObjectHelper;
175  $this->compositeValidator = $compositeValidator ?: ObjectManager::getInstance()
176  ->get(CompositeValidator::class);
177  parent::__construct(
178  $context,
179  $registry,
180  $extensionFactory,
182  $resource,
183  $resourceCollection,
184  $data
185  );
186  }
187 
193  public function getName()
194  {
195  $name = '';
196  if ($this->_eavConfig->getAttribute('customer_address', 'prefix')->getIsVisible() && $this->getPrefix()) {
197  $name .= $this->getPrefix() . ' ';
198  }
199  $name .= $this->getFirstname();
200  $middleName = $this->_eavConfig->getAttribute('customer_address', 'middlename');
201  if ($middleName->getIsVisible() && $this->getMiddlename()) {
202  $name .= ' ' . $this->getMiddlename();
203  }
204  $name .= ' ' . $this->getLastname();
205  if ($this->_eavConfig->getAttribute('customer_address', 'suffix')->getIsVisible() && $this->getSuffix()) {
206  $name .= ' ' . $this->getSuffix();
207  }
208  return $name;
209  }
210 
216  public function getStreet()
217  {
218  if (is_array($this->getStreetFull())) {
219  return $this->getStreetFull();
220  }
221  return explode("\n", $this->getStreetFull());
222  }
223 
230  public function getStreetLine($number)
231  {
232  $lines = $this->getStreet();
233  return isset($lines[$number - 1]) ? $lines[$number - 1] : '';
234  }
235 
241  public function getStreetFull()
242  {
243  $street = $this->getData('street');
244  return is_array($street) ? implode("\n", $street) : $street;
245  }
246 
253  public function setStreetFull($street)
254  {
255  return $this->setStreet($street);
256  }
257 
264  public function setStreet($street)
265  {
266  $this->setData('street', $street);
267  return $this;
268  }
269 
277  public function setData($key, $value = null)
278  {
279  if (is_array($key)) {
280  $key = $this->_implodeArrayField($key);
281  } elseif (is_array($value) && $this->isAddressMultilineAttribute($key)) {
283  }
284  return parent::setData($key, $value);
285  }
286 
292  protected function isAddressMultilineAttribute($code)
293  {
294  return $code == 'street' || in_array($code, $this->getCustomAttributesCodes());
295  }
296 
303  protected function _implodeArrayField(array $data)
304  {
305  foreach ($data as $key => $value) {
306  if (is_array($value) && $this->isAddressMultilineAttribute($key)) {
307  $data[$key] = $this->_implodeArrayValues($data[$key]);
308  }
309  }
310  return $data;
311  }
312 
319  protected function _implodeArrayValues($value)
320  {
321  if (is_array($value)) {
322  if (!count($value)) {
323  return '';
324  }
325 
326  $isScalar = false;
327  foreach ($value as $val) {
328  if (is_scalar($val)) {
329  $isScalar = true;
330  }
331  }
332  if ($isScalar) {
333  $value = trim(implode("\n", $value));
334  }
335  }
336  return $value;
337  }
338 
346  public function explodeStreetAddress()
347  {
348  $streetLines = $this->getStreet();
349  foreach ($streetLines as $i => $line) {
350  $this->setData('street' . ($i + 1), $line);
351  }
352  return $this;
353  }
354 
360  public function getRegion()
361  {
362  $regionId = $this->getData('region_id');
363  $region = $this->getData('region');
364 
365  if (!$regionId && is_numeric($region)) {
366  if ($this->getRegionModel($region)->getCountryId() == $this->getCountryId()) {
367  $this->setData('region', $this->getRegionModel($region)->getName());
368  $this->setData('region_id', $region);
369  }
370  } elseif ($regionId) {
371  if ($this->getRegionModel($regionId)->getCountryId() == $this->getCountryId()) {
372  $this->setData('region', $this->getRegionModel($regionId)->getName());
373  }
374  } elseif (is_string($region)) {
375  $this->setData('region', $region);
376  }
377 
378  return $this->getData('region');
379  }
380 
386  public function getRegionCode()
387  {
388  $regionId = $this->getData('region_id');
389  $region = $this->getData('region');
390 
391  if (!$regionId && is_numeric($region)) {
392  if ($this->getRegionModel($region)->getCountryId() == $this->getCountryId()) {
393  $this->setData('region_code', $this->getRegionModel($region)->getCode());
394  }
395  } elseif ($regionId) {
396  if ($this->getRegionModel($regionId)->getCountryId() == $this->getCountryId()) {
397  $this->setData('region_code', $this->getRegionModel($regionId)->getCode());
398  }
399  } elseif (is_string($region)) {
400  $this->setData('region_code', $region);
401  }
402  return $this->getData('region_code');
403  }
404 
408  public function getRegionId()
409  {
410  $regionId = $this->getData('region_id');
411  $region = $this->getData('region');
412  if (!$regionId) {
413  if (is_numeric($region)) {
414  $this->setData('region_id', $region);
415  $this->unsRegion();
416  } else {
417  $regionModel = $this->_createRegionInstance()->loadByCode(
418  $this->getRegionCode(),
419  $this->getCountryId()
420  );
421  $this->setData('region_id', $regionModel->getId());
422  }
423  }
424  return $this->getData('region_id');
425  }
426 
430  public function getCountry()
431  {
432  $country = $this->getCountryId();
433  return $country ? $country : $this->getData('country');
434  }
435 
441  public function getCountryModel()
442  {
443  if (!isset(self::$_countryModels[$this->getCountryId()])) {
444  $country = $this->_createCountryInstance();
445  $country->load($this->getCountryId());
446  self::$_countryModels[$this->getCountryId()] = $country;
447  }
448 
449  return self::$_countryModels[$this->getCountryId()];
450  }
451 
458  public function getRegionModel($regionId = null)
459  {
460  if ($regionId === null) {
461  $regionId = $this->getRegionId();
462  }
463 
464  if (!isset(self::$_regionModels[$regionId])) {
465  $region = $this->_createRegionInstance();
466  $region->load($regionId);
467  self::$_regionModels[$regionId] = $region;
468  }
469 
470  return self::$_regionModels[$regionId];
471  }
472 
485  public function format($type)
486  {
487  if (!($formatType = $this->getConfig()->getFormatByCode($type)) || !$formatType->getRenderer()) {
488  return null;
489  }
490  $this->_eventManager->dispatch('customer_address_format', ['type' => $formatType, 'address' => $this]);
491  return $formatType->getRenderer()->render($this);
492  }
493 
499  public function getConfig()
500  {
501  return $this->_addressConfig;
502  }
503 
507  public function beforeSave()
508  {
509  parent::beforeSave();
510  $this->getRegion();
511  return $this;
512  }
513 
524  public function getDataModel($defaultBillingAddressId = null, $defaultShippingAddressId = null)
525  {
526  $addressId = $this->getId();
527 
528  $attributes = $this->metadataService->getAllAttributesMetadata();
529  $addressData = [];
530  foreach ($attributes as $attribute) {
531  $code = $attribute->getAttributeCode();
532  if ($this->getData($code) !== null) {
535  } else {
536  $addressData[$code] = $this->getData($code);
537  }
538  }
539  }
540 
542  $region = $this->regionDataFactory->create();
543  $region->setRegion($this->getRegion())
544  ->setRegionCode($this->getRegionCode())
545  ->setRegionId($this->getRegionId());
546 
547  $addressData[AddressData::REGION] = $region;
548 
549  $addressDataObject = $this->addressDataFactory->create();
550  $this->dataObjectHelper->populateWithArray(
551  $addressDataObject,
552  $addressData,
553  \Magento\Customer\Api\Data\AddressInterface::class
554  );
555  if ($addressId) {
556  $addressDataObject->setId($addressId);
557  }
558 
559  if ($this->getCustomerId() || $this->getParentId()) {
560  $customerId = $this->getCustomerId() ?: $this->getParentId();
561  $addressDataObject->setCustomerId($customerId);
562  if ($defaultBillingAddressId == $addressId) {
563  $addressDataObject->setIsDefaultBilling(true);
564  }
565  if ($defaultShippingAddressId == $addressId) {
566  $addressDataObject->setIsDefaultShipping(true);
567  }
568  }
569 
570  return $addressDataObject;
571  }
572 
578  public function validate()
579  {
580  if ($this->getShouldIgnoreValidation()) {
581  return true;
582  }
583 
584  $errors = $this->compositeValidator->validate($this);
585 
586  if (empty($errors)) {
587  return true;
588  }
589 
590  return $errors;
591  }
592 
596  protected function _createRegionInstance()
597  {
598  return $this->_regionFactory->create();
599  }
600 
604  protected function _createCountryInstance()
605  {
606  return $this->_countryFactory->create();
607  }
608 
614  public function unsRegion()
615  {
616  return $this->unsetData("region");
617  }
618 
623  protected function isCompanyRequired()
624  {
625  return ($this->_eavConfig->getAttribute('customer_address', 'company')->getIsRequired());
626  }
627 
632  protected function isTelephoneRequired()
633  {
634  return ($this->_eavConfig->getAttribute('customer_address', 'telephone')->getIsRequired());
635  }
636 
641  protected function isFaxRequired()
642  {
643  return ($this->_eavConfig->getAttribute('customer_address', 'fax')->getIsRequired());
644  }
645 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
getDataUsingMethod($key, $args=null)
Definition: DataObject.php:218
__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, Config $addressConfig, \Magento\Directory\Model\RegionFactory $regionFactory, \Magento\Directory\Model\CountryFactory $countryFactory, AddressMetadataInterface $metadataService, AddressInterfaceFactory $addressDataFactory, RegionInterfaceFactory $regionDataFactory, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], CompositeValidator $compositeValidator=null)
$number
Definition: details.phtml:22
$resource
Definition: bulk.php:12
$addressData
Definition: order.php:19
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
getDataModel($defaultBillingAddressId=null, $defaultShippingAddressId=null)
Definition: Address.php:173
$attributes
Definition: matrix.phtml:13
$i
Definition: gallery.phtml:31
$errors
Definition: overview.phtml:9
$code
Definition: info.phtml:12
if(!isset($_GET['name'])) $name
Definition: log.php:14