-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathXml.php
More file actions
111 lines (100 loc) · 3.88 KB
/
Xml.php
File metadata and controls
111 lines (100 loc) · 3.88 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
107
108
109
110
111
<?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\Driver;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Gedmo\Exception\InvalidMappingException;
use Gedmo\Mapping\Driver\Xml as BaseXml;
/**
* This is a xml mapping driver for Timestampable
* behavioral extension. Used for extraction of extended
* metadata from xml specifically for Timestampable
* extension.
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
*
* @internal
*/
class Xml extends BaseXml
{
/**
* List of types which are valid for timestamp
*
* @var string[]
*/
private const VALID_TYPES = [
'date',
'date_immutable',
'time',
'time_immutable',
'datetime',
'datetime_immutable',
'datetimetz',
'datetimetz_immutable',
'timestamp',
'vardatetime',
'integer',
'date_point',
];
public function readExtendedMetadata($meta, array &$config)
{
/**
* @var \SimpleXmlElement
*/
$mapping = $this->_getMapping($meta->getName());
if (isset($mapping->field)) {
/**
* @var \SimpleXmlElement
*/
foreach ($mapping->field as $fieldMapping) {
$fieldMappingDoctrine = $fieldMapping;
$fieldMapping = $fieldMapping->children(self::GEDMO_NAMESPACE_URI);
if (isset($fieldMapping->timestampable)) {
/**
* @var \SimpleXmlElement
*/
$data = $fieldMapping->timestampable;
$field = $this->_getAttribute($fieldMappingDoctrine, 'name');
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 (!$this->_isAttributeSet($data, 'on') || !in_array($this->_getAttribute($data, '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' === $this->_getAttribute($data, 'on')) {
if (!$this->_isAttributeSet($data, 'field')) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->getName()}");
}
$trackedFieldAttribute = $this->_getAttribute($data, 'field');
$valueAttribute = $this->_isAttributeSet($data, 'value') ? $this->_getAttribute($data, 'value') : null;
$field = [
'field' => $field,
'trackedField' => $trackedFieldAttribute,
'value' => $valueAttribute,
];
}
$config[$this->_getAttribute($data, 'on')][] = $field;
}
}
}
return $config;
}
/**
* Checks if $field type is valid
*
* @param ClassMetadata<object> $meta
* @param string $field
*
* @return bool
*/
protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);
return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true);
}
}