DateType.php 2.4 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. /**
  18. * Class DateType
  19. */
  20. class DateType extends DateTimeType
  21. {
  22. /**
  23. * The class to use for representing date objects
  24. *
  25. * This property can only be used before an instance of this type
  26. * class is constructed. After that use `useMutable()` or `useImmutable()` instead.
  27. *
  28. * @var string
  29. * @deprecated 3.2.0 Use DateType::useMutable() or DateType::useImmutable() instead.
  30. */
  31. public static $dateTimeClass = 'Cake\I18n\Date';
  32. /**
  33. * Date format for DateTime object
  34. *
  35. * @var string|array
  36. */
  37. protected $_format = 'Y-m-d';
  38. /**
  39. * In this class we want Date objects to have their time
  40. * set to the beginning of the day.
  41. *
  42. * @var bool
  43. */
  44. protected $setToDateStart = true;
  45. /**
  46. * Change the preferred class name to the FrozenDate implementation.
  47. *
  48. * @return $this
  49. */
  50. public function useImmutable()
  51. {
  52. $this->_setClassName('Cake\I18n\FrozenDate', 'DateTimeImmutable');
  53. return $this;
  54. }
  55. /**
  56. * Change the preferred class name to the mutable Date implementation.
  57. *
  58. * @return $this
  59. */
  60. public function useMutable()
  61. {
  62. $this->_setClassName('Cake\I18n\Date', 'DateTime');
  63. return $this;
  64. }
  65. /**
  66. * Convert request data into a datetime object.
  67. *
  68. * @param mixed $value Request data
  69. * @return \DateTimeInterface
  70. */
  71. public function marshal($value)
  72. {
  73. $date = parent::marshal($value);
  74. if ($date instanceof DateTime) {
  75. $date->setTime(0, 0, 0);
  76. }
  77. return $date;
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. protected function _parseValue($value)
  83. {
  84. /* @var \Cake\I18n\Time $class */
  85. $class = $this->_className;
  86. return $class::parseDate($value, $this->_localeFormat);
  87. }
  88. }