AddressAttributeData.php 1.52 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\Customer\Block\DataProviders;

use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

/**
 * Provides address attribute data into template.
 */
class AddressAttributeData implements ArgumentInterface
{
    /**
     * @var AddressMetadataInterface
     */
    private $addressMetadata;

    /**
     * @var Escaper
     */
    private $escaper;

    /**
     * @param AddressMetadataInterface $addressMetadata
     * @param Escaper $escaper
     */
    public function __construct(
        AddressMetadataInterface $addressMetadata,
        Escaper $escaper
    ) {

        $this->addressMetadata = $addressMetadata;
        $this->escaper = $escaper;
    }

    /**
     * Returns frontend label for attribute.
     *
     * @param string $attributeCode
     * @return string
     * @throws LocalizedException
     */
    public function getFrontendLabel(string $attributeCode): string
    {
        try {
            $attribute =  $this->addressMetadata->getAttributeMetadata($attributeCode);
            $frontendLabel = $attribute->getFrontendLabel();
        } catch (NoSuchEntityException $e) {
            $frontendLabel = '';
        }

        return $this->escaper->escapeHtml(__($frontendLabel));
    }
}