Postcode.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Attribute\Data;
  7. use Magento\Directory\Helper\Data as DirectoryHelper;
  8. use Magento\Eav\Model\AttributeDataFactory;
  9. use Magento\Framework\App\RequestInterface;
  10. use Magento\Framework\Locale\ResolverInterface;
  11. use Psr\Log\LoggerInterface as PsrLogger;
  12. use Magento\Framework\Stdlib\DateTime\TimezoneInterface as MagentoTimezone;
  13. /**
  14. * Customer Address Postal/Zip Code Attribute Data Model
  15. * This Data Model Has to Be Set Up in additional EAV attribute table
  16. */
  17. class Postcode extends \Magento\Eav\Model\Attribute\Data\AbstractData
  18. {
  19. /**
  20. * @var \Magento\Directory\Helper\Data
  21. */
  22. protected $directoryHelper;
  23. /**
  24. * @param MagentoTimezone $localeDate
  25. * @param PsrLogger $logger
  26. * @param ResolverInterface $localeResolver
  27. * @param DirectoryHelper $directoryHelper
  28. */
  29. public function __construct(
  30. MagentoTimezone $localeDate,
  31. PsrLogger $logger,
  32. ResolverInterface $localeResolver,
  33. DirectoryHelper $directoryHelper
  34. ) {
  35. $this->directoryHelper = $directoryHelper;
  36. parent::__construct($localeDate, $logger, $localeResolver);
  37. }
  38. /**
  39. * Validate postal/zip code
  40. * Return true and skip validation if country zip code is optional
  41. *
  42. * @param array|string $value
  43. * @return array|bool
  44. */
  45. public function validateValue($value)
  46. {
  47. $attribute = $this->getAttribute();
  48. $countryId = $this->getExtractedData('country_id');
  49. if ($this->directoryHelper->isZipCodeOptional($countryId)) {
  50. return true;
  51. }
  52. $errors = [];
  53. if (empty($value) && $value !== '0') {
  54. $label = __($attribute->getStoreLabel());
  55. $errors[] = __('"%1" is a required value.', $label);
  56. }
  57. if (count($errors) == 0) {
  58. return true;
  59. }
  60. return $errors;
  61. }
  62. /**
  63. * Extract data from request and return value
  64. *
  65. * @param RequestInterface $request
  66. * @return array|string
  67. */
  68. public function extractValue(RequestInterface $request)
  69. {
  70. $value = $this->_getRequestValue($request);
  71. return $this->_applyInputFilter($value);
  72. }
  73. /**
  74. * Export attribute value to entity model
  75. *
  76. * @param array|string $value
  77. * @return $this
  78. */
  79. public function compactValue($value)
  80. {
  81. if ($value !== false) {
  82. $this->getEntity()->setDataUsingMethod($this->getAttribute()->getAttributeCode(), $value);
  83. }
  84. return $this;
  85. }
  86. /**
  87. * Restore attribute value from SESSION to entity model
  88. *
  89. * @param array|string $value
  90. * @return $this
  91. */
  92. public function restoreValue($value)
  93. {
  94. return $this->compactValue($value);
  95. }
  96. /**
  97. * Return formated attribute value from entity model
  98. *
  99. * @param string $format
  100. * @return string|array
  101. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  102. */
  103. public function outputValue($format = AttributeDataFactory::OUTPUT_FORMAT_TEXT)
  104. {
  105. $value = $this->getEntity()
  106. ->getData($this->getAttribute()->getAttributeCode());
  107. $value = $this->_applyOutputFilter($value);
  108. return $value;
  109. }
  110. }