Total Complexity | 25 |
Total Lines | 109 |
Duplicated Lines | 0 % |
Coverage | 76.67% |
Changes | 0 |
1 | <?php |
||
7 | class PhpCode extends PhpTemplate |
||
8 | { |
||
9 | public $snippets; |
||
10 | |||
11 | 4 | public function addSnippet($code, $prepend = false) |
|
12 | { |
||
13 | 4 | if ($prepend) { |
|
14 | array_unshift($this->snippets, $code); |
||
15 | } else { |
||
16 | 4 | $this->snippets[] = $code; |
|
17 | } |
||
18 | 4 | return $this; |
|
19 | } |
||
20 | |||
21 | 4 | protected function toString() |
|
22 | { |
||
23 | 4 | $result = ''; |
|
24 | 4 | if ($this->snippets === null) { |
|
25 | return ''; |
||
26 | } |
||
27 | 4 | foreach ($this->snippets as $code) { |
|
28 | 4 | if ($code instanceof AbstractTemplate) { |
|
29 | 4 | $result .= $code->render(); |
|
30 | } else { |
||
31 | 4 | $result .= $code; |
|
32 | } |
||
33 | } |
||
34 | 4 | return $result; |
|
35 | } |
||
36 | |||
37 | 4 | public static function toCamelCase($string, $lowerFirst = false) |
|
47 | } |
||
48 | |||
49 | 2 | public static function fromCamelCase($input) |
|
50 | { |
||
51 | 2 | preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); |
|
52 | 2 | $ret = $matches[0]; |
|
53 | 2 | foreach ($ret as &$match) { |
|
54 | 2 | $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); |
|
55 | } |
||
56 | 2 | return implode('_', $ret); |
|
57 | } |
||
58 | |||
59 | |||
60 | 4 | public static function makePhpName($rawName, $lowerFirst = true) |
|
70 | } |
||
71 | |||
72 | 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'); |
||
73 | |||
74 | 4 | public static function makePhpClassName($rawName) |
|
75 | { |
||
76 | 4 | $result = self::makePhpName($rawName, false); |
|
77 | 4 | if (in_array(strtolower($result), self::$keywords)) { |
|
78 | 1 | $result .= 'Class'; |
|
79 | } |
||
80 | 4 | return $result; |
|
81 | } |
||
82 | |||
83 | public static function makePhpNamespaceName(array $nsItems) |
||
84 | { |
||
85 | $result = array(); |
||
86 | foreach ($nsItems as $nsItem) { |
||
87 | $nsItem = self::makePhpName($nsItem, false); |
||
88 | if (!$nsItem) { |
||
89 | continue; |
||
90 | } |
||
91 | if (in_array(strtolower($nsItem), self::$keywords)) { |
||
92 | $nsItem .= 'Ns'; |
||
93 | } |
||
94 | $result[] = $nsItem; |
||
95 | } |
||
96 | |||
97 | return '\\' . implode('\\', $result); |
||
98 | } |
||
99 | |||
100 | 2 | public static function makePhpConstantName($rawName) |
|
116 | } |
||
117 | |||
118 | } |