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
|
|
|
public function __construct($jsynFile, $sqlSyntax) |
42
|
|
|
{ |
43
|
|
|
self::setJsynFile($jsynFile); |
44
|
|
|
self::setSqlSyntax($sqlSyntax); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $jsynFile string | PathUtil |
49
|
|
|
* |
50
|
|
|
* Setter function for the jsonFile global property |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function setJsynFile($jsynFile) |
55
|
|
|
{ |
56
|
|
|
$this->jsyn = json_decode(file_get_contents($jsynFile)); |
57
|
|
|
|
58
|
|
View Code Duplication |
if (isset($this->sqlSyntax)) { |
|
|
|
|
59
|
|
|
$sqlSyntax = $this->sqlSyntax; |
60
|
|
|
if (!isset($this->jsyn->$sqlSyntax)) { |
61
|
|
|
$sqlSyntax = "default"; |
62
|
|
|
} |
63
|
|
|
$this->jsyn = $this->jsyn->$sqlSyntax; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $sqlSyntax string |
71
|
|
|
* |
72
|
|
|
* Setter function for the sqlSyntax global property |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function setSqlSyntax($sqlSyntax) |
77
|
|
|
{ |
78
|
|
|
$this->sqlSyntax = $sqlSyntax; |
79
|
|
|
|
80
|
|
View Code Duplication |
if (isset($this->jsyn->$sqlSyntax)) { |
|
|
|
|
81
|
|
|
$this->jsyn = $this->jsyn->$sqlSyntax; |
82
|
|
|
} else { |
83
|
|
|
$sqlSyntax ="default"; |
84
|
|
|
$this->jsyn = $this->jsyn->$sqlSyntax; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function getJsyn() |
94
|
|
|
{ |
95
|
|
|
return $this->jsyn; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Performs extraction of the appropriate sql syntax |
100
|
|
|
* fromthe supplied jsyn file. |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
public function formatJsyn() |
105
|
|
|
{ |
106
|
|
|
for ($i = 0; $i < count($this->jsyn); ++$i) { |
|
|
|
|
107
|
|
|
if (strpos($this->jsyn[$i], '[') === 0 || strpos($this->jsyn[$i], '{') === (int) 0) { |
108
|
|
|
$this->jsyn[$i] = strtolower($this->jsyn[$i]); |
109
|
|
|
} else { |
110
|
|
|
$this->jsyn[$i] = strtoupper($this->jsyn[$i]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns the extracted jsyn as a string. |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function __toString() |
123
|
|
|
{ |
124
|
|
|
return implode(' ', $this->getJsyn()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.