1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\PhpCodeBuilder; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Swaggest\PhpCodeBuilder\Traits\Description; |
7
|
|
|
|
8
|
|
|
class PhpNamedVar |
9
|
|
|
{ |
10
|
|
|
use Description; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
private $name; |
14
|
|
|
|
15
|
|
|
/** @var PhpAnyType|null */ |
16
|
|
|
private $type; |
17
|
|
|
|
18
|
|
|
/** @var bool */ |
19
|
|
|
private $hasDefault; |
20
|
|
|
|
21
|
|
|
/** @var mixed */ |
22
|
|
|
private $default; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* PhpNamedVar constructor. |
26
|
|
|
* @param string $name |
27
|
|
|
* @param PhpAnyType $type |
28
|
|
|
* @param bool $hasDefault |
29
|
|
|
* @param null $default |
|
|
|
|
30
|
|
|
*/ |
31
|
13 |
|
public function __construct($name, PhpAnyType $type = null, $hasDefault = false, $default = null) |
32
|
|
|
{ |
33
|
13 |
|
$this->name = $name; |
34
|
13 |
|
$this->type = $type; |
35
|
13 |
|
$this->hasDefault = $hasDefault; |
36
|
13 |
|
$this->default = $default; |
37
|
13 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
13 |
|
public function getName() |
43
|
|
|
{ |
44
|
13 |
|
return $this->name; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $name |
49
|
|
|
* @return PhpNamedVar |
50
|
|
|
*/ |
51
|
|
|
public function setName($name) |
52
|
|
|
{ |
53
|
|
|
$this->name = $name; |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return PhpAnyType|null |
59
|
|
|
*/ |
60
|
|
|
public function getType() |
61
|
|
|
{ |
62
|
|
|
return $this->type; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param PhpAnyType $type |
67
|
|
|
* @return PhpNamedVar |
68
|
|
|
*/ |
69
|
|
|
public function setType($type) |
70
|
|
|
{ |
71
|
|
|
$this->type = $type; |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param mixed $value |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function setDefault($value) |
80
|
|
|
{ |
81
|
|
|
$this->hasDefault = true; |
82
|
|
|
$this->default = $value; |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function clearDefault() |
90
|
|
|
{ |
91
|
|
|
$this->hasDefault = false; |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
13 |
|
public function renderDefault() |
96
|
|
|
{ |
97
|
13 |
|
if (!$this->hasDefault) { |
98
|
13 |
|
return ''; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($this->default instanceof PhpTemplate) { |
102
|
|
|
$result = $this->default->render(); |
103
|
|
|
} else { |
104
|
|
|
$result = PhpCode::varExport($this->default); |
105
|
|
|
if (strpos($result, '::__set_state(') !== false || strpos($result, '(object)') !== false) { |
106
|
|
|
return ''; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
13 |
|
return ' = ' . $result; |
111
|
|
|
} |
112
|
13 |
|
|
113
|
12 |
|
public function renderArgumentType() |
114
|
|
|
{ |
115
|
12 |
|
if ($this->type && $type = $this->type->renderArgumentType()) { |
116
|
|
|
return $type . ' '; |
117
|
|
|
} |
118
|
13 |
|
return ''; |
119
|
|
|
} |
120
|
13 |
|
|
121
|
13 |
|
public function renderPhpDocValue($withName = false) |
122
|
13 |
|
{ |
123
|
|
|
$tagValue = ''; |
124
|
13 |
|
$phpDocType = ''; |
125
|
13 |
|
if ($this->type) { |
126
|
|
|
$phpDocType = $this->type->renderPhpDocType(); |
127
|
13 |
|
} |
128
|
3 |
|
if (!trim($phpDocType)) { |
129
|
|
|
$phpDocType = PhpStdType::TYPE_MIXED; |
130
|
13 |
|
} |
131
|
|
|
$tagValue .= $phpDocType; |
132
|
|
|
if ($withName) { |
133
|
|
|
$tagValue .= ' $' . $this->name; |
134
|
|
|
} |
135
|
|
|
if ($this->description) { |
136
|
|
|
$tagValue .= ' ' . $this->description; |
137
|
|
|
} |
138
|
|
|
return trim($tagValue); |
139
|
|
|
} |
140
|
|
|
} |