-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathORM.php
60 lines (51 loc) · 1.83 KB
/
ORM.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Gedmo\Timestampable\Mapping\Event\Adapter;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadata;
use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
use Gedmo\Timestampable\Mapping\Event\TimestampableAdapter;
/**
* Doctrine event adapter for ORM adapted
* for Timestampable behavior
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
*/
final class ORM extends BaseAdapterORM implements TimestampableAdapter
{
/**
* @param ClassMetadata $meta
*/
public function getDateValue($meta, $field)
{
$mapping = $meta->getFieldMapping($field);
$converter = Type::getType($mapping['type'] ?? Types::DATETIME_MUTABLE);
$platform = $this->getObjectManager()->getConnection()->getDriver()->getDatabasePlatform();
return $converter->convertToPHPValue($this->getRawDateValue($mapping), $platform);
}
/**
* Generates current timestamp for the specified mapping
*
* @param array<string, mixed> $mapping
*
* @return \DateTimeInterface|int
*/
private function getRawDateValue(array $mapping)
{
$datetime = new \DateTime();
$type = $mapping['type'] ?? null;
if ('integer' === $type) {
return (int) $datetime->format('U');
}
if (in_array($type, ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) {
return \DateTimeImmutable::createFromMutable($datetime);
}
return $datetime;
}
}