@@ -65,122 +65,92 @@ public function compile($input_file, $output_file = false)
6565 return $ this ;
6666 }
6767
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 = [])
6978 {
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 ();
7282 }
83+ $ this ->validateFormat ($ options ['format ' ]);
7384
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 ' ]}" ;
8488 }
8589
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 \"" ;
9492 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 \"" ;
10294 }
10395
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 . '" ' . " " ;
110101 }
111-
112102 }
113103
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+ }
156106
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+ }
160123
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 ();
163136 }
164137 }
165-
166- $ this ->command = $ command ;
167-
168- return $ this ;
169138 }
170139
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 )
172146 {
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 ( );
175149 }
176150
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 \"" ;
184154
185155 return $ this ;
186156 }
0 commit comments