Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Postcode.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Directory\Helper\Data as DirectoryHelper;
12 use Psr\Log\LoggerInterface as PsrLogger;
14 
19 class Postcode extends \Magento\Eav\Model\Attribute\Data\AbstractData
20 {
24  protected $directoryHelper;
25 
32  public function __construct(
33  MagentoTimezone $localeDate,
34  PsrLogger $logger,
35  ResolverInterface $localeResolver,
36  DirectoryHelper $directoryHelper
37  ) {
38  $this->directoryHelper = $directoryHelper;
39  parent::__construct($localeDate, $logger, $localeResolver);
40  }
41 
49  public function validateValue($value)
50  {
51  $attribute = $this->getAttribute();
52 
53  $countryId = $this->getExtractedData('country_id');
54  if ($this->directoryHelper->isZipCodeOptional($countryId)) {
55  return true;
56  }
57 
58  $errors = [];
59  if (empty($value) && $value !== '0') {
60  $label = __($attribute->getStoreLabel());
61  $errors[] = __('"%1" is a required value.', $label);
62  }
63  if (count($errors) == 0) {
64  return true;
65  }
66  return $errors;
67  }
68 
76  {
77  $value = $this->_getRequestValue($request);
78  return $this->_applyInputFilter($value);
79  }
80 
87  public function compactValue($value)
88  {
89  if ($value !== false) {
90  $this->getEntity()->setDataUsingMethod($this->getAttribute()->getAttributeCode(), $value);
91  }
92  return $this;
93  }
94 
101  public function restoreValue($value)
102  {
103  return $this->compactValue($value);
104  }
105 
113  public function outputValue($format = AttributeDataFactory::OUTPUT_FORMAT_TEXT)
114  {
115  $value = $this->getEntity()
116  ->getData($this->getAttribute()->getAttributeCode());
117  $value = $this->_applyOutputFilter($value);
118  return $value;
119  }
120 }
_getRequestValue(RequestInterface $request)
__()
Definition: __.php:13
$logger
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
outputValue($format=AttributeDataFactory::OUTPUT_FORMAT_TEXT)
Definition: Postcode.php:113
extractValue(RequestInterface $request)
Definition: Postcode.php:75
__construct(MagentoTimezone $localeDate, PsrLogger $logger, ResolverInterface $localeResolver, DirectoryHelper $directoryHelper)
Definition: Postcode.php:32
$errors
Definition: overview.phtml:9