DateType.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Type;
  16. use DateTime;
  17. use DateTimeImmutable;
  18. /**
  19. * Class DateType
  20. */
  21. class DateType extends DateTimeType
  22. {
  23. /**
  24. * The class to use for representing date objects
  25. *
  26. * This property can only be used before an instance of this type
  27. * class is constructed. After that use `useMutable()` or `useImmutable()` instead.
  28. *
  29. * @var string
  30. * @deprecated 3.2.0 Use DateType::useMutable() or DateType::useImmutable() instead.
  31. */
  32. public static $dateTimeClass = 'Cake\I18n\Date';
  33. /**
  34. * Date format for DateTime object
  35. *
  36. * @var string|array
  37. */
  38. protected $_format = 'Y-m-d';
  39. /**
  40. * In this class we want Date objects to have their time
  41. * set to the beginning of the day.
  42. *
  43. * @var bool
  44. */
  45. protected $setToDateStart = true;
  46. /**
  47. * Change the preferred class name to the FrozenDate implementation.
  48. *
  49. * @return $this
  50. */
  51. public function useImmutable()
  52. {
  53. $this->_setClassName('Cake\I18n\FrozenDate', 'DateTimeImmutable');
  54. return $this;
  55. }
  56. /**
  57. * Change the preferred class name to the mutable Date implementation.
  58. *
  59. * @return $this
  60. */
  61. public function useMutable()
  62. {
  63. $this->_setClassName('Cake\I18n\Date', 'DateTime');
  64. return $this;
  65. }
  66. /**
  67. * Convert request data into a datetime object.
  68. *
  69. * @param mixed $value Request data
  70. * @return \DateTimeInterface
  71. */
  72. public function marshal($value)
  73. {
  74. $date = parent::marshal($value);
  75. if ($date instanceof DateTime || $date instanceof DateTimeImmutable) {
  76. $date = $date->setTime(0, 0, 0);
  77. }
  78. return $date;
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. protected function _parseValue($value)
  84. {
  85. /** @var \Cake\I18n\Time $class */
  86. $class = $this->_className;
  87. return $class::parseDate($value, $this->_localeFormat);
  88. }
  89. }