-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathORM.php
More file actions
70 lines (59 loc) · 2.18 KB
/
ORM.php
File metadata and controls
70 lines (59 loc) · 2.18 KB
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
61
62
63
64
65
66
67
68
69
70
<?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\SoftDeleteable\Mapping\Event\Adapter;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\FieldMapping;
use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
use Gedmo\Mapping\Event\ClockAwareAdapterInterface;
use Gedmo\SoftDeleteable\Mapping\Event\SoftDeleteableAdapter;
use Psr\Clock\ClockInterface;
/**
* Doctrine event adapter for ORM adapted
* for SoftDeleteable behavior.
*
* @author David Buchmann <mail@davidbu.ch>
*/
final class ORM extends BaseAdapterORM implements SoftDeleteableAdapter, ClockAwareAdapterInterface
{
private ?ClockInterface $clock = null;
public function setClock(ClockInterface $clock): void
{
$this->clock = $clock;
}
/**
* @param ClassMetadata<object> $meta
*/
public function getDateValue($meta, $field)
{
$mapping = $meta->getFieldMapping($field);
return $this->getObjectManager()->getConnection()->convertToPHPValue(
$this->getRawDateValue($mapping),
$mapping instanceof FieldMapping ? $mapping->type : ($mapping['type'] ?? Types::DATETIME_MUTABLE)
);
}
/**
* Generates current timestamp for the specified mapping
*
* @param array<string, mixed>|FieldMapping $mapping
*
* @return \DateTimeInterface|int
*/
private function getRawDateValue($mapping)
{
$datetime = $this->clock instanceof ClockInterface ? $this->clock->now() : new \DateTimeImmutable();
$type = $mapping instanceof FieldMapping ? $mapping->type : ($mapping['type'] ?? '');
if ('integer' === $type) {
return (int) $datetime->format('U');
}
if (in_array($type, ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable', 'date_point'], true)) {
return $datetime;
}
return \DateTime::createFromImmutable($datetime);
}
}