@@ -65,122 +65,92 @@ public function compile($input_file, $output_file = false)
65
65
return $ this ;
66
66
}
67
67
68
- public function process ($ input_file , $ output_file = false , $ format = ['pdf ' ], $ parameters = [], $ db_connection = [], $ locale = false )
68
+
69
+ /**
70
+ * @param $input_file
71
+ * @param bool $output_file
72
+ * @param array $options
73
+ * @return $this
74
+ * @throws Exception\InvalidInputFile
75
+ * @throws Exception\InvalidFormat
76
+ */
77
+ public function process ($ input_file , $ output_file = false , $ options = [])
69
78
{
70
- if (is_null ($ input_file ) || empty ($ input_file )) {
71
- throw new \Exception ('No input file ' , 1 );
79
+ $ options = $ this ->parseProcessOptions ($ options );
80
+ if (!$ input_file ) {
81
+ throw new \JasperPHP \Exception \InvalidInputFile ();
72
82
}
83
+ $ this ->validateFormat ($ options ['format ' ]);
73
84
74
- if (is_array ($ format )) {
75
- foreach ($ format as $ key ) {
76
- if (!in_array ($ key , $ this ->formats )) {
77
- throw new \Exception ('Invalid format! ' , 1 );
78
- }
79
- }
80
- } else {
81
- if (!in_array ($ format , $ this ->formats )) {
82
- throw new \Exception ('Invalid format! ' , 1 );
83
- }
85
+ $ this ->command = $ this ->windows ? $ this ->executable : './ ' . $ this ->executable ;
86
+ if ($ options ['locale ' ]) {
87
+ $ this ->command .= " --locale {$ options ['locale ' ]}" ;
84
88
}
85
89
86
- $ command = ($ this ->windows ) ? $ this ->executable : './ ' . $ this ->executable ;
87
-
88
- $ command .= ($ locale ) ? " --locale $ locale " : '' ;
89
-
90
- $ command .= ' process ' ;
91
-
92
- $ command .= "\"$ input_file \"" ;
93
-
90
+ $ this ->command .= ' process ' ;
91
+ $ this ->command .= "\"$ input_file \"" ;
94
92
if ($ output_file !== false ) {
95
- $ command .= ' -o ' . "\"$ output_file \"" ;
96
- }
97
-
98
- if (is_array ($ format )) {
99
- $ command .= ' -f ' . join (' ' , $ format );
100
- } else {
101
- $ command .= ' -f ' . $ format ;
93
+ $ this ->command .= ' -o ' . "\"$ output_file \"" ;
102
94
}
103
95
104
- if (count ($ parameters ) > 0 ) {
105
- $ command .= ' -P ' ;
106
-
107
- foreach ($ parameters as $ key => $ value ) {
108
- $ param = $ key . '=" ' . $ value . '" ' ;
109
- $ command .= " " . $ param . " " ;
96
+ $ this ->command .= ' -f ' . join (' ' , $ options ['format ' ]);
97
+ if ($ options ['params ' ]) {
98
+ $ this ->command .= ' -P ' ;
99
+ foreach ($ options ['params ' ] as $ key => $ value ) {
100
+ $ this ->command .= " " . $ key . '=" ' . $ value . '" ' . " " ;
110
101
}
111
-
112
102
}
113
103
114
- if (count ($ db_connection ) > 0 ) {
115
- $ command .= ' -t ' . $ db_connection ['driver ' ];
116
-
117
- if (isset ($ db_connection ['username ' ])) {
118
- $ command .= " -u " . $ db_connection ['username ' ];
119
- }
120
-
121
- if (isset ($ db_connection ['password ' ]) && !empty ($ db_connection ['password ' ])) {
122
- $ command .= ' -p ' . $ db_connection ['password ' ];
123
- }
124
-
125
- if (isset ($ db_connection ['host ' ]) && !empty ($ db_connection ['host ' ])) {
126
- $ command .= ' -H ' . $ db_connection ['host ' ];
127
- }
128
-
129
- if (isset ($ db_connection ['database ' ]) && !empty ($ db_connection ['database ' ])) {
130
- $ command .= ' -n ' . $ db_connection ['database ' ];
131
- }
132
-
133
- if (isset ($ db_connection ['port ' ]) && !empty ($ db_connection ['port ' ])) {
134
- $ command .= ' --db-port ' . $ db_connection ['port ' ];
135
- }
136
-
137
- if (isset ($ db_connection ['jdbc_driver ' ]) && !empty ($ db_connection ['jdbc_driver ' ])) {
138
- $ command .= ' --db-driver ' . $ db_connection ['jdbc_driver ' ];
139
- }
140
-
141
- if (isset ($ db_connection ['jdbc_url ' ]) && !empty ($ db_connection ['jdbc_url ' ])) {
142
- $ command .= ' --db-url ' . $ db_connection ['jdbc_url ' ];
143
- }
144
-
145
- if (isset ($ db_connection ['jdbc_dir ' ]) && !empty ($ db_connection ['jdbc_dir ' ])) {
146
- $ command .= ' --jdbc-dir ' . $ db_connection ['jdbc_dir ' ];
147
- }
148
-
149
- if (isset ($ db_connection ['db_sid ' ]) && !empty ($ db_connection ['db_sid ' ])) {
150
- $ command .= ' --db-sid ' . $ db_connection ['db_sid ' ];
151
- }
152
-
153
- if (isset ($ db_connection ['xml_xpath ' ])) {
154
- $ command .= ' --xml-xpath ' . $ db_connection ['xml_xpath ' ];
155
- }
104
+ return $ this ;
105
+ }
156
106
157
- if (isset ($ db_connection ['data_file ' ])) {
158
- $ command .= ' --data-file ' . $ db_connection ['data_file ' ];
159
- }
107
+ /**
108
+ *
109
+ * @param $options
110
+ * @return array
111
+ */
112
+ protected function parseProcessOptions ($ options )
113
+ {
114
+ $ defaultOptions = [
115
+ 'format ' => ['pdf ' ],
116
+ 'params ' => [],
117
+ 'locale ' => false ,
118
+ 'db_connection ' => []
119
+ ];
120
+
121
+ return array_merge ($ defaultOptions , $ options );
122
+ }
160
123
161
- if (isset ($ db_connection ['json_query ' ])) {
162
- $ command .= ' --json-query ' . $ db_connection ['json_query ' ];
124
+ /**
125
+ * @param $format
126
+ * @throws Exception\InvalidFormat
127
+ */
128
+ protected function validateFormat ($ format )
129
+ {
130
+ if (!is_array ($ format )) {
131
+ $ format = [$ format ];
132
+ }
133
+ foreach ($ format as $ value ) {
134
+ if (!in_array ($ value , $ this ->formats )) {
135
+ throw new \JasperPHP \Exception \InvalidFormat ();
163
136
}
164
137
}
165
-
166
- $ this ->command = $ command ;
167
-
168
- return $ this ;
169
138
}
170
139
171
- public function list_parameters ($ input_file )
140
+ /**
141
+ * @param $input_file
142
+ * @return $this
143
+ * @throws \Exception
144
+ */
145
+ public function listParameters ($ input_file )
172
146
{
173
- if (is_null ( $ input_file) || empty ( $ input_file ) ) {
174
- throw new \Exception ( ' No input file ' , 1 );
147
+ if (! $ input_file ) {
148
+ throw new \JasperPHP \ Exception \ InvalidInputFile ( );
175
149
}
176
150
177
- $ command = ($ this ->windows ) ? $ this ->executable : './ ' . $ this ->executable ;
178
-
179
- $ command .= ' list_parameters ' ;
180
-
181
- $ command .= "\"$ input_file \"" ;
182
-
183
- $ this ->command = $ command ;
151
+ $ this ->command = $ this ->windows ? $ this ->executable : './ ' . $ this ->executable ;
152
+ $ this ->command .= ' list_parameters ' ;
153
+ $ this ->command .= "\"$ input_file \"" ;
184
154
185
155
return $ this ;
186
156
}
0 commit comments