|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BigShark\SQLToBuilder; |
|
4
|
|
|
|
|
5
|
|
|
class Generator |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var array |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $functions = []; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $class = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param string $class |
|
19
|
|
|
* @param array $args |
|
20
|
|
|
*/ |
|
21
|
120 |
|
public function __construct($class, $args = []) |
|
22
|
|
|
{ |
|
23
|
120 |
|
$this->class = ['name' => $class, 'args' => $args]; |
|
24
|
120 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $name |
|
28
|
|
|
* @param array $args |
|
29
|
|
|
* |
|
30
|
|
|
* @return $this |
|
31
|
|
|
*/ |
|
32
|
108 |
|
public function addFunction($name, $args = []) |
|
33
|
|
|
{ |
|
34
|
108 |
|
$this->functions[] = ['name' => $name, 'args' => $args]; |
|
35
|
|
|
|
|
36
|
108 |
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
3 |
|
public function getFunctions() |
|
43
|
|
|
{ |
|
44
|
3 |
|
return $this->functions; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
3 |
|
public function getClass() |
|
51
|
|
|
{ |
|
52
|
3 |
|
return $this->class; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
108 |
|
public function isStatic() |
|
59
|
|
|
{ |
|
60
|
108 |
|
return !(substr($this->class['name'], 0, 1) === '$'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @throws \Exception |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
108 |
|
public function generate() |
|
69
|
|
|
{ |
|
70
|
108 |
|
if (count($this->functions) === 0) { |
|
71
|
3 |
|
throw new \Exception('function list empty'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
105 |
|
$parts = []; |
|
75
|
105 |
|
foreach ($this->functions as $function) { |
|
76
|
105 |
|
$args = []; |
|
77
|
105 |
|
if ($function['args']) { |
|
78
|
99 |
|
$args = $this->generateArgs($function['args']); |
|
79
|
|
|
} |
|
80
|
105 |
|
$parts[] = $function['name'].'('.implode(', ', $args).')'; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
105 |
|
$class = $this->class['name']; |
|
84
|
105 |
|
$class .= count($this->class['args']) ? '('.implode(', ', $this->class['args']).')' : ''; |
|
85
|
|
|
|
|
86
|
105 |
|
$result = $class.($this->isStatic() ? '::' : '->').implode('->', $parts); |
|
87
|
|
|
|
|
88
|
105 |
|
return $result; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param $_args |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
99 |
|
protected function generateArgs($_args) |
|
97
|
|
|
{ |
|
98
|
99 |
|
$args = []; |
|
99
|
99 |
|
foreach ($_args as $arg) { |
|
100
|
99 |
|
if (is_int($arg) || is_float($arg) || is_numeric($arg)) { |
|
101
|
24 |
|
$args[] = $arg; |
|
102
|
99 |
|
} elseif (is_array($arg)) { |
|
103
|
9 |
|
$args[] = '['.implode(', ', $this->generateArgs($arg)).']'; |
|
104
|
99 |
|
} elseif ($arg instanceof static) { |
|
105
|
6 |
|
$args[] = $arg->generate(); |
|
106
|
|
|
} else { |
|
107
|
99 |
|
$args[] = '\''.$arg.'\''; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
99 |
|
return $args; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|