Date.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\Attribute\Data;
  7. use Magento\Framework\App\RequestInterface;
  8. /**
  9. * EAV Entity Attribute Date Data Model
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Date extends \Magento\Eav\Model\Attribute\Data\AbstractData
  14. {
  15. /**
  16. * Extract data from request and return value
  17. *
  18. * @param RequestInterface $request
  19. * @return array|string
  20. */
  21. public function extractValue(RequestInterface $request)
  22. {
  23. $value = $this->_getRequestValue($request);
  24. return $this->_applyInputFilter($value);
  25. }
  26. /**
  27. * Validate data
  28. * Return true or array of errors
  29. *
  30. * @param array|string $value
  31. * @return bool|array
  32. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  33. * @SuppressWarnings(PHPMD.NPathComplexity)
  34. */
  35. public function validateValue($value)
  36. {
  37. $errors = [];
  38. $attribute = $this->getAttribute();
  39. if ($value === false) {
  40. // try to load original value and validate it
  41. $value = $this->getEntity()->getDataUsingMethod($attribute->getAttributeCode());
  42. }
  43. if ($attribute->getIsRequired() && empty($value)) {
  44. $label = __($attribute->getStoreLabel());
  45. $errors[] = __('"%1" is a required value.', $label);
  46. }
  47. if (!$errors && !$attribute->getIsRequired() && empty($value)) {
  48. return true;
  49. }
  50. $result = $this->_validateInputRule($value);
  51. if ($result !== true) {
  52. $errors = array_merge($errors, $result);
  53. }
  54. //range validation
  55. $validateRules = $attribute->getValidateRules();
  56. if (!empty($validateRules['date_range_min']) && strtotime(
  57. $value
  58. ) < $validateRules['date_range_min'] || !empty($validateRules['date_range_max']) && strtotime(
  59. $value
  60. ) > $validateRules['date_range_max']
  61. ) {
  62. if (!empty($validateRules['date_range_min']) && !empty($validateRules['date_range_max'])) {
  63. $label = __($attribute->getStoreLabel());
  64. $errors[] = __(
  65. 'Please enter a valid date between %1 and %2 at %3.',
  66. date('d/m/Y', $validateRules['date_range_min']),
  67. date('d/m/Y', $validateRules['date_range_max']),
  68. $label
  69. );
  70. } elseif (!empty($validateRules['date_range_min'])) {
  71. $label = __($attribute->getStoreLabel());
  72. $errors[] = __(
  73. 'Please enter a valid date equal to or greater than %1 at %2.',
  74. date('d/m/Y', $validateRules['date_range_min']),
  75. $label
  76. );
  77. } elseif (!empty($validateRules['date_range_max'])) {
  78. $label = __($attribute->getStoreLabel());
  79. $errors[] = __(
  80. 'Please enter a valid date less than or equal to %1 at %2.',
  81. date('d/m/Y', $validateRules['date_range_max']),
  82. $label
  83. );
  84. }
  85. }
  86. if (count($errors) == 0) {
  87. return true;
  88. }
  89. return $errors;
  90. }
  91. /**
  92. * Export attribute value to entity model
  93. *
  94. * @param array|string $value
  95. * @return $this
  96. */
  97. public function compactValue($value)
  98. {
  99. if ($value !== false) {
  100. if (empty($value)) {
  101. $value = null;
  102. }
  103. $this->getEntity()->setDataUsingMethod($this->getAttribute()->getAttributeCode(), $value);
  104. }
  105. return $this;
  106. }
  107. /**
  108. * Restore attribute value from SESSION to entity model
  109. *
  110. * @param array|string $value
  111. * @return $this
  112. * @codeCoverageIgnore
  113. */
  114. public function restoreValue($value)
  115. {
  116. return $this->compactValue($value);
  117. }
  118. /**
  119. * Return formatted attribute value from entity model
  120. *
  121. * @param string $format
  122. * @return string|array
  123. */
  124. public function outputValue($format = \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT)
  125. {
  126. $value = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
  127. if ($value) {
  128. switch ($format) {
  129. case \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT:
  130. case \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_HTML:
  131. case \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_PDF:
  132. $this->_dateFilterFormat(\IntlDateFormatter::MEDIUM);
  133. break;
  134. }
  135. $value = $this->_applyOutputFilter($value);
  136. }
  137. $this->_dateFilterFormat(\IntlDateFormatter::SHORT);
  138. return $value;
  139. }
  140. }