Repository navigation
Expand file tree
/
Copy pathPhpCode.php
More file actions
145 lines (124 loc) · 4.59 KB
/
Copy pathPhpCode.php
File metadata and controls
145 lines (124 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace Swaggest\PhpCodeBuilder;
use Swaggest\CodeBuilder\AbstractTemplate;
class PhpCode extends PhpTemplate
{
public $snippets;
public function addSnippet($code, $prepend = false)
{
if ($prepend) {
array_unshift($this->snippets, $code);
} else {
$this->snippets[] = $code;
}
return $this;
}
protected function toString()
{
$result = '';
if ($this->snippets === null) {
return '';
}
foreach ($this->snippets as $code) {
if ($code instanceof AbstractTemplate) {
$result .= $code->render();
} else {
$result .= $code;
}
}
return $result;
}
public static function toCamelCase($string, $lowerFirst = false)
{
$result = implode('', array_map('ucfirst', explode('_', $string)));
if (!$result) {
return '';
}
if ($lowerFirst) {
$result[0] = strtolower($result[0]);
}
return $result;
}
public static function fromCamelCase($input)
{
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
$ret = $matches[0];
$index = array();
foreach ($ret as &$match) {
$index[$match] = '_' . ($match == strtoupper($match) ? strtolower($match) : lcfirst($match));
}
krsort($index);
$result = strtr($input, $index);
if ($input[0] !== '_' && $result[0] === '_') {
$result = substr($result, 1);
}
return preg_replace('/_+/', '_', $result);
}
public static function makePhpName($rawName, $lowerFirst = true)
{
$phpName = preg_replace("/([^a-zA-Z0-9_]+)/", "_", $rawName);
$phpName = self::toCamelCase($phpName, $lowerFirst);
if (!$phpName) {
$phpName = 'Property' . substr(md5($rawName), 0, 6);
} elseif (is_numeric($phpName[0])) {
$phpName = 'Property' . $phpName;
}
if ($lowerFirst) {
$phpName[0] = strtolower($phpName[0]);
}
return $phpName;
}
private static $keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');
public static function makePhpClassName($rawName)
{
$result = self::makePhpName($rawName, false);
if (in_array(strtolower($result), self::$keywords)) {
$result .= 'Class';
}
return $result;
}
public static function makePhpNamespaceName(array $nsItems)
{
$result = array();
foreach ($nsItems as $nsItem) {
$nsItem = self::makePhpName($nsItem, false);
if (!$nsItem) {
continue;
}
if (in_array(strtolower($nsItem), self::$keywords)) {
$nsItem .= 'Ns';
}
$result[] = $nsItem;
}
return '\\' . implode('\\', $result);
}
public static function makePhpConstantName($rawName)
{
$phpName = preg_replace("/([^a-zA-Z0-9_]+)/", "_", $rawName);
if (is_string($phpName)) {
$phpName = trim($phpName, '_');
}
if ($phpName) {
$phpName = self::fromCamelCase($phpName);
if (in_array(strtolower($phpName), self::$keywords)) {
$phpName = '_' . $phpName;
}
if (is_numeric($phpName[0])) {
$phpName = 'const_' . $phpName;
}
} else {
$phpName = 'const_' . substr(md5($rawName), 0, 6);
}
return strtoupper($phpName);
}
public static function varExport($value)
{
/** @var string $result */
$result = var_export($value, true);
$result = str_replace("stdClass::__set_state", "(object)", $result);
$result = str_replace("array (\n", "array(\n", $result);
$result = str_replace(' ', ' ', $result);
$result = str_replace("array(\n)", "array()", $result);
return $result;
}
}