1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Sulu. |
5
|
|
|
* |
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sulu\Component\DocumentManager; |
13
|
|
|
|
14
|
|
|
class Metadata |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $class; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $alias; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $phpcrType; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \ReflectionClass |
33
|
|
|
*/ |
34
|
|
|
private $reflection; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $fieldMappings = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add a field mapping for field with given name, for example:. |
43
|
|
|
* |
44
|
|
|
* ```` |
45
|
|
|
* $metadata->addFieldMapping(array( |
46
|
|
|
* 'encoding' => 'content', |
47
|
|
|
* 'property' => 'phpcr_property_name', |
48
|
|
|
* )); |
49
|
|
|
* ```` |
50
|
|
|
* |
51
|
|
|
* @param string $name Name of field/property in the mapped class. |
52
|
|
|
* @param array $mapping { |
53
|
|
|
* |
54
|
|
|
* @var string Encoding type to use, @see \Sulu\Component\DocumentManager\PropertyEncoder::encode() |
55
|
|
|
* @var string PHPCR property name (excluding the prefix) |
56
|
|
|
* @var string Type of field (leave blank to determine automatically) |
57
|
|
|
* @var bool If the field should be mapped. Set to false to manually persist and hydrate the data. |
58
|
|
|
* } |
59
|
|
|
*/ |
60
|
|
|
public function addFieldMapping($name, $mapping) |
61
|
|
|
{ |
62
|
|
|
$mapping = array_merge([ |
63
|
|
|
'encoding' => 'content', |
64
|
|
|
'property' => $name, |
65
|
|
|
'type' => null, |
66
|
|
|
'mapped' => true, |
67
|
|
|
'multiple' => false, |
68
|
|
|
'default' => null, |
69
|
|
|
], $mapping); |
70
|
|
|
|
71
|
|
|
$this->fieldMappings[$name] = $mapping; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return all field mappings. |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getFieldMappings() |
80
|
|
|
{ |
81
|
|
|
return $this->fieldMappings; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Return the mapping for the named field. |
86
|
|
|
* |
87
|
|
|
* @see Metadata::addFieldMapping |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function getFieldMapping($name) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
if (!isset($this->fieldMappings[$name])) { |
94
|
|
|
throw new \InvalidArgumentException(sprintf( |
95
|
|
|
'Field mapping "%s" not known in metadata for document type "%s". Known mappings: "%s"', |
96
|
|
|
$name, $this->alias, implode('", "', array_keys($this->fieldMappings)) |
97
|
|
|
)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->fieldMappings[$name]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getClass() |
107
|
|
|
{ |
108
|
|
|
return $this->class; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $class |
113
|
|
|
*/ |
114
|
|
|
public function setClass($class) |
115
|
|
|
{ |
116
|
|
|
$this->class = $class; |
117
|
|
|
$this->reflection = null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return ReflectionClass |
122
|
|
|
*/ |
123
|
|
|
public function getReflectionClass() |
124
|
|
|
{ |
125
|
|
|
if ($this->reflection) { |
126
|
|
|
return $this->reflection; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (!$this->class) { |
130
|
|
|
throw new \InvalidArgumentException( |
131
|
|
|
'Cannot retrieve ReflectionClass on metadata which has no class attribute' |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$this->reflection = new \ReflectionClass($this->class); |
136
|
|
|
|
137
|
|
|
return $this->reflection; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getAlias() |
144
|
|
|
{ |
145
|
|
|
return $this->alias; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $alias |
150
|
|
|
*/ |
151
|
|
|
public function setAlias($alias) |
152
|
|
|
{ |
153
|
|
|
$this->alias = $alias; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function getPhpcrType() |
160
|
|
|
{ |
161
|
|
|
return $this->phpcrType; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $phpcrType |
166
|
|
|
*/ |
167
|
|
|
public function setPhpcrType($phpcrType) |
168
|
|
|
{ |
169
|
|
|
$this->phpcrType = $phpcrType; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.