1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlgoWeb\ODataMetadata\MetadataV3\edm; |
4
|
|
|
|
5
|
|
|
use AlgoWeb\ODataMetadata\IsOK; |
6
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\Groups\GSchemaBodyElementsTrait; |
7
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\IsOKTraits\TNamespaceNameTrait; |
8
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\IsOKTraits\TSimpleIdentifierTrait; |
9
|
|
|
use AlgoWeb\ODataMetadata\StringTraits\XSDTopLevelTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class representing TSchemaType |
13
|
|
|
* |
14
|
|
|
* |
15
|
|
|
* XSD Type: TSchema |
16
|
|
|
*/ |
17
|
|
|
class TSchemaType extends IsOK |
18
|
|
|
{ |
19
|
|
|
use GSchemaBodyElementsTrait, TSimpleIdentifierTrait, XSDTopLevelTrait, TNamespaceNameTrait { |
20
|
|
|
TSimpleIdentifierTrait::isNCName insteadof TNamespaceNameTrait; |
21
|
|
|
TSimpleIdentifierTrait::matchesRegexPattern insteadof TNamespaceNameTrait; |
22
|
|
|
TSimpleIdentifierTrait::isName insteadof TNamespaceNameTrait; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
} |
26
|
|
|
/** |
27
|
|
|
* @property string $namespace |
28
|
|
|
*/ |
29
|
|
|
private $namespace = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @property string $namespaceUri |
33
|
|
|
*/ |
34
|
|
|
private $namespaceUri = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @property string $alias |
38
|
|
|
*/ |
39
|
|
|
private $alias = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Gets as namespaceUri |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getNamespaceUri() |
47
|
|
|
{ |
48
|
|
|
return $this->namespaceUri; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sets a new namespaceUri |
53
|
|
|
* |
54
|
|
|
* @param string $namespaceUri |
55
|
|
|
* @return self |
56
|
|
|
*/ |
57
|
|
|
public function setNamespaceUri($namespaceUri) |
58
|
|
|
{ |
59
|
|
|
if (null != $namespaceUri && !$this->isURLValid($namespaceUri)) { |
60
|
|
|
$msg = "Namespace url must be a valid url"; |
61
|
|
|
throw new \InvalidArgumentException($msg); |
62
|
|
|
} |
63
|
|
|
$this->namespaceUri = $namespaceUri; |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets as alias |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function getAlias() |
73
|
|
|
{ |
74
|
|
|
return $this->alias; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Sets a new alias |
79
|
|
|
* |
80
|
|
|
* @param string $alias |
81
|
|
|
* @return self |
82
|
|
|
*/ |
83
|
|
|
public function setAlias($alias) |
84
|
|
|
{ |
85
|
|
|
if (null != $alias && !$this->isTSimpleIdentifierValid($alias)) { |
86
|
|
|
$msg = "Alias must be a valid TSimpleIdentifier"; |
87
|
|
|
throw new \InvalidArgumentException($msg); |
88
|
|
|
} |
89
|
|
|
$this->alias = $alias; |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function isOK(&$msg = null) |
94
|
|
|
{ |
95
|
|
|
if (!$this->isTNamespaceNameValid($this->namespace)) { |
96
|
|
|
$msg = "Namespace must be a valid TNamespaceName"; |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
if (null != $this->namespaceUri && !$this->isURLValid($this->namespaceUri)) { |
100
|
|
|
$msg = "Namespace url must be a valid url"; |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
View Code Duplication |
if (null != $this->alias && !$this->isTSimpleIdentifierValid($this->alias)) { |
|
|
|
|
104
|
|
|
$msg = "Alias must be a valid TSimpleIdentifier"; |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
if (!$this->isGSchemaBodyElementsValid($msg)) { |
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
return $this->isStructureOK($msg); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function isStructureOK(&$msg = null) |
114
|
|
|
{ |
115
|
|
|
$entityTypeNames = []; |
116
|
|
|
$associationNames = []; |
117
|
|
|
$this->getEntityType(); |
|
|
|
|
118
|
|
|
foreach ($this->getEntityType() as $entityType) { |
119
|
|
|
$entityTypeNames[] = $entityType->getName(); |
120
|
|
|
} |
121
|
|
|
foreach ($this->association as $association) { |
122
|
|
|
$associationNames[] = $association->getName(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$entitySets = $this->getEntityContainer()[0]->getEntitySet(); |
126
|
|
|
foreach ($entitySets as $eset) { |
127
|
|
|
$eSetType = $eset->getEntityType(); |
128
|
|
|
/*if (substr($eSetType, 0, strlen($this->getNamespace())) != $this->getNamespace()) { |
|
|
|
|
129
|
|
|
$msg = "Types for Entity Sets should have the namespace at the beginning " . __CLASS__; |
130
|
|
|
die($this->getNamespace()); |
131
|
|
|
return false; |
132
|
|
|
}*/ |
133
|
|
|
$eSetType = str_replace($this->getNamespace() . ".", "", $eSetType); |
134
|
|
|
if (!in_array($eSetType, $entityTypeNames)) { |
135
|
|
|
$msg = "entitySet Types should have a matching type name in entity Types"; |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Check Associations to assocationSets |
141
|
|
|
$found = false; |
142
|
|
|
$namespaceLen = strlen($this->namespace); |
143
|
|
|
if (0 != $namespaceLen) { |
144
|
|
|
$namespaceLen++; |
145
|
|
|
} |
146
|
|
|
foreach ($this->association as $assocation) { |
147
|
|
|
foreach ($this->entityContainer->associationSet as $assocationSet) { |
148
|
|
|
if ($assocation->getName() == substr($assocationSet->association, $namespaceLen)) { |
149
|
|
|
$aEnd1 = $assocationSet->getEnd()[0]; |
150
|
|
|
$aEnd2 = $assocationSet->getEnd()[1]; |
151
|
|
|
$asEnd1 = $assocationSet->getEnd()[0]; |
152
|
|
|
$asEnd2 = $assocationSet->getEnd()[1]; |
153
|
|
|
//== TRUE if $a and $b have the same key/value pairs. |
154
|
|
|
//===TRUE if $a and $b have the same key/value pairs in the same order and of the same types. |
155
|
|
|
if ([$aEnd1->getRole(), $aEnd2->getRole()] == [$asEnd1->getRole(), $asEnd2->getRole()]) { |
156
|
|
|
$found = true; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
if (!$found) { |
161
|
|
|
$msg = "can not find assocationset with matching roles for assocation"; |
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
$found = false; |
165
|
|
|
} |
166
|
|
|
return true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Gets as namespace |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function getNamespace() |
175
|
|
|
{ |
176
|
|
|
return $this->namespace; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Sets a new namespace |
181
|
|
|
* |
182
|
|
|
* @param string $namespace |
183
|
|
|
* @return self |
184
|
|
|
*/ |
185
|
|
|
public function setNamespace($namespace) |
186
|
|
|
{ |
187
|
|
|
if (!$this->isTNamespaceNameValid($namespace)) { |
188
|
|
|
$msg = "Namespace must be a valid TNamespaceName"; |
189
|
|
|
throw new \InvalidArgumentException($msg); |
190
|
|
|
} |
191
|
|
|
$this->namespace = $namespace; |
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
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.