1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
namespace gossi\codegen\model; |
18
|
|
|
|
19
|
|
|
use gossi\codegen\model\parts\TypeDocblockGeneratorPart; |
20
|
|
|
use gossi\codegen\model\parts\ValuePart; |
21
|
|
|
use gossi\docblock\Docblock; |
22
|
|
|
use gossi\docblock\tags\VarTag; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Represents a PHP property. |
26
|
|
|
* |
27
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
28
|
|
|
* @author Thomas Gossmann |
29
|
|
|
*/ |
30
|
|
|
class PhpProperty extends AbstractPhpMember { |
31
|
|
|
|
32
|
|
|
use TypeDocblockGeneratorPart; |
33
|
|
|
use ValuePart; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Creates a new PHP property |
37
|
|
|
* |
38
|
|
|
* @param string $name the properties name |
39
|
|
|
* @return static |
40
|
|
|
*/ |
41
|
11 |
|
public static function create($name) { |
42
|
11 |
|
return new static($name); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Export array as php 5.4 |
48
|
|
|
* |
49
|
|
|
* @param $var |
50
|
|
|
* @param string $indent |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
static public function export($var, $indent="") { |
|
|
|
|
54
|
|
|
switch (gettype($var)) { |
55
|
|
|
case "string": |
|
|
|
|
56
|
|
|
return '"' . addcslashes($var, "\\\$\"\r\n\t\v\f") . '"'; |
57
|
|
|
case "array": |
|
|
|
|
58
|
|
|
$indexed = array_keys($var) === range(0, count($var) - 1); |
59
|
|
|
$r = []; |
60
|
|
|
foreach ($var as $key => $value) { |
61
|
|
|
$r[] = "$indent " |
|
|
|
|
62
|
|
|
. ($indexed ? "" : static::export($key) . " => ") |
|
|
|
|
63
|
|
|
. static::export($value, "$indent "); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
return "[\n" . implode(",\n", $r) . "\n" . $indent . "]"; |
|
|
|
|
66
|
|
|
case "boolean": |
|
|
|
|
67
|
|
|
return $var ? "TRUE" : "FALSE"; |
|
|
|
|
68
|
|
|
default: |
69
|
|
|
return var_export($var, TRUE); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Creates a new PHP property from reflection |
75
|
|
|
* |
76
|
|
|
* @param \ReflectionProperty $ref |
77
|
|
|
* @return static |
78
|
|
|
*/ |
79
|
1 |
|
public static function fromReflection(\ReflectionProperty $ref) { |
80
|
1 |
|
$property = new static($ref->name); |
81
|
1 |
|
$property->setStatic($ref->isStatic()) |
82
|
1 |
|
->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE)); |
83
|
|
|
|
84
|
1 |
|
$docblock = new Docblock($ref); |
85
|
1 |
|
$property->setDocblock($docblock); |
86
|
1 |
|
$property->setDescription($docblock->getShortDescription()); |
87
|
1 |
|
$property->setLongDescription($docblock->getLongDescription()); |
88
|
|
|
|
89
|
1 |
|
$vars = $docblock->getTags('var'); |
90
|
1 |
|
if ($vars->size() > 0) { |
91
|
|
|
$var = $vars->get(0); |
92
|
|
|
$property->setType($var->getType(), $var->getDescription()); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
$defaultProperties = $ref->getDeclaringClass()->getDefaultProperties(); |
96
|
|
|
|
97
|
1 |
|
if (isset($defaultProperties[$ref->name])) { |
98
|
|
|
$default = $defaultProperties[$ref->name]; |
99
|
|
|
if (is_string($default)) { |
100
|
|
|
$property->setValue($default); |
101
|
|
|
} elseif(is_array($default)) { |
102
|
|
|
$property->setValue(static::export($default)); |
103
|
|
|
} elseif(is_object($default)) { |
|
|
|
|
104
|
|
|
} else { |
105
|
|
|
$property->setExpression($default); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
return $property; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Generates docblock based on provided information |
114
|
|
|
*/ |
115
|
8 |
|
public function generateDocblock() { |
116
|
8 |
|
$docblock = $this->getDocblock(); |
117
|
8 |
|
$docblock->setShortDescription($this->getDescription()); |
118
|
8 |
|
$docblock->setLongDescription($this->getLongDescription()); |
119
|
|
|
|
120
|
|
|
// var tag |
121
|
8 |
|
$this->generateTypeTag(new VarTag()); |
122
|
8 |
|
} |
123
|
|
|
} |
124
|
|
|
|