-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathODM.php
52 lines (43 loc) · 1.58 KB
/
ODM.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
<?php
/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <[email protected]> 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\ODM\MongoDB\Mapping\ClassMetadata;
use Gedmo\Mapping\Event\Adapter\ODM as BaseAdapterODM;
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 <[email protected]>
*/
final class ODM extends BaseAdapterODM 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)
{
$datetime = $this->clock instanceof ClockInterface ? $this->clock->now() : new \DateTimeImmutable();
$mapping = $meta->getFieldMapping($field);
$type = $mapping['type'] ?? null;
if ('timestamp' === $type) {
return (int) $datetime->format('U');
}
if (in_array($type, ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) {
return $datetime;
}
return \DateTime::createFromImmutable($datetime);
}
}