DateTimeType.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Type;
  16. use Cake\Database\Driver;
  17. /**
  18. * Datetime type converter.
  19. *
  20. * Use to convert datetime instances to strings & back.
  21. */
  22. class DateTimeType extends \Cake\Database\Type {
  23. /**
  24. * The class to use for representing date objects
  25. *
  26. * @var string
  27. */
  28. public static $dateTimeClass = 'Cake\Utility\Time';
  29. /**
  30. * String format to use for DateTime parsing
  31. *
  32. * @var string
  33. */
  34. protected $_format = 'Y-m-d H:i:s';
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function __construct($name = null) {
  39. parent::__construct($name);
  40. if (!class_exists(static::$dateTimeClass)) {
  41. static::$dateTimeClass = 'DateTime';
  42. }
  43. }
  44. /**
  45. * Convert DateTime instance into strings.
  46. *
  47. * @param string|int|\DateTime $value The value to convert.
  48. * @param Driver $driver The driver instance to convert with.
  49. * @return string
  50. */
  51. public function toDatabase($value, Driver $driver) {
  52. if ($value === null || is_string($value)) {
  53. return $value;
  54. }
  55. if (is_int($value)) {
  56. $value = new static::$dateTimeClass('@' . $value);
  57. }
  58. return $value->format($this->_format);
  59. }
  60. /**
  61. * Convert strings into DateTime instances.
  62. *
  63. * @param string $value The value to convert.
  64. * @param Driver $driver The driver instance to convert with.
  65. * @return \Carbon\Carbon
  66. */
  67. public function toPHP($value, Driver $driver) {
  68. if ($value === null) {
  69. return null;
  70. }
  71. list($value) = explode('.', $value);
  72. $class = static::$dateTimeClass;
  73. return $class::createFromFormat($this->_format, $value);
  74. }
  75. /**
  76. * Convert request data into a datetime object.
  77. *
  78. * @param mixed $value Request data
  79. * @return \Carbon\Carbon
  80. */
  81. public function marshal($value) {
  82. if ($value instanceof \DateTime) {
  83. return $value;
  84. }
  85. $class = static::$dateTimeClass;
  86. try {
  87. if ($value === '' || $value === null || $value === false || $value === true) {
  88. return $value;
  89. } elseif (is_numeric($value)) {
  90. $date = new $class('@' . $value);
  91. } elseif (is_string($value)) {
  92. $date = new $class($value);
  93. }
  94. if (isset($date)) {
  95. return $date;
  96. }
  97. } catch (\Exception $e) {
  98. return $value;
  99. }
  100. $value += ['hour' => 0, 'minute' => 0, 'second' => 0];
  101. $format = '';
  102. if (
  103. isset($value['year'], $value['month'], $value['day']) &&
  104. (is_numeric($value['year']) & is_numeric($value['month']) && is_numeric($value['day']))
  105. ) {
  106. $format .= sprintf('%d-%02d-%02d', $value['year'], $value['month'], $value['day']);
  107. }
  108. if (isset($value['meridian'])) {
  109. $value['hour'] = strtolower($value['meridian']) === 'am' ? $value['hour'] : $value['hour'] + 12;
  110. }
  111. $format .= sprintf('%02d:%02d:%02d', $value['hour'], $value['minute'], $value['second']);
  112. return new $class($format);
  113. }
  114. }