Skip to content

Commit

Permalink
ENGCOM-4484: [Backport] Fix #21692 - logic in constructor of address …
Browse files Browse the repository at this point in the history
…validator #21719
  • Loading branch information
sivaschenko authored Mar 28, 2019
2 parents 9399ac8 + ed7accf commit bd584bb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
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 @@ -48,8 +48,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 @@ -60,6 +60,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 @@ -72,16 +83,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 a required field', $label);
Expand Down Expand Up @@ -194,23 +196,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 fax 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 @@ -47,34 +52,42 @@ class Resolver implements ResolverInterface
*/
protected $emulatedLocales = [];

/**
* @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 @@ -83,12 +96,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 @@ -98,7 +114,7 @@ public function getDefaultLocale()
}

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

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

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

/**
* {@inheritdoc}
* @inheritdoc
*/
public function revert()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected function setUp()

/** @var \Magento\Directory\Model\CurrencyFactory|\PHPUnit_Framework_MockObject_MockObject $currencyFactory */
$currencyFactory = $this->getMockBuilder(\Magento\Directory\Model\CurrencyFactory::class)
->disableOriginalConstructor()
->getMock();

$this->formatModel = new \Magento\Framework\Locale\Format(
Expand Down

0 comments on commit bd584bb

Please sign in to comment.