|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AlgoWeb\ODataMetadata; |
|
4
|
|
|
|
|
5
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType; |
|
6
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx; |
|
7
|
|
|
|
|
8
|
|
|
class MetadataManager |
|
9
|
|
|
{ |
|
10
|
|
|
private $V3Edmx = null; |
|
11
|
|
|
private $oldEdmx = null; |
|
12
|
|
|
private $lastError = null; |
|
13
|
|
|
private $serializer = null; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct($namespaceName = "Data", $containerName = "DefaultContainer") |
|
16
|
|
|
{ |
|
17
|
|
|
$this->V3Edmx = new Edmx($namespaceName, $containerName); |
|
18
|
|
|
if (!$this->V3Edmx->isOK($msg)) { |
|
19
|
|
|
throw new \Exception($msg); |
|
20
|
|
|
} |
|
21
|
|
|
$ymlDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . "MetadataV3" . DIRECTORY_SEPARATOR . "JMSmetadata"; |
|
22
|
|
|
$this->serializer = |
|
23
|
|
|
\JMS\Serializer\SerializerBuilder::create() |
|
24
|
|
|
->addMetadataDir($ymlDir) |
|
25
|
|
|
->build(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getEdmx() |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->V3Edmx; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getEdmxXML() |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->serializer->serialize($this->V3Edmx, "xml"); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function addEntityType($name, $accessType = "Public") |
|
39
|
|
|
{ |
|
40
|
|
|
$this->startEdmxTransaction(); |
|
41
|
|
|
$NewEntity = new TEntityTypeType(); |
|
42
|
|
|
$NewEntity->setName($name); |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
$entitySet = new \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType(); |
|
46
|
|
|
$entitySet->setName($this->pluralize(2, $NewEntity->getName())); |
|
47
|
|
|
$namespace = $this->V3Edmx->getDataServices()[0]->getNamespace(); |
|
48
|
|
|
if (0 == strlen(trim($namespace))) { |
|
49
|
|
|
$entityTypeName = $NewEntity->getName(); |
|
50
|
|
|
} else { |
|
51
|
|
|
$entityTypeName = $namespace . "." . $NewEntity->getName(); |
|
52
|
|
|
} |
|
53
|
|
|
$entitySet->setEntityType($entityTypeName); |
|
54
|
|
|
$entitySet->setGetterAccess($accessType); |
|
55
|
|
|
|
|
56
|
|
|
$this->V3Edmx->getDataServices()[0]->addToEntityType($NewEntity); |
|
57
|
|
|
$this->V3Edmx->getDataServices()[0]->getEntityContainer()[0]->addToEntitySet($entitySet); |
|
58
|
|
|
if (!$this->V3Edmx->isok($this->lastError)) { |
|
59
|
|
|
$this->revertEdmxTransaction(); |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
$this->commitEdmxTransaction(); |
|
|
|
|
|
|
63
|
|
|
return $NewEntity; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function startEdmxTransaction() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->oldEdmx = serialize($this->V3Edmx); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Pluralizes a word if quantity is not one. |
|
73
|
|
|
* |
|
74
|
|
|
* @param int $quantity Number of items |
|
75
|
|
|
* @param string $singular Singular form of word |
|
76
|
|
|
* @param string $plural Plural form of word; function will attempt to deduce plural form from singular if not provided |
|
77
|
|
|
* @return string Pluralized word if quantity is not one, otherwise singular |
|
78
|
|
|
*/ |
|
79
|
|
View Code Duplication |
public static function pluralize($quantity, $singular, $plural = null) |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
if ($quantity == 1 || !strlen($singular)) return $singular; |
|
82
|
|
|
if ($plural !== null) return $plural; |
|
83
|
|
|
|
|
84
|
|
|
$last_letter = strtolower($singular[strlen($singular) - 1]); |
|
85
|
|
|
switch ($last_letter) { |
|
86
|
|
|
case 'y': |
|
87
|
|
|
return substr($singular, 0, -1) . 'ies'; |
|
88
|
|
|
case 's': |
|
89
|
|
|
return $singular . 'es'; |
|
90
|
|
|
default: |
|
91
|
|
|
return $singular . 's'; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
private function revertEdmxTransaction() |
|
96
|
|
|
{ |
|
97
|
|
|
$this->V3Edmx = unserialize($this->oldEdmx); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function commitEdmxTransaction() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->oldEdmx == null; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function addComplexType(\ReflectionClass $refClass, $name, $namespace = null, $baseResourceType = null) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->createResourceType($refClass, $name, $namespace, ResourceTypeKind::COMPLEX, $baseResourceType); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function getLastError() |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
return $this->lastError(); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: