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