Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #21692 #21752 - logic in constructor of address validator and Locale Resolver check #21693

33 changes: 22 additions & 11 deletions app/code/Magento/Sales/Model/Order/Address/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class Validator

/**
* @param DirectoryHelper $directoryHelper
* @param CountryFactory $countryFactory
* @param EavConfig $eavConfig
* @param CountryFactory $countryFactory
* @param EavConfig $eavConfig
*/
public function __construct(
DirectoryHelper $directoryHelper,
Expand All @@ -61,6 +61,17 @@ public function __construct(
$this->countryFactory = $countryFactory;
$this->eavConfig = $eavConfig ?: ObjectManager::getInstance()
->get(EavConfig::class);
}

/**
* Validate address.
*
* @param \Magento\Sales\Model\Order\Address $address
* @return array
*/
public function validate(Address $address)
{
$warnings = [];

if ($this->isTelephoneRequired()) {
$this->required['telephone'] = 'Phone Number';
Expand All @@ -73,16 +84,7 @@ public function __construct(
if ($this->isFaxRequired()) {
$this->required['fax'] = 'Fax';
}
}

/**
*
* @param \Magento\Sales\Model\Order\Address $address
* @return array
*/
public function validate(Address $address)
{
$warnings = [];
foreach ($this->required as $code => $label) {
if (!$address->hasData($code)) {
$warnings[] = sprintf('"%s" is required. Enter and try again.', $label);
Expand Down Expand Up @@ -195,23 +197,32 @@ protected function isStateRequired($countryId)
}

/**
* Check whether telephone is required for address.
*
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function isTelephoneRequired()
{
return ($this->eavConfig->getAttribute('customer_address', 'telephone')->getIsRequired());
}

/**
* Check whether company is required for address.
*
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function isCompanyRequired()
{
return ($this->eavConfig->getAttribute('customer_address', 'company')->getIsRequired());
}

/**
* Check whether telephone is required for address.
*
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function isFaxRequired()
{
Expand Down
8 changes: 6 additions & 2 deletions lib/internal/Magento/Framework/Locale/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\Framework\Locale;

/**
* Price locale format.
*/
class Format implements \Magento\Framework\Locale\FormatInterface
{
/**
Expand Down Expand Up @@ -38,7 +41,8 @@ public function __construct(
}

/**
* Returns the first found number from a string
* Returns the first found number from a string.
*
* Parsing depends on given locale (grouping and decimal)
*
* Examples for input:
Expand Down Expand Up @@ -100,7 +104,7 @@ public function getPriceFormat($localeCode = null, $currencyCode = null)
}

$formatter = new \NumberFormatter(
$localeCode . '@currency=' . $currency->getCode(),
$currency->getCode() ? $localeCode . '@currency=' . $currency->getCode() : $localeCode,
\NumberFormatter::CURRENCY
);
$format = $formatter->getPattern();
Expand Down
34 changes: 25 additions & 9 deletions lib/internal/Magento/Framework/Locale/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
namespace Magento\Framework\Locale;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\ObjectManager;

/**
* Manages locale config information.
*/
class Resolver implements ResolverInterface
{
/**
Expand Down Expand Up @@ -52,34 +57,42 @@ class Resolver implements ResolverInterface
*/
private $defaultLocalePath;

/**
* @var DeploymentConfig
*/
private $deploymentConfig;

/**
* @param ScopeConfigInterface $scopeConfig
* @param string $defaultLocalePath
* @param string $scopeType
* @param mixed $locale
* @param DeploymentConfig|null $deploymentConfig
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
$defaultLocalePath,
$scopeType,
$locale = null
$locale = null,
DeploymentConfig $deploymentConfig = null
) {
$this->scopeConfig = $scopeConfig;
$this->defaultLocalePath = $defaultLocalePath;
$this->scopeType = $scopeType;
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->create(DeploymentConfig::class);
$this->setLocale($locale);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getDefaultLocalePath()
{
return $this->defaultLocalePath;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function setDefaultLocale($locale)
{
Expand All @@ -88,12 +101,15 @@ public function setDefaultLocale($locale)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getDefaultLocale()
{
if (!$this->defaultLocale) {
$locale = $this->scopeConfig->getValue($this->getDefaultLocalePath(), $this->scopeType);
$locale = false;
if ($this->deploymentConfig->isAvailable() && $this->deploymentConfig->isDbAvailable()) {
$locale = $this->scopeConfig->getValue($this->getDefaultLocalePath(), $this->scopeType);
}
if (!$locale) {
$locale = self::DEFAULT_LOCALE;
}
Expand All @@ -103,7 +119,7 @@ public function getDefaultLocale()
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function setLocale($locale = null)
{
Expand All @@ -116,7 +132,7 @@ public function setLocale($locale = null)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getLocale()
{
Expand All @@ -127,7 +143,7 @@ public function getLocale()
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function emulate($scopeId)
{
Expand All @@ -147,7 +163,7 @@ public function emulate($scopeId)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function revert()
{
Expand Down