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
7 
13 
22 {
26  const XML_PATH_VIV_DISABLE_AUTO_ASSIGN_DEFAULT = 'customer/create_account/viv_disable_auto_group_assign_default';
27 
28  const XML_PATH_VIV_ON_EACH_TRANSACTION = 'customer/create_account/viv_on_each_transaction';
29 
30  const XML_PATH_VAT_VALIDATION_ENABLED = 'customer/create_account/auto_group_assign';
31 
32  const XML_PATH_VIV_TAX_CALCULATION_ADDRESS_TYPE = 'customer/create_account/tax_calculation_address_type';
33 
34  const XML_PATH_VAT_FRONTEND_VISIBILITY = 'customer/create_account/vat_frontend_visibility';
35 
39  const TYPE_BILLING = 'billing';
40 
41  const TYPE_SHIPPING = 'shipping';
42 
48  protected $_attributes;
49 
55  protected $_config = [];
56 
62  protected $_streetLines = [];
63 
67  protected $_formatTemplate = [];
68 
72  protected $_blockFactory;
73 
77  protected $_storeManager;
78 
85 
90 
94  protected $_addressConfig;
95 
104  public function __construct(
105  \Magento\Framework\App\Helper\Context $context,
106  \Magento\Framework\View\Element\BlockFactory $blockFactory,
107  \Magento\Store\Model\StoreManagerInterface $storeManager,
108  CustomerMetadataInterface $customerMetadataService,
109  AddressMetadataInterface $addressMetadataService,
110  \Magento\Customer\Model\Address\Config $addressConfig
111  ) {
112  $this->_blockFactory = $blockFactory;
113  $this->_storeManager = $storeManager;
114  $this->_customerMetadataService = $customerMetadataService;
115  $this->_addressMetadataService = $addressMetadataService;
116  $this->_addressConfig = $addressConfig;
117  parent::__construct($context);
118  }
119 
125  public function getBookUrl()
126  {
127  }
128 
132  public function getEditUrl()
133  {
134  }
135 
139  public function getDeleteUrl()
140  {
141  }
142 
146  public function getCreateUrl()
147  {
148  }
149 
154  public function getRenderer($renderer)
155  {
156  if (is_string($renderer) && $renderer) {
157  return $this->_blockFactory->createBlock($renderer, []);
158  } else {
159  return $renderer;
160  }
161  }
162 
170  public function getConfig($key, $store = null)
171  {
172  $store = $this->_storeManager->getStore($store);
173  $websiteId = $store->getWebsiteId();
174  if (!isset($this->_config[$websiteId])) {
175  $this->_config[$websiteId] = $this->scopeConfig->getValue(
176  'customer/address',
177  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
178  $store
179  );
180  }
181  return isset($this->_config[$websiteId][$key]) ? (string)$this->_config[$websiteId][$key] : null;
182  }
183 
190  public function getStreetLines($store = null)
191  {
192  $websiteId = $this->_storeManager->getStore($store)->getWebsiteId();
193  if (!isset($this->_streetLines[$websiteId])) {
194  $attribute = $this->_addressMetadataService->getAttributeMetadata('street');
195 
196  $lines = $attribute->getMultilineCount();
197  if ($lines <= 0) {
198  $lines = 2;
199  }
200  $this->_streetLines[$websiteId] = min($lines, 20);
201  }
202 
203  return $this->_streetLines[$websiteId];
204  }
205 
210  public function getFormat($code)
211  {
212  $format = $this->_addressConfig->getFormatByCode($code);
213  return $format->getRenderer() ? $format->getRenderer()->getFormatArray() : '';
214  }
215 
222  public function getFormatTypeRenderer($code)
223  {
224  $formatType = $this->_addressConfig->getFormatByCode($code);
225  if (!$formatType || !$formatType->getRenderer()) {
226  return null;
227  }
228  return $formatType->getRenderer();
229  }
230 
237  public function canShowConfig($key)
238  {
239  return (bool)$this->getConfig($key);
240  }
241 
250  public function getAttributeValidationClass($attributeCode)
251  {
252  $class = '';
253 
254  try {
256  $attribute = isset($this->_attributes[$attributeCode])
257  ? $this->_attributes[$attributeCode]
258  : $this->_addressMetadataService->getAttributeMetadata($attributeCode);
259 
260  $class = $attribute ? $attribute->getFrontendClass() : '';
261  } catch (NoSuchEntityException $e) {
262  // the attribute does not exist so just return an empty string
263  }
264 
265  return $class;
266  }
267 
283  public function convertStreetLines($origStreets, $toCount)
284  {
285  $lines = [];
286  if (!empty($origStreets) && $toCount > 0) {
287  $countArgs = (int)floor(count($origStreets) / $toCount);
288  $modulo = count($origStreets) % $toCount;
289  $offset = 0;
290  $neededLinesCount = 0;
291  for ($i = 0; $i < $toCount; $i++) {
292  $offset += $neededLinesCount;
293  $neededLinesCount = $countArgs;
294  if ($modulo > 0) {
295  ++$neededLinesCount;
296  --$modulo;
297  }
298  $values = array_slice($origStreets, $offset, $neededLinesCount);
299  if (is_array($values)) {
300  $lines[] = implode(' ', $values);
301  }
302  }
303  }
304 
305  return $lines;
306  }
307 
314  public function isVatValidationEnabled($store = null)
315  {
316  return (bool)$this->scopeConfig->getValue(
317  self::XML_PATH_VAT_VALIDATION_ENABLED,
318  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
319  $store
320  );
321  }
322 
329  {
330  return (bool)$this->scopeConfig->getValue(
331  self::XML_PATH_VIV_DISABLE_AUTO_ASSIGN_DEFAULT,
332  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
333  );
334  }
335 
342  public function hasValidateOnEachTransaction($store = null)
343  {
344  return (bool)$this->scopeConfig->getValue(
345  self::XML_PATH_VIV_ON_EACH_TRANSACTION,
346  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
347  $store
348  );
349  }
350 
357  public function getTaxCalculationAddressType($store = null)
358  {
359  return (string)$this->scopeConfig->getValue(
360  self::XML_PATH_VIV_TAX_CALCULATION_ADDRESS_TYPE,
361  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
362  $store
363  );
364  }
365 
371  public function isVatAttributeVisible()
372  {
373  return (bool)$this->scopeConfig->getValue(
374  self::XML_PATH_VAT_FRONTEND_VISIBILITY,
375  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
376  );
377  }
378 
386  public function isAttributeVisible($code)
387  {
388  $attributeMetadata = $this->_addressMetadataService->getAttributeMetadata($code);
389  if ($attributeMetadata) {
390  return $attributeMetadata->isVisible();
391  }
392  return false;
393  }
394 }
getTaxCalculationAddressType($store=null)
Definition: Address.php:357
$storeManager
$values
Definition: options.phtml:88
$_option $_optionId $class
Definition: date.phtml:13
$format
Definition: list.phtml:12
$attributeCode
Definition: extend.phtml:12
convertStreetLines($origStreets, $toCount)
Definition: Address.php:283
const XML_PATH_VIV_TAX_CALCULATION_ADDRESS_TYPE
Definition: Address.php:32
isVatValidationEnabled($store=null)
Definition: Address.php:314
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Framework\View\Element\BlockFactory $blockFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, CustomerMetadataInterface $customerMetadataService, AddressMetadataInterface $addressMetadataService, \Magento\Customer\Model\Address\Config $addressConfig)
Definition: Address.php:104
getConfig($key, $store=null)
Definition: Address.php:170
const XML_PATH_VIV_DISABLE_AUTO_ASSIGN_DEFAULT
Definition: Address.php:26
$i
Definition: gallery.phtml:31
$code
Definition: info.phtml:12
hasValidateOnEachTransaction($store=null)
Definition: Address.php:342