PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/module-customer/Block/Address/Renderer/DefaultRenderer.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 194 lines | 113 code | 14 blank | 67 comment | 12 complexity | 56a533007e7aad181b96987096c0280f MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Address\Renderer;
  7. use Magento\Customer\Model\Address\AddressModelInterface;
  8. use Magento\Customer\Model\Address\Mapper;
  9. use Magento\Customer\Model\Metadata\ElementFactory;
  10. use Magento\Framework\View\Element\AbstractBlock;
  11. /**
  12. * Address format renderer default
  13. */
  14. class DefaultRenderer extends AbstractBlock implements RendererInterface
  15. {
  16. /**
  17. * Format type object
  18. *
  19. * @var \Magento\Framework\DataObject
  20. */
  21. protected $_type;
  22. /**
  23. * @var ElementFactory
  24. */
  25. protected $_elementFactory;
  26. /**
  27. * @var \Magento\Directory\Model\CountryFactory
  28. */
  29. protected $_countryFactory;
  30. /**
  31. * @var \Magento\Customer\Api\AddressMetadataInterface
  32. */
  33. protected $_addressMetadataService;
  34. /**
  35. * @var Mapper
  36. */
  37. protected $addressMapper;
  38. /**
  39. * Constructor
  40. *
  41. * @param \Magento\Framework\View\Element\Context $context
  42. * @param ElementFactory $elementFactory
  43. * @param \Magento\Directory\Model\CountryFactory $countryFactory
  44. * @param \Magento\Customer\Api\AddressMetadataInterface $metadataService
  45. * @param Mapper $addressMapper
  46. * @param array $data
  47. */
  48. public function __construct(
  49. \Magento\Framework\View\Element\Context $context,
  50. ElementFactory $elementFactory,
  51. \Magento\Directory\Model\CountryFactory $countryFactory,
  52. \Magento\Customer\Api\AddressMetadataInterface $metadataService,
  53. Mapper $addressMapper,
  54. array $data = []
  55. ) {
  56. $this->_elementFactory = $elementFactory;
  57. $this->_countryFactory = $countryFactory;
  58. $this->_addressMetadataService = $metadataService;
  59. $this->addressMapper = $addressMapper;
  60. parent::__construct($context, $data);
  61. $this->_isScopePrivate = true;
  62. }
  63. /**
  64. * Retrieve format type object
  65. *
  66. * @return \Magento\Framework\DataObject
  67. */
  68. public function getType()
  69. {
  70. return $this->_type;
  71. }
  72. /**
  73. * Retrieve format type object
  74. *
  75. * @param \Magento\Framework\DataObject $type
  76. * @return $this
  77. */
  78. public function setType(\Magento\Framework\DataObject $type)
  79. {
  80. $this->_type = $type;
  81. return $this;
  82. }
  83. /**
  84. * @param AbstractAddress|null $address
  85. * @return string
  86. * All new code should use renderArray based on Metadata service
  87. */
  88. public function getFormat(AbstractAddress $address = null)
  89. {
  90. $countryFormat = $address === null
  91. ? false : $address->getCountryModel()->getFormat(
  92. $this->getType()->getCode()
  93. );
  94. $format = $countryFormat ? $countryFormat->getFormat() : $this->getType()->getDefaultFormat();
  95. return $format;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. *
  100. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  101. * @SuppressWarnings(PHPMD.NPathComplexity)
  102. */
  103. public function render(AddressModelInterface $address, $format = null)
  104. {
  105. $address = $address->getDataModel(0, 0);
  106. return $this->renderArray($this->addressMapper->toFlatArray($address), $format);
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getFormatArray($addressAttributes = null)
  112. {
  113. $countryFormat = false;
  114. if ($addressAttributes && isset($addressAttributes['country_id'])) {
  115. /** @var \Magento\Directory\Model\Country $country */
  116. $country = $this->_countryFactory->create()->load($addressAttributes['country_id']);
  117. $countryFormat = $country->getFormat($this->getType()->getCode());
  118. }
  119. $format = $countryFormat ? $countryFormat->getFormat() : $this->getType()->getDefaultFormat();
  120. return $format;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. *
  125. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  126. * @SuppressWarnings(PHPMD.NPathComplexity)
  127. */
  128. public function renderArray($addressAttributes, $format = null)
  129. {
  130. switch ($this->getType()->getCode()) {
  131. case 'html':
  132. $dataFormat = ElementFactory::OUTPUT_FORMAT_HTML;
  133. break;
  134. case 'pdf':
  135. $dataFormat = ElementFactory::OUTPUT_FORMAT_PDF;
  136. break;
  137. case 'oneline':
  138. $dataFormat = ElementFactory::OUTPUT_FORMAT_ONELINE;
  139. break;
  140. default:
  141. $dataFormat = ElementFactory::OUTPUT_FORMAT_TEXT;
  142. break;
  143. }
  144. $attributesMetadata = $this->_addressMetadataService->getAllAttributesMetadata();
  145. $data = [];
  146. foreach ($attributesMetadata as $attributeMetadata) {
  147. if (!$attributeMetadata->isVisible()) {
  148. continue;
  149. }
  150. $attributeCode = $attributeMetadata->getAttributeCode();
  151. if ($attributeCode == 'country_id' && isset($addressAttributes['country_id'])) {
  152. $data['country'] = $this->_countryFactory->create()->loadByCode(
  153. $addressAttributes['country_id']
  154. )->getName();
  155. } elseif ($attributeCode == 'region' && isset($addressAttributes['region'])) {
  156. $data['region'] = __($addressAttributes['region']);
  157. } elseif (isset($addressAttributes[$attributeCode])) {
  158. $value = $addressAttributes[$attributeCode];
  159. $dataModel = $this->_elementFactory->create($attributeMetadata, $value, 'customer_address');
  160. $value = $dataModel->outputValue($dataFormat);
  161. if ($attributeMetadata->getFrontendInput() == 'multiline') {
  162. $values = $dataModel->outputValue(ElementFactory::OUTPUT_FORMAT_ARRAY);
  163. // explode lines
  164. foreach ($values as $k => $v) {
  165. $key = sprintf('%s%d', $attributeCode, $k + 1);
  166. $data[$key] = $v;
  167. }
  168. }
  169. $data[$attributeCode] = $value;
  170. }
  171. }
  172. if ($this->getType()->getEscapeHtml()) {
  173. foreach ($data as $key => $value) {
  174. $data[$key] = $this->escapeHtml($value);
  175. }
  176. }
  177. $format = $format !== null ? $format : $this->getFormatArray($addressAttributes);
  178. return $this->filterManager->template($format, ['variables' => $data]);
  179. }
  180. }