Date.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Form Element Date Data Model
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Customer\Model\Metadata\Form;
  9. use Magento\Framework\Api\ArrayObjectSearch;
  10. class Date extends AbstractData
  11. {
  12. /**
  13. * @inheritdoc
  14. */
  15. public function extractValue(\Magento\Framework\App\RequestInterface $request)
  16. {
  17. $value = $this->_getRequestValue($request);
  18. return $this->_applyInputFilter($value);
  19. }
  20. /**
  21. * @inheritdoc
  22. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  23. * @SuppressWarnings(PHPMD.NPathComplexity)
  24. */
  25. public function validateValue($value)
  26. {
  27. $errors = [];
  28. $attribute = $this->getAttribute();
  29. $label = $attribute->getStoreLabel();
  30. if ($value === false) {
  31. // try to load original value and validate it
  32. $value = $this->_value;
  33. }
  34. if ($attribute->isRequired() && empty($value)) {
  35. $errors[] = __('"%1" is a required value.', $label);
  36. }
  37. if (!$errors && !$attribute->isRequired() && empty($value)) {
  38. return true;
  39. }
  40. $result = $this->_validateInputRule($value);
  41. if ($result !== true) {
  42. $errors = array_merge($errors, $result);
  43. }
  44. //range validation
  45. $validateRules = $attribute->getValidationRules();
  46. $minDateValue = ArrayObjectSearch::getArrayElementByName(
  47. $validateRules,
  48. 'date_range_min'
  49. );
  50. $maxDateValue = ArrayObjectSearch::getArrayElementByName(
  51. $validateRules,
  52. 'date_range_max'
  53. );
  54. if ($minDateValue !== null && strtotime($value) < $minDateValue
  55. || $maxDateValue !== null && strtotime($value) > $maxDateValue
  56. ) {
  57. if ($minDateValue !== null && $maxDateValue !== null) {
  58. $errors[] = __(
  59. 'Please enter a valid date between %1 and %2 at %3.',
  60. date('d/m/Y', $minDateValue),
  61. date('d/m/Y', $maxDateValue),
  62. $label
  63. );
  64. } elseif ($minDateValue !== null) {
  65. $errors[] = __(
  66. 'Please enter a valid date equal to or greater than %1 at %2.',
  67. date('d/m/Y', $minDateValue),
  68. $label
  69. );
  70. } elseif ($maxDateValue !== null) {
  71. $errors[] = __(
  72. 'Please enter a valid date less than or equal to %1 at %2.',
  73. date('d/m/Y', $maxDateValue),
  74. $label
  75. );
  76. }
  77. }
  78. if (count($errors) == 0) {
  79. return true;
  80. }
  81. return $errors;
  82. }
  83. /**
  84. * @inheritdoc
  85. */
  86. public function compactValue($value)
  87. {
  88. return $value;
  89. }
  90. /**
  91. * @inheritdoc
  92. */
  93. public function restoreValue($value)
  94. {
  95. return $this->compactValue($value);
  96. }
  97. /**
  98. * @inheritdoc
  99. */
  100. public function outputValue($format = \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_TEXT)
  101. {
  102. $value = $this->_value;
  103. if ($value) {
  104. switch ($format) {
  105. case \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_TEXT:
  106. case \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_HTML:
  107. case \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_PDF:
  108. $this->_dateFilterFormat(\IntlDateFormatter::MEDIUM);
  109. break;
  110. }
  111. $value = $this->_applyOutputFilter($value);
  112. }
  113. $this->_dateFilterFormat(\IntlDateFormatter::SHORT);
  114. return $value;
  115. }
  116. }