-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathClassPropertyParser.php
185 lines (160 loc) · 5.57 KB
/
ClassPropertyParser.php
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
declare(strict_types=1);
namespace PhpKafka\PhpAvroSchemaGenerator\Parser;
use PhpKafka\PhpAvroSchemaGenerator\Avro\Avro;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
use PhpParser\Comment\Doc;
use PhpParser\Node\Identifier;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\UnionType;
use RuntimeException;
class ClassPropertyParser implements ClassPropertyParserInterface
{
private DocCommentParserInterface $docParser;
/**
* @param DocCommentParserInterface $docParser
*/
public function __construct(DocCommentParserInterface $docParser)
{
$this->docParser = $docParser;
}
/**
* @param Property|mixed $property
* @return PhpClassPropertyInterface
*/
public function parseProperty($property): PhpClassPropertyInterface
{
if (false === $property instanceof Property) {
throw new RuntimeException(sprintf('Property must be of type: %s', Property::class));
}
$propertyAttributes = $this->getPropertyAttributes($property);
return new PhpClassProperty(
$propertyAttributes['name'],
$propertyAttributes['types'],
$propertyAttributes['default'],
$propertyAttributes['doc'],
$propertyAttributes['logicalType']
);
}
/**
* @param Property $property
* @return array<string, mixed>
*/
private function getPropertyAttributes(Property $property): array
{
$attributes = $this->getEmptyAttributesArray();
$docComments = $this->getAllPropertyDocComments($property);
$attributes['name'] = $this->getPropertyName($property);
$attributes['types'] = $this->getTypeFromDocComment($docComments);
if (null === $attributes['types']) {
$attributes['types'] = $this->getPropertyType($property, $docComments);
}
$attributes['default'] = $this->getDefaultFromDocComment($docComments);
$attributes['doc'] = $this->getDocFromDocComment($docComments);
$attributes['logicalType'] = $this->getLogicalTypeFromDocComment($docComments);
return $attributes;
}
private function getPropertyName(Property $property): string
{
return $property->props[0]->name->name;
}
/**
* @param Property $property
* @param array<string,mixed> $docComments
* @return string
*/
private function getPropertyType(Property $property, array $docComments): string
{
if ($property->type instanceof NullableType) {
if ($property->type->type instanceof Identifier) {
$type = Avro::MAPPED_TYPES[$property->type->type->name] ?? $property->type->type->name;
return 'null|' . $type;
}
} elseif ($property->type instanceof Identifier) {
return Avro::MAPPED_TYPES[$property->type->name] ?? $property->type->name;
} elseif ($property->type instanceof UnionType) {
$types = '';
$separator = '';
/** @var Identifier $type */
foreach ($property->type->types as $type) {
$type = Avro::MAPPED_TYPES[$type->name] ?? $type->name;
$types .= $separator . $type;
$separator = '|';
}
return $types;
}
return $this->getDocCommentByType($docComments, 'var') ?? 'string';
}
/**
* @param array<string, mixed> $docComments
* @return mixed
*/
private function getDocCommentByType(array $docComments, string $type)
{
return $docComments[$type] ?? null;
}
/**
* @param array<string, mixed> $docComments
* @return string|null
*/
private function getTypeFromDocComment(array $docComments): ?string
{
return $docComments['avro-type'] ?? null;
}
/**
* @param array<string, mixed> $docComments
* @return string
*/
private function getDefaultFromDocComment(array $docComments): string
{
return $docComments['avro-default'] ?? PhpClassPropertyInterface::NO_DEFAULT;
}
/**
* @param array<string, mixed> $docComments
* @return string|null
*/
private function getLogicalTypeFromDocComment(array $docComments): ?string
{
return $docComments['avro-logical-type'] ?? null;
}
/**
* @param array<string, mixed> $docComments
* @return string|null
*/
private function getDocFromDocComment(array $docComments): ?string
{
return $docComments['avro-doc'] ?? $docComments[DocCommentParserInterface::DOC_DESCRIPTION] ?? null;
}
/**
* @param Property $property
* @return array<string, mixed>
*/
private function getAllPropertyDocComments(Property $property): array
{
$docComments = [];
foreach ($property->getAttributes() as $attributeName => $attributeValue) {
if ('comments' === $attributeName) {
/** @var Doc $comment */
foreach ($attributeValue as $comment) {
$docComments = array_merge($docComments, $this->docParser->parseDoc($comment->getText()));
}
}
}
return $docComments;
}
/**
* @return array<string,null|string>
*/
private function getEmptyAttributesArray(): array
{
return [
'name' => null,
'types' => null,
'default' => PhpClassPropertyInterface::NO_DEFAULT,
'logicalType' => null,
'doc' => null
];
}
}