Files
stemcell/app/code/Stemcell/Customer/Model/Address/Validator/General.php
T
2026-09-15 10:26:34 +08:00

150 lines
4.4 KiB
PHP

<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Stemcell\Customer\Model\Address\Validator;
use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Model\Address\AbstractAddress;
use Magento\Framework\Validator\NotEmpty;
use Magento\Framework\Validator\ValidatorChain;
/**
* Address general fields validator.
*/
class General extends \Magento\Customer\Model\Address\Validator\General
{
/**
* @var \Magento\Eav\Model\Config
*/
protected $eavConfig;
/**
* @var \Magento\Directory\Helper\Data
*/
protected $directoryData;
/**
* @param \Magento\Eav\Model\Config $eavConfig
* @param \Magento\Directory\Helper\Data $directoryData
*/
public function __construct(
\Magento\Eav\Model\Config $eavConfig,
\Magento\Directory\Helper\Data $directoryData
) {
$this->eavConfig = $eavConfig;
$this->directoryData = $directoryData;
parent::__construct($eavConfig, $directoryData);
}
/**
* @inheritdoc
*/
public function validate(AbstractAddress $address)
{
$errors = array_merge(
$this->checkRequiredFields($address),
$this->checkOptionalFields($address)
);
return $errors;
}
/**
* @param AbstractAddress $address
* @return array
* @throws \Magento\Framework\Validator\ValidateException
*/
private function checkRequiredFields(AbstractAddress $address)
{
$errors = [];
if (!ValidatorChain::is($address->getFirstname(), NotEmpty::class)) {
$errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'firstname']);
}
if (!ValidatorChain::is($address->getStreetLine(1), NotEmpty::class)) {
$errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'street']);
}
return $errors;
}
/**
* @param AbstractAddress $address
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Validator\ValidateException
*/
private function checkOptionalFields(AbstractAddress $address)
{
$this->reloadAddressAttributes($address);
$errors = [];
if ($this->isTelephoneRequired()
&& !ValidatorChain::is($address->getTelephone(), NotEmpty::class)
) {
$errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'telephone']);
}
if ($this->isFaxRequired()
&& !ValidatorChain::is($address->getFax(), NotEmpty::class)
) {
$errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'fax']);
}
if ($this->isCompanyRequired()
&& !ValidatorChain::is($address->getCompany(), NotEmpty::class)
) {
$errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'company']);
}
return $errors;
}
/**
* Check if company field required in configuration.
*
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function isCompanyRequired()
{
return $this->eavConfig->getAttribute('customer_address', 'company')->getIsRequired();
}
/**
* Check if telephone field required in configuration.
*
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function isTelephoneRequired()
{
return $this->eavConfig->getAttribute('customer_address', 'telephone')->getIsRequired();
}
/**
* Check if fax field required in configuration.
*
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function isFaxRequired()
{
return $this->eavConfig->getAttribute('customer_address', 'fax')->getIsRequired();
}
/**
* Reload address attributes for the certain store
*
* @param AbstractAddress $address
* @return void
*/
private function reloadAddressAttributes(AbstractAddress $address): void
{
$attributeSetId = $address->getAttributeSetId() ?: AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS;
$address->setData('attribute_set_id', $attributeSetId);
$this->eavConfig->getEntityAttributes(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $address);
}
}