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