1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jellyfish\Transfer\Definition; |
4
|
|
|
|
5
|
|
|
class ClassDefinition implements ClassDefinitionInterface |
6
|
|
|
{ |
7
|
|
|
public const NAMESPACE_PREFIX = 'Generated\\Transfer'; |
8
|
|
|
public const NAMESPACE_SEPARATOR = '\\'; |
9
|
|
|
public const FACTORY_NAME_SUFFIX = 'Factory'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $name; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string|null |
18
|
|
|
*/ |
19
|
|
|
protected $namespace; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Jellyfish\Transfer\Definition\ClassPropertyDefinition[] |
23
|
|
|
*/ |
24
|
|
|
protected $properties; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public function getId(): string |
30
|
|
|
{ |
31
|
|
|
$pattern = '/(?<=\\w)(?=[A-Z])/'; |
32
|
|
|
$replacement = '_$1'; |
33
|
|
|
$id = static::NAMESPACE_PREFIX; |
34
|
|
|
|
35
|
|
|
if ($this->namespace !== null) { |
36
|
|
|
$id .= $this->namespace; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$id .= $this->name; |
40
|
|
|
$id = \str_replace('\\', '', $id); |
41
|
|
|
$id = \preg_replace($pattern, $replacement, $id); |
42
|
|
|
|
43
|
|
|
return \strtolower($id); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getName(): string |
50
|
|
|
{ |
51
|
|
|
return $this->name; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $name |
56
|
|
|
* |
57
|
|
|
* @return \Jellyfish\Transfer\Definition\ClassDefinitionInterface |
58
|
|
|
*/ |
59
|
|
|
public function setName(string $name): ClassDefinitionInterface |
60
|
|
|
{ |
61
|
|
|
$this->name = $name; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string|null |
68
|
|
|
*/ |
69
|
|
|
public function getNamespace(): ?string |
70
|
|
|
{ |
71
|
|
|
return $this->namespace; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string|null $namespace |
76
|
|
|
* |
77
|
|
|
* @return \Jellyfish\Transfer\Definition\ClassDefinitionInterface |
78
|
|
|
*/ |
79
|
|
|
public function setNamespace(?string $namespace): ClassDefinitionInterface |
80
|
|
|
{ |
81
|
|
|
$this->namespace = $namespace; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getNamespaceStatement(): string |
90
|
|
|
{ |
91
|
|
|
return \sprintf('namespace %s\\%s;', static::NAMESPACE_PREFIX, $this->namespace); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function getUseStatements(): array |
98
|
|
|
{ |
99
|
|
|
$useStatements = []; |
100
|
|
|
|
101
|
|
|
foreach ($this->properties as $property) { |
102
|
|
|
if (!$this->canCreateUseStatement($property)) { |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$useStatement = $this->createUseStatement($property); |
107
|
|
|
$useStatementKey = \sha1($useStatement); |
108
|
|
|
|
109
|
|
|
if (array_key_exists($useStatementKey, $useStatements)) { |
110
|
|
|
continue; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$useStatements[$useStatementKey] = $useStatement; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $useStatements; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param \Jellyfish\Transfer\Definition\ClassPropertyDefinitionInterface $property |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
protected function canCreateUseStatement(ClassPropertyDefinitionInterface $property): bool |
125
|
|
|
{ |
126
|
|
|
return $property->isPrimitive() === false |
127
|
|
|
&& ($property->getTypeNamespace() !== $this->namespace || $property->getTypeAlias() !== null); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param \Jellyfish\Transfer\Definition\ClassPropertyDefinitionInterface $property |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
protected function createUseStatement(ClassPropertyDefinitionInterface $property): string |
136
|
|
|
{ |
137
|
|
|
if ($property->getTypeAlias() === null) { |
138
|
|
|
return \sprintf( |
139
|
|
|
'use %s\\%s\\%s;', |
140
|
|
|
static::NAMESPACE_PREFIX, |
141
|
|
|
$property->getTypeNamespace(), |
142
|
|
|
$property->getType() |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return \sprintf( |
147
|
|
|
'use %s\\%s\\%s as %s;', |
148
|
|
|
static::NAMESPACE_PREFIX, |
149
|
|
|
$property->getTypeNamespace(), |
150
|
|
|
$property->getType(), |
151
|
|
|
$property->getTypeAlias() |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return \Jellyfish\Transfer\Definition\ClassPropertyDefinition[] |
157
|
|
|
*/ |
158
|
|
|
public function getProperties(): array |
159
|
|
|
{ |
160
|
|
|
return $this->properties; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param \Jellyfish\Transfer\Definition\ClassPropertyDefinition[] $properties |
165
|
|
|
* |
166
|
|
|
* @return \Jellyfish\Transfer\Definition\ClassDefinitionInterface |
167
|
|
|
*/ |
168
|
|
|
public function setProperties(array $properties): ClassDefinitionInterface |
169
|
|
|
{ |
170
|
|
|
$this->properties = $properties; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|