123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Model\Attribute\Data;
- use Magento\Directory\Helper\Data as DirectoryHelper;
- use Magento\Eav\Model\AttributeDataFactory;
- use Magento\Framework\App\RequestInterface;
- use Magento\Framework\Locale\ResolverInterface;
- use Psr\Log\LoggerInterface as PsrLogger;
- use Magento\Framework\Stdlib\DateTime\TimezoneInterface as MagentoTimezone;
- /**
- * Customer Address Postal/Zip Code Attribute Data Model
- * This Data Model Has to Be Set Up in additional EAV attribute table
- */
- class Postcode extends \Magento\Eav\Model\Attribute\Data\AbstractData
- {
- /**
- * @var \Magento\Directory\Helper\Data
- */
- protected $directoryHelper;
- /**
- * @param MagentoTimezone $localeDate
- * @param PsrLogger $logger
- * @param ResolverInterface $localeResolver
- * @param DirectoryHelper $directoryHelper
- */
- public function __construct(
- MagentoTimezone $localeDate,
- PsrLogger $logger,
- ResolverInterface $localeResolver,
- DirectoryHelper $directoryHelper
- ) {
- $this->directoryHelper = $directoryHelper;
- parent::__construct($localeDate, $logger, $localeResolver);
- }
- /**
- * Validate postal/zip code
- * Return true and skip validation if country zip code is optional
- *
- * @param array|string $value
- * @return array|bool
- */
- public function validateValue($value)
- {
- $attribute = $this->getAttribute();
- $countryId = $this->getExtractedData('country_id');
- if ($this->directoryHelper->isZipCodeOptional($countryId)) {
- return true;
- }
- $errors = [];
- if (empty($value) && $value !== '0') {
- $label = __($attribute->getStoreLabel());
- $errors[] = __('"%1" is a required value.', $label);
- }
- if (count($errors) == 0) {
- return true;
- }
- return $errors;
- }
- /**
- * Extract data from request and return value
- *
- * @param RequestInterface $request
- * @return array|string
- */
- public function extractValue(RequestInterface $request)
- {
- $value = $this->_getRequestValue($request);
- return $this->_applyInputFilter($value);
- }
- /**
- * Export attribute value to entity model
- *
- * @param array|string $value
- * @return $this
- */
- public function compactValue($value)
- {
- if ($value !== false) {
- $this->getEntity()->setDataUsingMethod($this->getAttribute()->getAttributeCode(), $value);
- }
- return $this;
- }
- /**
- * Restore attribute value from SESSION to entity model
- *
- * @param array|string $value
- * @return $this
- */
- public function restoreValue($value)
- {
- return $this->compactValue($value);
- }
- /**
- * Return formated attribute value from entity model
- *
- * @param string $format
- * @return string|array
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function outputValue($format = AttributeDataFactory::OUTPUT_FORMAT_TEXT)
- {
- $value = $this->getEntity()
- ->getData($this->getAttribute()->getAttributeCode());
- $value = $this->_applyOutputFilter($value);
- return $value;
- }
- }
|