123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sales\Model\Order\Address;
- use Magento\Directory\Helper\Data as DirectoryHelper;
- use Magento\Directory\Model\CountryFactory;
- use Magento\Eav\Model\Config as EavConfig;
- use Magento\Framework\App\ObjectManager;
- use Magento\Sales\Model\Order\Address;
- /**
- * Class Validator
- */
- class Validator
- {
- /**
- * @var array
- */
- protected $required = [
- 'parent_id' => 'Parent Order Id',
- 'postcode' => 'Zip code',
- 'lastname' => 'Last name',
- 'street' => 'Street',
- 'city' => 'City',
- 'email' => 'Email',
- 'country_id' => 'Country',
- 'firstname' => 'First Name',
- 'address_type' => 'Address Type',
- ];
- /**
- * @var DirectoryHelper
- */
- protected $directoryHelper;
- /**
- * @var CountryFactory
- */
- protected $countryFactory;
- /**
- * @var EavConfig
- */
- protected $eavConfig;
- /**
- * @param DirectoryHelper $directoryHelper
- * @param CountryFactory $countryFactory
- * @param EavConfig $eavConfig
- */
- public function __construct(
- DirectoryHelper $directoryHelper,
- CountryFactory $countryFactory,
- EavConfig $eavConfig = null
- ) {
- $this->directoryHelper = $directoryHelper;
- $this->countryFactory = $countryFactory;
- $this->eavConfig = $eavConfig ?: ObjectManager::getInstance()
- ->get(EavConfig::class);
- if ($this->isTelephoneRequired()) {
- $this->required['telephone'] = 'Phone Number';
- }
- if ($this->isCompanyRequired()) {
- $this->required['company'] = 'Company';
- }
- 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);
- }
- }
- if (!filter_var($address->getEmail(), FILTER_VALIDATE_EMAIL)) {
- $warnings[] = 'Email has a wrong format';
- }
- if (!filter_var(in_array($address->getAddressType(), [Address::TYPE_BILLING, Address::TYPE_SHIPPING]))) {
- $warnings[] = 'Address type doesn\'t match required options';
- }
- return $warnings;
- }
- /**
- * Validate address attribute for customer creation
- *
- * @return bool|array
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- *
- * @param Address $address
- */
- public function validateForCustomer(Address $address)
- {
- if ($address->getShouldIgnoreValidation()) {
- return true;
- }
- $errors = [];
- if ($this->isEmpty($address->getFirstname())) {
- $errors[] = __('Please enter the first name.');
- }
- if ($this->isEmpty($address->getLastname())) {
- $errors[] = __('Please enter the last name.');
- }
- if ($this->isEmpty($address->getStreetLine(1))) {
- $errors[] = __('Please enter the street.');
- }
- if ($this->isEmpty($address->getCity())) {
- $errors[] = __('Please enter the city.');
- }
- if ($this->isTelephoneRequired()) {
- if ($this->isEmpty($address->getTelephone())) {
- $errors[] = __('Please enter the phone number.');
- }
- }
- if ($this->isCompanyRequired()) {
- if ($this->isEmpty($address->getCompany())) {
- $errors[] = __('Please enter the company.');
- }
- }
- if ($this->isFaxRequired()) {
- if ($this->isEmpty($address->getFax())) {
- $errors[] = __('Please enter the fax number.');
- }
- }
- $countryId = $address->getCountryId();
- if ($this->isZipRequired($countryId) && $this->isEmpty($address->getPostcode())) {
- $errors[] = __('Please enter the zip/postal code.');
- }
- if ($this->isEmpty($countryId)) {
- $errors[] = __('Please enter the country.');
- }
- if ($this->isStateRequired($countryId) && $this->isEmpty($address->getRegionId())) {
- $errors[] = __('Please enter the state/province.');
- }
- return empty($errors) ? true : $errors;
- }
- /**
- * Check if value is empty
- *
- * @param mixed $value
- * @return bool
- */
- protected function isEmpty($value)
- {
- return empty($value);
- }
- /**
- * Checks if zip for current country id is required
- *
- * @param string $countryId
- * @return bool
- */
- protected function isZipRequired($countryId)
- {
- return !in_array($countryId, $this->directoryHelper->getCountriesWithOptionalZip());
- }
- /**
- * Checks if state for current country id is required
- *
- * @param string $countryId
- * @return bool
- */
- protected function isStateRequired($countryId)
- {
- $country = $this->countryFactory->create()->load($countryId);
- return $this->directoryHelper->isRegionRequired($countryId) && $country->getRegionCollection()->getSize();
- }
- /**
- * @return bool
- */
- protected function isTelephoneRequired()
- {
- return ($this->eavConfig->getAttribute('customer_address', 'telephone')->getIsRequired());
- }
- /**
- * @return bool
- */
- protected function isCompanyRequired()
- {
- return ($this->eavConfig->getAttribute('customer_address', 'company')->getIsRequired());
- }
- /**
- * @return bool
- */
- protected function isFaxRequired()
- {
- return ($this->eavConfig->getAttribute('customer_address', 'fax')->getIsRequired());
- }
- }
|