1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the samshal/scripd package. |
5
|
|
|
* |
6
|
|
|
* (c) Samuel Adeshina <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Samshal\Scripd; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Contains utility methods for parsing a jsyn file. |
16
|
|
|
* |
17
|
|
|
* @since 1.0 |
18
|
|
|
* |
19
|
|
|
* @author Samuel Adeshina <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class JsynExtractor |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
* |
26
|
|
|
* Content of the JSYN File |
27
|
|
|
*/ |
28
|
|
|
private $jsyn; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
* |
33
|
|
|
* SQL Syntax to use for script generation |
34
|
|
|
*/ |
35
|
|
|
private $sqlSyntax; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $jsynFile string | PathUtil |
39
|
|
|
* @param $sqlSyntax string |
40
|
|
|
* |
41
|
|
|
* @return null |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
public function __construct($jsynFile, $sqlSyntax) |
44
|
|
|
{ |
45
|
|
|
self::setJsynFile($jsynFile); |
46
|
|
|
self::setSqlSyntax($sqlSyntax); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $jsynFile string | PathUtil |
51
|
|
|
* |
52
|
|
|
* Setter function for the jsonFile global property |
53
|
|
|
* |
54
|
|
|
* @return null |
55
|
|
|
*/ |
56
|
|
|
public function setJsynFile($jsynFile) |
57
|
|
|
{ |
58
|
|
|
$this->jsyn = json_decode(file_get_contents($jsynFile)); |
59
|
|
|
|
60
|
|
|
if (isset($this->sqlSyntax)) { |
61
|
|
|
$sqlSyntax = $this->sqlSyntax; |
62
|
|
|
$this->jsyn = $this->jsyn->$sqlSyntax; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $sqlSyntax string |
70
|
|
|
* |
71
|
|
|
* Setter function for the sqlSyntax global property |
72
|
|
|
* |
73
|
|
|
* @return null |
74
|
|
|
*/ |
75
|
|
|
public function setSqlSyntax($sqlSyntax) |
76
|
|
|
{ |
77
|
|
|
$this->sqlSyntax = $sqlSyntax; |
78
|
|
|
|
79
|
|
|
if (isset($this->jsyn->$sqlSyntax)) { |
80
|
|
|
$this->jsyn = $this->jsyn->$sqlSyntax; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return Array |
88
|
|
|
*/ |
89
|
|
|
public function getJsyn() |
90
|
|
|
{ |
91
|
|
|
return $this->jsyn; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Performs extraction of the appropriate sql syntax |
96
|
|
|
* fromthe supplied jsyn file. |
97
|
|
|
* |
98
|
|
|
* @return null |
99
|
|
|
*/ |
100
|
|
|
public function formatJsyn() |
101
|
|
|
{ |
102
|
|
|
for ($i = 0; $i < count($this->jsyn); ++$i) { |
|
|
|
|
103
|
|
|
if (strpos($this->jsyn[$i], '[') === 0 || strpos($this->jsyn[$i], '{') === (int) 0) { |
104
|
|
|
$this->jsyn[$i] = strtolower($this->jsyn[$i]); |
105
|
|
|
} else { |
106
|
|
|
$this->jsyn[$i] = strtoupper($this->jsyn[$i]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns the extracted jsyn as a string |
113
|
|
|
* |
114
|
|
|
* @return null |
115
|
|
|
*/ |
116
|
|
|
public function __toString() |
117
|
|
|
{ |
118
|
|
|
return implode(' ', $this->getJsyn()); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.