-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathAttribute.php
More file actions
106 lines (89 loc) · 3.76 KB
/
Attribute.php
File metadata and controls
106 lines (89 loc) · 3.76 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?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\Timestampable\Mapping\Driver;
use Gedmo\Exception\InvalidMappingException;
use Gedmo\Mapping\Annotation\Timestampable;
use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
/**
* Mapping driver for the timestampable extension which reads extended metadata from attributes on a timestampable class.
*
* @author Gediminas Morkevicius <[email protected]>
* @author Kevin Mian Kraiker <[email protected]>
*
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*
* @internal
*/
class Attribute extends AbstractAnnotationDriver
{
/**
* Mapping object for the timestampable extension.
*/
public const TIMESTAMPABLE = Timestampable::class;
/**
* List of types which are valid for timestamp
*
* @var string[]
*/
protected $validTypes = [
'date',
'date_immutable',
'time',
'time_immutable',
'datetime',
'datetime_immutable',
'datetimetz',
'datetimetz_immutable',
'timestamp',
'vardatetime',
'integer',
'date_point',
];
public function readExtendedMetadata($meta, array &$config)
{
$class = $this->getMetaReflectionClass($meta);
// property annotations
foreach ($class->getProperties() as $property) {
if ($meta->isMappedSuperclass && !$property->isPrivate()
|| $meta->isInheritedField($property->name)
|| isset($meta->associationMappings[$property->name]['inherited'])
) {
continue;
}
if ($timestampable = $this->reader->getPropertyAnnotation($property, self::TIMESTAMPABLE)) {
\assert($timestampable instanceof Timestampable);
$field = $property->getName();
if (!$meta->hasField($field)) {
throw new InvalidMappingException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->getName()}");
}
if (!$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->getName()}");
}
if (!in_array($timestampable->on, ['update', 'create', 'change'], true)) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->getName()}");
}
if ('change' === $timestampable->on) {
if (!isset($timestampable->field)) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->getName()}");
}
if (is_array($timestampable->field) && isset($timestampable->value)) {
throw new InvalidMappingException('Timestampable extension does not support multiple value changeset detection yet.');
}
$field = [
'field' => $field,
'trackedField' => $timestampable->field,
'value' => $timestampable->value,
];
}
// properties are unique and mapper checks that, no risk here
$config[$timestampable->on][] = $field;
}
}
return $config;
}
}