@@ -77,7 +77,7 @@ class Visualization
77
77
*
78
78
* @throws Visualization_Error
79
79
*/
80
- public function __construct ($ db = null , $ dialect = 'mysql ' )
80
+ public function __construct (PDO $ db = null , $ dialect = 'mysql ' )
81
81
{
82
82
if (!function_exists ('json_encode ' )) {
83
83
throw new Visualization_Error ('You must include the PHP json extension installed to use the MC Google Visualization Server ' );
@@ -90,15 +90,12 @@ public function __construct($db = null, $dialect = 'mysql')
90
90
/**
91
91
* Set the database connection to use when handling the entire request or getting pivot values.
92
92
*
93
- * @param null|mixed| PDO $db the database connection to use - or null if you want to handle your own queries
93
+ * @param null|PDO $db the database connection to use - or null if you want to handle your own queries
94
94
*
95
95
* @throws Visualization_Error
96
96
*/
97
- public function setDB ($ db = null )
97
+ public function setDB (PDO $ db = null )
98
98
{
99
- if (null !== $ db && !($ db instanceof PDO )) {
100
- throw new Visualization_Error ('You must give a PDO database connection ' );
101
- }
102
99
if (null !== $ db ) {
103
100
$ db ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
104
101
}
@@ -142,19 +139,25 @@ public function setDefaultFormat($type, $format)
142
139
}
143
140
144
141
/**
145
- * Handle the entire request, pulling the query from the $_GET variables, and printing the results directly.
142
+ * Handle the entire request, pulling the query from the $_GET variables and printing the results directly
143
+ * if not specified otherwise.
146
144
*
147
- * @param bool $echo print response and set header
145
+ * @param bool $echo print response and set header
146
+ * @param array $query_params query parameters
148
147
*
149
148
* @throws Visualization_Error
150
149
*
151
150
* @return string the javascript response
152
151
*/
153
- public function handleRequest ($ echo = true )
152
+ public function handleRequest ($ echo = true , array $ query_params = null )
154
153
{
155
- $ query = $ _GET ['tq ' ];
154
+ if (null === $ query_params ) {
155
+ $ query_params = $ _GET ;
156
+ }
157
+
158
+ $ query = $ query_params ['tq ' ];
156
159
$ params = ['version ' => $ this ->version , 'responseHandler ' => 'google.visualization.Query.setResponse ' ];
157
- $ paramlist = explode ('; ' , $ _GET ['tqx ' ]);
160
+ $ paramlist = explode ('; ' , $ query_params ['tqx ' ]);
158
161
foreach ($ paramlist as $ paramstr ) {
159
162
list ($ name , $ val ) = explode (': ' , $ paramstr );
160
163
$ params [$ name ] = $ val ;
@@ -166,8 +169,8 @@ public function handleRequest($echo = true)
166
169
throw new Visualization_Error ('Data Source version ' .$ params ['version ' ].' is unsupported at this time ' );
167
170
}
168
171
169
- if (isset ($ _GET ['responseHandler ' ])) {
170
- $ params ['responseHandler ' ] = $ _GET ['responseHandler ' ];
172
+ if (isset ($ query_params ['responseHandler ' ])) {
173
+ $ params ['responseHandler ' ] = $ query_params ['responseHandler ' ];
171
174
}
172
175
173
176
$ response = $ this ->handleQuery ($ query , $ params );
@@ -188,7 +191,7 @@ public function handleRequest($echo = true)
188
191
*
189
192
* @return string the javascript response
190
193
*/
191
- public function handleQuery ($ query , $ params )
194
+ public function handleQuery ($ query , array $ params )
192
195
{
193
196
$ reqid = null ;
194
197
$ response = '' ;
@@ -261,7 +264,7 @@ public function handleError($reqid, $detail_msg, $handler = 'google.visualizatio
261
264
*
262
265
* @return string the SQL version of the visualization query
263
266
*/
264
- public function generateSQL (&$ meta )
267
+ public function generateSQL (array &$ meta )
265
268
{
266
269
if (!isset ($ meta ['query_fields ' ])) {
267
270
$ meta ['query_fields ' ] = $ meta ['select ' ];
@@ -422,7 +425,7 @@ public function generateSQL(&$meta)
422
425
*
423
426
* @return array the metadata array from merging the query with the entity table definitions
424
427
*/
425
- public function generateMetadata ($ query )
428
+ public function generateMetadata (array $ query )
426
429
{
427
430
$ meta = [];
428
431
if (!isset ($ query ['from ' ]) && null === $ this ->default_entity ) {
@@ -720,7 +723,7 @@ public function parseQuery($str)
720
723
*
721
724
* @throws Visualization_Error
722
725
*/
723
- public function addEntity ($ name , $ spec = [])
726
+ public function addEntity ($ name , array $ spec = [])
724
727
{
725
728
$ entity = ['table ' => isset ($ spec ['table ' ]) ? $ spec ['table ' ] : $ name , 'fields ' => [], 'joins ' => []];
726
729
$ this ->entities [$ name ] = $ entity ;
@@ -751,7 +754,7 @@ public function addEntity($name, $spec = [])
751
754
*
752
755
* @throws Visualization_Error
753
756
*/
754
- public function addEntityField ($ entity , $ field , $ spec )
757
+ public function addEntityField ($ entity , $ field , array $ spec )
755
758
{
756
759
if (!isset ($ spec ['field ' ]) && !isset ($ spec ['callback ' ])) {
757
760
throw new Visualization_Error ('Entity fields must either be mapped to database fields or given callback functions ' );
@@ -828,7 +831,7 @@ public function setDefaultEntity($default = null)
828
831
*
829
832
* @return string the initial output string for a successful query
830
833
*/
831
- public function getSuccessInit ($ meta )
834
+ public function getSuccessInit (array $ meta )
832
835
{
833
836
$ handler = $ meta ['req_params ' ]['responseHandler ' ] ?: 'google.visualization.Query.setResponse ' ;
834
837
$ version = $ meta ['req_params ' ]['version ' ] ?: $ this ->version ;
@@ -845,7 +848,7 @@ public function getSuccessInit($meta)
845
848
*
846
849
* @return string
847
850
*/
848
- public function getTableInit ($ meta )
851
+ public function getTableInit (array $ meta )
849
852
{
850
853
$ field_init = [];
851
854
foreach ($ meta ['select ' ] as $ field ) {
@@ -915,7 +918,7 @@ public function getTableInit($meta)
915
918
*
916
919
* @return string the string fragment to include in the results back to the javascript client
917
920
*/
918
- public function getRowValues ($ row , $ meta )
921
+ public function getRowValues (array $ row , array $ meta )
919
922
{
920
923
$ vals = [];
921
924
foreach ($ meta ['select ' ] as $ field ) {
@@ -1235,7 +1238,7 @@ protected function getFieldQuote()
1235
1238
*
1236
1239
* @return string the SQL string for this field, with an op
1237
1240
*/
1238
- protected function getFieldSQL ($ name , $ spec , $ alias = false , $ func = null , $ pivot = null , $ pivot_fields = null )
1241
+ protected function getFieldSQL ($ name , $ spec , $ alias = false , $ func = null , array $ pivot = null , array $ pivot_fields = null )
1239
1242
{
1240
1243
$ sql = $ spec ['field ' ];
1241
1244
$ q = $ this ->getFieldQuote ();
@@ -1272,7 +1275,7 @@ protected function getFieldSQL($name, $spec, $alias = false, $func = null, $pivo
1272
1275
*
1273
1276
* @throws Visualization_Error
1274
1277
*/
1275
- protected function addDependantCallbackFields ($ field , $ entity , &$ meta )
1278
+ protected function addDependantCallbackFields (array $ field , array $ entity , array &$ meta )
1276
1279
{
1277
1280
foreach ($ field ['fields ' ] as $ dependant ) {
1278
1281
if (!isset ($ entity ['fields ' ][$ dependant ])) {
@@ -1295,10 +1298,10 @@ protected function addDependantCallbackFields($field, $entity, &$meta)
1295
1298
/**
1296
1299
* Helper method for the query parser to recursively scan the delimited list of select fields.
1297
1300
*
1298
- * @param Token $token the token or token group to recursively parse
1299
- * @param array $fields the collector array reference to receive the flattened select field values
1301
+ * @param Token $token the token or token group to recursively parse
1302
+ * @param null| array $fields the collector array reference to receive the flattened select field values
1300
1303
*/
1301
- protected function parseFieldTokens ($ token , &$ fields )
1304
+ protected function parseFieldTokens ($ token , array &$ fields = null )
1302
1305
{
1303
1306
if ('* ' === $ token ->value ) {
1304
1307
return ;
@@ -1326,10 +1329,10 @@ protected function parseFieldTokens($token, &$fields)
1326
1329
/**
1327
1330
* Helper method for the query parser to recursively scan and flatten the where clause's conditions.
1328
1331
*
1329
- * @param Token $token the token or token group to parse
1330
- * @param array $where the collector array of tokens that make up the where clause
1332
+ * @param Token $token the token or token group to parse
1333
+ * @param null| array $where the collector array of tokens that make up the where clause
1331
1334
*/
1332
- protected function parseWhereTokens ($ token , &$ where )
1335
+ protected function parseWhereTokens ($ token , array &$ where = null )
1333
1336
{
1334
1337
if (!is_array ($ where )) {
1335
1338
$ where = [];
0 commit comments