|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PhpGenerator\Element; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
|
|
9
|
|
|
class PhpAnnotationBlock extends AbstractElement |
|
10
|
|
|
{ |
|
11
|
52 |
|
public function __construct(array $annotations = []) |
|
12
|
|
|
{ |
|
13
|
52 |
|
parent::__construct('_'); |
|
14
|
52 |
|
$this->setAnnotations($annotations); |
|
15
|
50 |
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param mixed $child |
|
19
|
|
|
* |
|
20
|
|
|
* @throws InvalidArgumentException |
|
21
|
|
|
*/ |
|
22
|
14 |
|
public function addChild($child): self |
|
23
|
|
|
{ |
|
24
|
14 |
|
if (!$this->childIsValid($child)) { |
|
25
|
2 |
|
parent::addChild($child); |
|
26
|
|
|
|
|
27
|
12 |
|
return $this; |
|
28
|
|
|
} |
|
29
|
10 |
|
$this->children[] = $this->transformAnnotation($child); |
|
30
|
|
|
|
|
31
|
|
|
return $this; |
|
32
|
36 |
|
} |
|
33
|
|
|
|
|
34
|
36 |
|
public function getPhpDeclaration(): string |
|
35
|
|
|
{ |
|
36
|
|
|
return ''; |
|
37
|
14 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getChildrenTypes(): array |
|
40
|
14 |
|
{ |
|
41
|
|
|
return [ |
|
42
|
|
|
'array', |
|
43
|
|
|
'string', |
|
44
|
|
|
PhpAnnotation::class, |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
36 |
|
* Allows to generate content before children content is generated. |
|
50
|
|
|
*/ |
|
51
|
36 |
|
public function getLineBeforeChildren(?int $indentation = null): string |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->getIndentedString(parent::OPEN_ANNOTATION, $indentation); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
36 |
|
* Allows to generate content after children content is generated. |
|
58
|
|
|
*/ |
|
59
|
36 |
|
public function getLineAfterChildren(?int $indentation = null): string |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->getIndentedString(parent::CLOSE_ANNOTATION, $indentation); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param array[]|PhpAnnotation[]|string[] $annotations |
|
66
|
|
|
* |
|
67
|
52 |
|
* @throws InvalidArgumentException |
|
68
|
|
|
*/ |
|
69
|
52 |
|
protected function setAnnotations(array $annotations): self |
|
70
|
2 |
|
{ |
|
71
|
|
|
if (!static::annotationsAreValid($annotations)) { |
|
72
|
50 |
|
throw new InvalidArgumentException('Annotations are not valid'); |
|
73
|
|
|
} |
|
74
|
50 |
|
$this->children = static::transformAnnotations($annotations); |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param array[]|PhpAnnotation[]|string[] $annotations |
|
81
|
|
|
* |
|
82
|
50 |
|
* @return PhpAnnotation[] |
|
83
|
|
|
*/ |
|
84
|
50 |
|
protected static function transformAnnotations(array $annotations): array |
|
85
|
50 |
|
{ |
|
86
|
42 |
|
$finalAnnotations = []; |
|
87
|
|
|
foreach ($annotations as $annotation) { |
|
88
|
|
|
$finalAnnotations[] = static::transformAnnotation($annotation); |
|
89
|
50 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $finalAnnotations; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param array|PhpAnnotation|string $annotation |
|
96
|
|
|
* |
|
97
|
50 |
|
* @throws InvalidArgumentException |
|
98
|
|
|
*/ |
|
99
|
50 |
|
protected static function transformAnnotation($annotation): PhpAnnotation |
|
100
|
28 |
|
{ |
|
101
|
|
|
if ($annotation instanceof PhpAnnotation) { |
|
102
|
42 |
|
return $annotation; |
|
103
|
38 |
|
} |
|
104
|
|
|
if (is_string($annotation)) { |
|
105
|
4 |
|
return new PhpAnnotation(PhpAnnotation::NO_NAME, $annotation); |
|
106
|
2 |
|
} |
|
107
|
|
|
if (is_array($annotation) && array_key_exists('content', $annotation)) { |
|
108
|
|
|
return new PhpAnnotation(array_key_exists('name', $annotation) ? $annotation['name'] : PhpAnnotation::NO_NAME, $annotation['content']); |
|
109
|
2 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
throw new InvalidArgumentException(sprintf('Annotation parameter "%s" is invalid', gettype($annotation))); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
52 |
|
* @param array[]|PhpAnnotation[]|string[] $annotations |
|
116
|
|
|
*/ |
|
117
|
52 |
|
protected static function annotationsAreValid(array $annotations): bool |
|
118
|
52 |
|
{ |
|
119
|
44 |
|
$valid = true; |
|
120
|
|
|
foreach ($annotations as $annotation) { |
|
121
|
|
|
$valid &= static::annotationIsValid($annotation); |
|
122
|
52 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
return (bool) $valid; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
44 |
|
* @param array|PhpAnnotation|string $annotation |
|
129
|
|
|
*/ |
|
130
|
44 |
|
protected static function annotationIsValid($annotation): bool |
|
131
|
|
|
{ |
|
132
|
|
|
return static::stringIsValid($annotation, false) || (is_array($annotation) && array_key_exists('content', $annotation)) || $annotation instanceof PhpAnnotation; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|