AddressAttributeData.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Customer\Block\DataProviders;
  8. use Magento\Framework\Escaper;
  9. use Magento\Framework\View\Element\Block\ArgumentInterface;
  10. use Magento\Customer\Api\AddressMetadataInterface;
  11. use Magento\Framework\Exception\LocalizedException;
  12. use Magento\Framework\Exception\NoSuchEntityException;
  13. /**
  14. * Provides address attribute data into template.
  15. */
  16. class AddressAttributeData implements ArgumentInterface
  17. {
  18. /**
  19. * @var AddressMetadataInterface
  20. */
  21. private $addressMetadata;
  22. /**
  23. * @var Escaper
  24. */
  25. private $escaper;
  26. /**
  27. * @param AddressMetadataInterface $addressMetadata
  28. * @param Escaper $escaper
  29. */
  30. public function __construct(
  31. AddressMetadataInterface $addressMetadata,
  32. Escaper $escaper
  33. ) {
  34. $this->addressMetadata = $addressMetadata;
  35. $this->escaper = $escaper;
  36. }
  37. /**
  38. * Returns frontend label for attribute.
  39. *
  40. * @param string $attributeCode
  41. * @return string
  42. * @throws LocalizedException
  43. */
  44. public function getFrontendLabel(string $attributeCode): string
  45. {
  46. try {
  47. $attribute = $this->addressMetadata->getAttributeMetadata($attributeCode);
  48. $frontendLabel = $attribute->getFrontendLabel();
  49. } catch (NoSuchEntityException $e) {
  50. $frontendLabel = '';
  51. }
  52. return $this->escaper->escapeHtml(__($frontendLabel));
  53. }
  54. }