1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\PhpCodeBuilder; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Swaggest\PhpCodeBuilder\Traits\Description; |
7
|
|
|
|
8
|
|
|
abstract class PhpClassTraitInterface extends PhpTemplate implements PhpAnyType |
9
|
|
|
{ |
10
|
|
|
use Description; |
11
|
|
|
|
12
|
|
|
protected $name; |
13
|
|
|
private $namespace; |
14
|
|
|
|
15
|
|
|
/** @var null|PhpClass */ |
16
|
|
|
private $extends; |
17
|
|
|
|
18
|
12 |
|
protected function renderHeadComment() |
19
|
|
|
{ |
20
|
12 |
|
if ($this->description) { |
21
|
2 |
|
$this->getPhpDoc()->prepend('', $this->description); |
22
|
|
|
} |
23
|
12 |
|
return $this->phpDoc; |
24
|
|
|
} |
25
|
|
|
|
26
|
12 |
|
protected function renderExtends() |
27
|
|
|
{ |
28
|
12 |
|
if ($this->extends) { |
29
|
|
|
return <<<PHP |
30
|
11 |
|
extends {$this->extends->getReference()} |
31
|
|
|
PHP; |
32
|
|
|
} |
33
|
1 |
|
return ''; |
34
|
|
|
} |
35
|
|
|
|
36
|
12 |
|
public function getReference() |
37
|
|
|
{ |
38
|
12 |
|
if ($file = PhpFile::getCurrentPhpFile()) { |
39
|
5 |
|
return $file->getNamespaces()->getReference($this->getFullyQualifiedName()); |
40
|
|
|
} else { |
41
|
8 |
|
return $this->getFullyQualifiedName(); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
12 |
|
public function getFullyQualifiedName() |
46
|
|
|
{ |
47
|
12 |
|
return $this->namespace . '\\' . $this->name; |
48
|
|
|
} |
49
|
|
|
|
50
|
12 |
|
public function renderArgumentType() |
51
|
|
|
{ |
52
|
12 |
|
return $this->getReference(); |
53
|
|
|
} |
54
|
|
|
|
55
|
12 |
|
public function renderPhpDocType() |
56
|
|
|
{ |
57
|
12 |
|
return $this->getReference(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
4 |
|
public function getName() |
64
|
|
|
{ |
65
|
4 |
|
return $this->name; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param mixed $name |
70
|
|
|
* @return PhpClassTraitInterface |
71
|
|
|
*/ |
72
|
12 |
|
public function setName($name) |
73
|
|
|
{ |
74
|
12 |
|
$this->name = $name; |
75
|
12 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
4 |
|
public function getNamespace() |
82
|
|
|
{ |
83
|
4 |
|
return $this->namespace; |
84
|
|
|
} |
85
|
|
|
|
86
|
9 |
|
public function setFullyQualifiedName($fqn) |
87
|
|
|
{ |
88
|
9 |
|
$path = explode('\\', $fqn); |
89
|
9 |
|
$this->name = array_pop($path); |
90
|
9 |
|
$this->namespace = implode('\\', $path); |
91
|
9 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param mixed $namespace |
96
|
|
|
* @return PhpClassTraitInterface |
97
|
|
|
*/ |
98
|
5 |
|
public function setNamespace($namespace) |
99
|
|
|
{ |
100
|
5 |
|
$this->namespace = $namespace; |
101
|
5 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return null|PhpClass |
106
|
|
|
*/ |
107
|
|
|
public function getExtends() |
108
|
|
|
{ |
109
|
|
|
return $this->extends; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param PhpClass $extends |
114
|
|
|
* @return PhpClassTraitInterface |
115
|
|
|
*/ |
116
|
11 |
|
public function setExtends(PhpClass $extends) |
117
|
|
|
{ |
118
|
11 |
|
$this->extends = $extends; |
119
|
11 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Create instance by fully qualified name |
124
|
|
|
* |
125
|
|
|
* @param string $fqn |
126
|
|
|
* @return static |
127
|
|
|
*/ |
128
|
8 |
|
public static function byFQN($fqn) |
129
|
|
|
{ |
130
|
8 |
|
$c = new static; |
131
|
8 |
|
$c->setFullyQualifiedName($fqn); |
132
|
8 |
|
return $c; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
} |