1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flagbit\Plantuml\TokenReflection; |
4
|
|
|
|
5
|
|
|
use TokenReflection\IReflectionProperty; |
6
|
|
|
|
7
|
|
|
class PropertyWriter extends WriterAbstract |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param IReflectionProperty $property |
11
|
|
|
* |
12
|
|
|
* @return string |
13
|
|
|
*/ |
14
|
8 |
|
public function writeElement(IReflectionProperty $property) |
15
|
|
|
{ |
16
|
8 |
|
return $this->formatLine($this->writeVisibility($property) . $property->getName() |
17
|
8 |
|
. $this->writeType($property) . $this->writeValue($property)); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param IReflectionProperty[] $properties |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
10 |
|
public function writeElements(array $properties) |
26
|
|
|
{ |
27
|
|
|
// see https://bugs.php.net/bug.php?id=50688 |
28
|
10 |
|
@usort($properties, function(IReflectionProperty $a, IReflectionProperty $b) { |
|
|
|
|
29
|
3 |
|
return strnatcasecmp($a->getName(), $b->getName()); |
30
|
10 |
|
}); |
31
|
|
|
|
32
|
10 |
|
$propertiesString = ''; |
33
|
10 |
|
foreach ($properties as $property) { |
34
|
|
|
/** @var $property IReflectionProperty */ |
35
|
3 |
|
$propertiesString .= $this->writeElement($property); |
36
|
10 |
|
} |
37
|
10 |
|
return $propertiesString; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param IReflectionProperty $property |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
8 |
|
public function writeVisibility(IReflectionProperty $property) |
46
|
|
|
{ |
47
|
8 |
|
return $property->isPrivate() ? '-' : ($property->isProtected() ? '#' : '+'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param IReflectionProperty $property |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
8 |
View Code Duplication |
public function writeType(IReflectionProperty $property) |
|
|
|
|
56
|
|
|
{ |
57
|
8 |
|
$type = ''; |
58
|
8 |
|
preg_match('/\*\h+@var\h+([^\h]+)/', (string) $property->getDocComment(), $matches); |
59
|
8 |
|
if (isset($matches[1])) { |
60
|
4 |
|
$type = $matches[1]; |
61
|
4 |
|
if ($property->getDeclaringClass()) { |
62
|
2 |
|
$type = $this->expandNamespaceAlias($property->getDeclaringClass(), $type); |
63
|
2 |
|
} |
64
|
4 |
|
$type = ' : ' . $this->formatClassName($type); |
65
|
4 |
|
} |
66
|
8 |
|
return $type; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param IReflectionProperty $property |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
8 |
|
public function writeValue(IReflectionProperty $property) |
75
|
|
|
{ |
76
|
8 |
|
$value = ''; |
77
|
8 |
|
if ($property->getDeclaringClass() && $defaultProperties = $property->getDeclaringClass()->getDefaultProperties()) { |
78
|
3 |
|
if (!is_null($defaultProperties[$property->getName()])) { |
79
|
2 |
|
$value = ' = ' . $this->formatValue($defaultProperties[$property->getName()]); |
80
|
2 |
|
} |
81
|
3 |
|
} |
82
|
8 |
|
return $value; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: