Skip to content

Commit 94e3de8

Browse files
committed
Enables your own set of query parameters. Type hint functions parameters.
1 parent 7258546 commit 94e3de8

File tree

12 files changed

+121
-41
lines changed

12 files changed

+121
-41
lines changed

lib/MC/Google/Visualization.php

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Visualization
7777
*
7878
* @throws Visualization_Error
7979
*/
80-
public function __construct($db = null, $dialect = 'mysql')
80+
public function __construct(PDO $db = null, $dialect = 'mysql')
8181
{
8282
if (!function_exists('json_encode')) {
8383
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')
9090
/**
9191
* Set the database connection to use when handling the entire request or getting pivot values.
9292
*
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
9494
*
9595
* @throws Visualization_Error
9696
*/
97-
public function setDB($db = null)
97+
public function setDB(PDO $db = null)
9898
{
99-
if (null !== $db && !($db instanceof PDO)) {
100-
throw new Visualization_Error('You must give a PDO database connection');
101-
}
10299
if (null !== $db) {
103100
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
104101
}
@@ -142,19 +139,25 @@ public function setDefaultFormat($type, $format)
142139
}
143140

144141
/**
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.
146144
*
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
148147
*
149148
* @throws Visualization_Error
150149
*
151150
* @return string the javascript response
152151
*/
153-
public function handleRequest($echo = true)
152+
public function handleRequest($echo = true, array $query_params = null)
154153
{
155-
$query = $_GET['tq'];
154+
if (null === $query_params) {
155+
$query_params = $_GET;
156+
}
157+
158+
$query = $query_params['tq'];
156159
$params = ['version' => $this->version, 'responseHandler' => 'google.visualization.Query.setResponse'];
157-
$paramlist = explode(';', $_GET['tqx']);
160+
$paramlist = explode(';', $query_params['tqx']);
158161
foreach ($paramlist as $paramstr) {
159162
list($name, $val) = explode(':', $paramstr);
160163
$params[$name] = $val;
@@ -166,8 +169,8 @@ public function handleRequest($echo = true)
166169
throw new Visualization_Error('Data Source version '.$params['version'].' is unsupported at this time');
167170
}
168171

169-
if (isset($_GET['responseHandler'])) {
170-
$params['responseHandler'] = $_GET['responseHandler'];
172+
if (isset($query_params['responseHandler'])) {
173+
$params['responseHandler'] = $query_params['responseHandler'];
171174
}
172175

173176
$response = $this->handleQuery($query, $params);
@@ -188,7 +191,7 @@ public function handleRequest($echo = true)
188191
*
189192
* @return string the javascript response
190193
*/
191-
public function handleQuery($query, $params)
194+
public function handleQuery($query, array $params)
192195
{
193196
$reqid = null;
194197
$response = '';
@@ -261,7 +264,7 @@ public function handleError($reqid, $detail_msg, $handler = 'google.visualizatio
261264
*
262265
* @return string the SQL version of the visualization query
263266
*/
264-
public function generateSQL(&$meta)
267+
public function generateSQL(array &$meta)
265268
{
266269
if (!isset($meta['query_fields'])) {
267270
$meta['query_fields'] = $meta['select'];
@@ -422,7 +425,7 @@ public function generateSQL(&$meta)
422425
*
423426
* @return array the metadata array from merging the query with the entity table definitions
424427
*/
425-
public function generateMetadata($query)
428+
public function generateMetadata(array $query)
426429
{
427430
$meta = [];
428431
if (!isset($query['from']) && null === $this->default_entity) {
@@ -720,7 +723,7 @@ public function parseQuery($str)
720723
*
721724
* @throws Visualization_Error
722725
*/
723-
public function addEntity($name, $spec = [])
726+
public function addEntity($name, array $spec = [])
724727
{
725728
$entity = ['table' => isset($spec['table']) ? $spec['table'] : $name, 'fields' => [], 'joins' => []];
726729
$this->entities[$name] = $entity;
@@ -751,7 +754,7 @@ public function addEntity($name, $spec = [])
751754
*
752755
* @throws Visualization_Error
753756
*/
754-
public function addEntityField($entity, $field, $spec)
757+
public function addEntityField($entity, $field, array $spec)
755758
{
756759
if (!isset($spec['field']) && !isset($spec['callback'])) {
757760
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)
828831
*
829832
* @return string the initial output string for a successful query
830833
*/
831-
public function getSuccessInit($meta)
834+
public function getSuccessInit(array $meta)
832835
{
833836
$handler = $meta['req_params']['responseHandler'] ?: 'google.visualization.Query.setResponse';
834837
$version = $meta['req_params']['version'] ?: $this->version;
@@ -845,7 +848,7 @@ public function getSuccessInit($meta)
845848
*
846849
* @return string
847850
*/
848-
public function getTableInit($meta)
851+
public function getTableInit(array $meta)
849852
{
850853
$field_init = [];
851854
foreach ($meta['select'] as $field) {
@@ -915,7 +918,7 @@ public function getTableInit($meta)
915918
*
916919
* @return string the string fragment to include in the results back to the javascript client
917920
*/
918-
public function getRowValues($row, $meta)
921+
public function getRowValues(array $row, array $meta)
919922
{
920923
$vals = [];
921924
foreach ($meta['select'] as $field) {
@@ -1235,7 +1238,7 @@ protected function getFieldQuote()
12351238
*
12361239
* @return string the SQL string for this field, with an op
12371240
*/
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)
12391242
{
12401243
$sql = $spec['field'];
12411244
$q = $this->getFieldQuote();
@@ -1272,7 +1275,7 @@ protected function getFieldSQL($name, $spec, $alias = false, $func = null, $pivo
12721275
*
12731276
* @throws Visualization_Error
12741277
*/
1275-
protected function addDependantCallbackFields($field, $entity, &$meta)
1278+
protected function addDependantCallbackFields(array $field, array $entity, array &$meta)
12761279
{
12771280
foreach ($field['fields'] as $dependant) {
12781281
if (!isset($entity['fields'][$dependant])) {
@@ -1295,10 +1298,10 @@ protected function addDependantCallbackFields($field, $entity, &$meta)
12951298
/**
12961299
* Helper method for the query parser to recursively scan the delimited list of select fields.
12971300
*
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
13001303
*/
1301-
protected function parseFieldTokens($token, &$fields)
1304+
protected function parseFieldTokens($token, array &$fields = null)
13021305
{
13031306
if ('*' === $token->value) {
13041307
return;
@@ -1326,10 +1329,10 @@ protected function parseFieldTokens($token, &$fields)
13261329
/**
13271330
* Helper method for the query parser to recursively scan and flatten the where clause's conditions.
13281331
*
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
13311334
*/
1332-
protected function parseWhereTokens($token, &$where)
1335+
protected function parseWhereTokens($token, array &$where = null)
13331336
{
13341337
if (!is_array($where)) {
13351338
$where = [];

lib/MC/Parser/Def.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function getName()
9393
*
9494
* @param string $name
9595
*
96-
* @return Def $this - chainable method
96+
* @return self - chainable method
9797
*/
9898
public function name($name)
9999
{
@@ -104,6 +104,8 @@ public function name($name)
104104

105105
/**
106106
* Toggle suppressing the token from the results.
107+
*
108+
* @return self - chainable method
107109
*/
108110
public function suppress()
109111
{

lib/MC/Parser/Def/IsEmpty.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,20 @@ class IsEmpty extends Def
1111
{
1212
public $suppress = true;
1313

14+
/**
15+
* @param string $str
16+
* @param int $loc
17+
*
18+
* @return array
19+
*/
1420
public function _parse($str, $loc)
1521
{
1622
return [$loc, $this->token(null)];
1723
}
1824

25+
/**
26+
* @return string
27+
*/
1928
public function _name()
2029
{
2130
return 'empty string';

lib/MC/Parser/Def/Literal.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public function _parse($str, $loc)
5555
return [$loc, $this->token($this->search)];
5656
}
5757

58+
/**
59+
* @return string
60+
*/
5861
public function _name()
5962
{
6063
return $this->search;

lib/MC/Parser/Def/NOrMore.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class NOrMore extends Def
1212

1313
public $min;
1414

15+
/**
16+
* @param Def $expr
17+
* @param int $min
18+
*/
1519
public function __construct(Def $expr, $min)
1620
{
1721
$this->expr = $expr;
@@ -51,6 +55,9 @@ public function _parse($str, $loc)
5155
return [$loc, $toks];
5256
}
5357

58+
/**
59+
* @return string
60+
*/
5461
public function _name()
5562
{
5663
return $this->min.' or more: '.$this->expr->getName();

lib/MC/Parser/Def/OneOf.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public function _parse($str, $loc)
6464
return [$max_match, $res];
6565
}
6666

67+
/**
68+
* @return string
69+
*/
6770
public function _name()
6871
{
6972
$names = [];

lib/MC/Parser/Def/Recursive.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public function replace(Def $expr)
4343
return $this;
4444
}
4545

46+
/**
47+
* @return string
48+
*/
4649
public function _name()
4750
{
4851
if (null === $this->replacement) {

lib/MC/Parser/Def/Regex.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class Regex extends Def
1818
public $errstr;
1919
public $retgroup = 0;
2020

21+
/**
22+
* @param null|string $regex
23+
* @param null|string $flags
24+
* @param null|string $errstr
25+
*/
2126
public function __construct($regex = null, $flags = null, $errstr = null)
2227
{
2328
if (null !== $regex) {
@@ -52,9 +57,12 @@ public function _parse($str, $loc)
5257
return [$loc, $this->token($success[0])];
5358
}
5459

60+
/**
61+
* @return string
62+
*/
5563
public function _name()
5664
{
57-
if ($this->errstr) {
65+
if (null !== $this->errstr) {
5866
return $this->errstr;
5967
}
6068

lib/MC/Parser/Def/Set.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Set extends Def
1919
*
2020
* @throws DefError
2121
*/
22-
public function __construct($exprs = [])
22+
public function __construct(array $exprs = [])
2323
{
2424
if (!is_array($exprs)) {
2525
throw new DefError('Set sub-expressions must be an array');
@@ -47,6 +47,9 @@ public function _parse($str, $loc)
4747
return [$loc, $res];
4848
}
4949

50+
/**
51+
* @return string
52+
*/
5053
public function _name()
5154
{
5255
$names = [];

lib/MC/Parser/Token.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,37 @@ class Token
1010
public $name;
1111
public $value;
1212

13+
/**
14+
* Token constructor.
15+
*
16+
* @param mixed $value
17+
* @param null|string $name
18+
*/
1319
public function __construct($value, $name = null)
1420
{
1521
$this->value = $value;
1622
$this->name = $name;
1723
}
1824

25+
/**
26+
* @return array
27+
*/
1928
public function getValues()
2029
{
2130
return [$this->value];
2231
}
2332

33+
/**
34+
* @return array
35+
*/
2436
public function getNameValues()
2537
{
2638
return [[$this->name, $this->value]];
2739
}
2840

41+
/**
42+
* @return bool
43+
*/
2944
public function hasChildren()
3045
{
3146
return false;

0 commit comments

Comments
 (0)