‪TYPO3CMS  ‪main
TypeDatetimeFormatTime.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Doctrine\DBAL\Types\BigIntType;
21 use Doctrine\DBAL\Types\IntegerType;
26 
33 {
39  protected ‪$matchArray = [
40  'fieldConfig' => [
41  'config' => [
42  'type' => 'datetime',
43  'format' => 'time',
44  ],
45  ],
46  ];
47 
54  public function ‪generate(array $data): string
55  {
56  // 05:23
57  $value = '19380';
58 
59  // If database field is configured as integer field type, keep the integer-like value.
60  $tableSchemaInformation = GeneralUtility::makeInstance(ConnectionPool::class)
61  ->getConnectionForTable($data['tableName'])
62  ->getSchemaInformation()
63  ->introspectTable($data['tableName']);
64  if ($tableSchemaInformation->hasColumn($data['fieldName'])
65  && ($tableSchemaInformation->getColumn($data['fieldName'])->getType() instanceof IntegerType ||
66  $tableSchemaInformation->getColumn($data['fieldName'])->getType() instanceof BigIntType)
67  ) {
68  return $value;
69  }
70 
71  // We need to partly do the same work as the DataHandler for some dbTypes for the DateTime type to get
72  // database compatible values. Without it, we will get invalid format database exception when inserted.
73  // See \TYPO3\CMS\Core\DataHandling\DataHandler::checkValueForDatetime().
74  $nativeDateTimeType = $data['fieldConfig']['config']['dbType'] ?? '';
75  $dateTimeFormats = ‪QueryHelper::getDateTimeFormats();
76  $nativeDateTimeFieldFormat = $dateTimeFormats[$nativeDateTimeType]['format'] ?? 'h:i:s';
77  $value = gmdate($nativeDateTimeFieldFormat, (int)$value);
78 
79  return $value;
80  }
81 }
‪TYPO3\CMS\Core\Database\Query\QueryHelper\getDateTimeFormats
‪static array getDateTimeFormats()
Definition: QueryHelper.php:183
‪TYPO3\CMS\Styleguide\TcaDataGenerator\FieldGenerator\TypeDatetimeFormatTime\$matchArray
‪array $matchArray
Definition: TypeDatetimeFormatTime.php:38
‪TYPO3\CMS\Styleguide\TcaDataGenerator\FieldGenerator\TypeDatetimeFormatTime\generate
‪string generate(array $data)
Definition: TypeDatetimeFormatTime.php:53
‪TYPO3\CMS\Styleguide\TcaDataGenerator\FieldGenerator\AbstractFieldGenerator
Definition: AbstractFieldGenerator.php:29
‪TYPO3\CMS\Core\Database\Query\QueryHelper
Definition: QueryHelper.php:32
‪TYPO3\CMS\Styleguide\TcaDataGenerator\FieldGenerator
Definition: AbstractFieldGenerator.php:18
‪TYPO3\CMS\Styleguide\TcaDataGenerator\FieldGenerator\TypeDatetimeFormatTime
Definition: TypeDatetimeFormatTime.php:33
‪TYPO3\CMS\Styleguide\TcaDataGenerator\FieldGeneratorInterface
Definition: FieldGeneratorInterface.php:26
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52