Skip to content

Commit

Permalink
Enables your own set of query parameters. Type hint functions paramet…
Browse files Browse the repository at this point in the history
…ers.
  • Loading branch information
ddebin committed Jul 8, 2019
1 parent 7258546 commit 94e3de8
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 41 deletions.
61 changes: 32 additions & 29 deletions lib/MC/Google/Visualization.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Visualization
*
* @throws Visualization_Error
*/
public function __construct($db = null, $dialect = 'mysql')
public function __construct(PDO $db = null, $dialect = 'mysql')
{
if (!function_exists('json_encode')) {
throw new Visualization_Error('You must include the PHP json extension installed to use the MC Google Visualization Server');
Expand All @@ -90,15 +90,12 @@ public function __construct($db = null, $dialect = 'mysql')
/**
* Set the database connection to use when handling the entire request or getting pivot values.
*
* @param null|mixed|PDO $db the database connection to use - or null if you want to handle your own queries
* @param null|PDO $db the database connection to use - or null if you want to handle your own queries
*
* @throws Visualization_Error
*/
public function setDB($db = null)
public function setDB(PDO $db = null)
{
if (null !== $db && !($db instanceof PDO)) {
throw new Visualization_Error('You must give a PDO database connection');
}
if (null !== $db) {
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
Expand Down Expand Up @@ -142,19 +139,25 @@ public function setDefaultFormat($type, $format)
}

/**
* Handle the entire request, pulling the query from the $_GET variables, and printing the results directly.
* Handle the entire request, pulling the query from the $_GET variables and printing the results directly
* if not specified otherwise.
*
* @param bool $echo print response and set header
* @param bool $echo print response and set header
* @param array $query_params query parameters
*
* @throws Visualization_Error
*
* @return string the javascript response
*/
public function handleRequest($echo = true)
public function handleRequest($echo = true, array $query_params = null)
{
$query = $_GET['tq'];
if (null === $query_params) {
$query_params = $_GET;
}

$query = $query_params['tq'];
$params = ['version' => $this->version, 'responseHandler' => 'google.visualization.Query.setResponse'];
$paramlist = explode(';', $_GET['tqx']);
$paramlist = explode(';', $query_params['tqx']);
foreach ($paramlist as $paramstr) {
list($name, $val) = explode(':', $paramstr);
$params[$name] = $val;
Expand All @@ -166,8 +169,8 @@ public function handleRequest($echo = true)
throw new Visualization_Error('Data Source version '.$params['version'].' is unsupported at this time');
}

if (isset($_GET['responseHandler'])) {
$params['responseHandler'] = $_GET['responseHandler'];
if (isset($query_params['responseHandler'])) {
$params['responseHandler'] = $query_params['responseHandler'];
}

$response = $this->handleQuery($query, $params);
Expand All @@ -188,7 +191,7 @@ public function handleRequest($echo = true)
*
* @return string the javascript response
*/
public function handleQuery($query, $params)
public function handleQuery($query, array $params)
{
$reqid = null;
$response = '';
Expand Down Expand Up @@ -261,7 +264,7 @@ public function handleError($reqid, $detail_msg, $handler = 'google.visualizatio
*
* @return string the SQL version of the visualization query
*/
public function generateSQL(&$meta)
public function generateSQL(array &$meta)
{
if (!isset($meta['query_fields'])) {
$meta['query_fields'] = $meta['select'];
Expand Down Expand Up @@ -422,7 +425,7 @@ public function generateSQL(&$meta)
*
* @return array the metadata array from merging the query with the entity table definitions
*/
public function generateMetadata($query)
public function generateMetadata(array $query)
{
$meta = [];
if (!isset($query['from']) && null === $this->default_entity) {
Expand Down Expand Up @@ -720,7 +723,7 @@ public function parseQuery($str)
*
* @throws Visualization_Error
*/
public function addEntity($name, $spec = [])
public function addEntity($name, array $spec = [])
{
$entity = ['table' => isset($spec['table']) ? $spec['table'] : $name, 'fields' => [], 'joins' => []];
$this->entities[$name] = $entity;
Expand Down Expand Up @@ -751,7 +754,7 @@ public function addEntity($name, $spec = [])
*
* @throws Visualization_Error
*/
public function addEntityField($entity, $field, $spec)
public function addEntityField($entity, $field, array $spec)
{
if (!isset($spec['field']) && !isset($spec['callback'])) {
throw new Visualization_Error('Entity fields must either be mapped to database fields or given callback functions');
Expand Down Expand Up @@ -828,7 +831,7 @@ public function setDefaultEntity($default = null)
*
* @return string the initial output string for a successful query
*/
public function getSuccessInit($meta)
public function getSuccessInit(array $meta)
{
$handler = $meta['req_params']['responseHandler'] ?: 'google.visualization.Query.setResponse';
$version = $meta['req_params']['version'] ?: $this->version;
Expand All @@ -845,7 +848,7 @@ public function getSuccessInit($meta)
*
* @return string
*/
public function getTableInit($meta)
public function getTableInit(array $meta)
{
$field_init = [];
foreach ($meta['select'] as $field) {
Expand Down Expand Up @@ -915,7 +918,7 @@ public function getTableInit($meta)
*
* @return string the string fragment to include in the results back to the javascript client
*/
public function getRowValues($row, $meta)
public function getRowValues(array $row, array $meta)
{
$vals = [];
foreach ($meta['select'] as $field) {
Expand Down Expand Up @@ -1235,7 +1238,7 @@ protected function getFieldQuote()
*
* @return string the SQL string for this field, with an op
*/
protected function getFieldSQL($name, $spec, $alias = false, $func = null, $pivot = null, $pivot_fields = null)
protected function getFieldSQL($name, $spec, $alias = false, $func = null, array $pivot = null, array $pivot_fields = null)
{
$sql = $spec['field'];
$q = $this->getFieldQuote();
Expand Down Expand Up @@ -1272,7 +1275,7 @@ protected function getFieldSQL($name, $spec, $alias = false, $func = null, $pivo
*
* @throws Visualization_Error
*/
protected function addDependantCallbackFields($field, $entity, &$meta)
protected function addDependantCallbackFields(array $field, array $entity, array &$meta)
{
foreach ($field['fields'] as $dependant) {
if (!isset($entity['fields'][$dependant])) {
Expand All @@ -1295,10 +1298,10 @@ protected function addDependantCallbackFields($field, $entity, &$meta)
/**
* Helper method for the query parser to recursively scan the delimited list of select fields.
*
* @param Token $token the token or token group to recursively parse
* @param array $fields the collector array reference to receive the flattened select field values
* @param Token $token the token or token group to recursively parse
* @param null|array $fields the collector array reference to receive the flattened select field values
*/
protected function parseFieldTokens($token, &$fields)
protected function parseFieldTokens($token, array &$fields = null)
{
if ('*' === $token->value) {
return;
Expand Down Expand Up @@ -1326,10 +1329,10 @@ protected function parseFieldTokens($token, &$fields)
/**
* Helper method for the query parser to recursively scan and flatten the where clause's conditions.
*
* @param Token $token the token or token group to parse
* @param array $where the collector array of tokens that make up the where clause
* @param Token $token the token or token group to parse
* @param null|array $where the collector array of tokens that make up the where clause
*/
protected function parseWhereTokens($token, &$where)
protected function parseWhereTokens($token, array &$where = null)
{
if (!is_array($where)) {
$where = [];
Expand Down
4 changes: 3 additions & 1 deletion lib/MC/Parser/Def.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function getName()
*
* @param string $name
*
* @return Def $this - chainable method
* @return self - chainable method
*/
public function name($name)
{
Expand All @@ -104,6 +104,8 @@ public function name($name)

/**
* Toggle suppressing the token from the results.
*
* @return self - chainable method
*/
public function suppress()
{
Expand Down
9 changes: 9 additions & 0 deletions lib/MC/Parser/Def/IsEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ class IsEmpty extends Def
{
public $suppress = true;

/**
* @param string $str
* @param int $loc
*
* @return array
*/
public function _parse($str, $loc)
{
return [$loc, $this->token(null)];
}

/**
* @return string
*/
public function _name()
{
return 'empty string';
Expand Down
3 changes: 3 additions & 0 deletions lib/MC/Parser/Def/Literal.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public function _parse($str, $loc)
return [$loc, $this->token($this->search)];
}

/**
* @return string
*/
public function _name()
{
return $this->search;
Expand Down
7 changes: 7 additions & 0 deletions lib/MC/Parser/Def/NOrMore.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class NOrMore extends Def

public $min;

/**
* @param Def $expr
* @param int $min
*/
public function __construct(Def $expr, $min)
{
$this->expr = $expr;
Expand Down Expand Up @@ -51,6 +55,9 @@ public function _parse($str, $loc)
return [$loc, $toks];
}

/**
* @return string
*/
public function _name()
{
return $this->min.' or more: '.$this->expr->getName();
Expand Down
3 changes: 3 additions & 0 deletions lib/MC/Parser/Def/OneOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public function _parse($str, $loc)
return [$max_match, $res];
}

/**
* @return string
*/
public function _name()
{
$names = [];
Expand Down
3 changes: 3 additions & 0 deletions lib/MC/Parser/Def/Recursive.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public function replace(Def $expr)
return $this;
}

/**
* @return string
*/
public function _name()
{
if (null === $this->replacement) {
Expand Down
10 changes: 9 additions & 1 deletion lib/MC/Parser/Def/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class Regex extends Def
public $errstr;
public $retgroup = 0;

/**
* @param null|string $regex
* @param null|string $flags
* @param null|string $errstr
*/
public function __construct($regex = null, $flags = null, $errstr = null)
{
if (null !== $regex) {
Expand Down Expand Up @@ -52,9 +57,12 @@ public function _parse($str, $loc)
return [$loc, $this->token($success[0])];
}

/**
* @return string
*/
public function _name()
{
if ($this->errstr) {
if (null !== $this->errstr) {
return $this->errstr;
}

Expand Down
5 changes: 4 additions & 1 deletion lib/MC/Parser/Def/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Set extends Def
*
* @throws DefError
*/
public function __construct($exprs = [])
public function __construct(array $exprs = [])
{
if (!is_array($exprs)) {
throw new DefError('Set sub-expressions must be an array');
Expand Down Expand Up @@ -47,6 +47,9 @@ public function _parse($str, $loc)
return [$loc, $res];
}

/**
* @return string
*/
public function _name()
{
$names = [];
Expand Down
15 changes: 15 additions & 0 deletions lib/MC/Parser/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,37 @@ class Token
public $name;
public $value;

/**
* Token constructor.
*
* @param mixed $value
* @param null|string $name
*/
public function __construct($value, $name = null)
{
$this->value = $value;
$this->name = $name;
}

/**
* @return array
*/
public function getValues()
{
return [$this->value];
}

/**
* @return array
*/
public function getNameValues()
{
return [[$this->name, $this->value]];
}

/**
* @return bool
*/
public function hasChildren()
{
return false;
Expand Down
Loading

0 comments on commit 94e3de8

Please sign in to comment.