Validator.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Stdlib\DateTime\Timezone;
  7. use Magento\Framework\Exception\ValidatorException;
  8. use Magento\Framework\Phrase;
  9. /**
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Validator
  14. {
  15. /**
  16. * Maximum allowed year value
  17. *
  18. * @var int
  19. */
  20. protected $_yearMaxValue;
  21. /**
  22. * Minimum allowed year value
  23. *
  24. * @var int
  25. */
  26. protected $_yearMinValue;
  27. /**
  28. * @param int $yearMinValue
  29. * @param int $yearMaxValue
  30. */
  31. public function __construct(
  32. $yearMinValue = \Magento\Framework\Stdlib\DateTime::YEAR_MIN_VALUE,
  33. $yearMaxValue = \Magento\Framework\Stdlib\DateTime::YEAR_MAX_VALUE
  34. ) {
  35. $this->_yearMaxValue = $yearMaxValue;
  36. $this->_yearMinValue = $yearMinValue;
  37. }
  38. /**
  39. * Validate timestamp
  40. *
  41. * @param int|string $timestamp
  42. * @param int|string $toDate
  43. * @return void
  44. * @throws \Magento\Framework\Exception\ValidatorException
  45. */
  46. public function validate($timestamp, $toDate)
  47. {
  48. $transitionYear = date('Y', $timestamp);
  49. if ($transitionYear > $this->_yearMaxValue || $transitionYear < $this->_yearMinValue) {
  50. throw new ValidatorException(
  51. new Phrase(
  52. "The transition year isn't included in the system date range. "
  53. . "Verify the year date range and try again."
  54. )
  55. );
  56. }
  57. if ((int) $timestamp > (int) $toDate) {
  58. throw new ValidatorException(
  59. new Phrase('Transition year is out of specified date range.')
  60. );
  61. }
  62. }
  63. }