Skip to content

Commit

Permalink
Add fatal user error if invalid method is called
Browse files Browse the repository at this point in the history
Add $validMethods property to CommonQuery to catch invalid calls
Rename $clause to $name to better represent incoming data
  • Loading branch information
cbornhoft committed Oct 12, 2017
1 parent b172d58 commit 7d70c3a
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions FluentPDO/CommonQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
abstract class CommonQuery extends BaseQuery
{
/** @var array - methods which are allowed to be call by the magic method __call() */
private $validMethods = ['from', 'fullJoin', 'group', 'groupBy', 'having', 'innerJoin', 'join', 'leftJoin',
'limit', 'offset', 'order', 'orderBy', 'outerJoin', 'rightJoin', 'select'];

/** @var array - Query tables (also include table from clause FROM) */
protected $joins = array();
Expand Down Expand Up @@ -102,13 +105,17 @@ public function where($condition, $parameters = array()) {
}

/**
* @param $clause
* @param array $parameters - first is $statement followed by $parameters
* @param string $name
* @param array $parameters - first is $statement followed by $parameters
*
* @return $this|\SelectQuery
* @return $this|SelectQuery
*/
public function __call($clause, $parameters = array()) {
$clause = FluentUtils::toUpperWords($clause);
public function __call($name, $parameters = array()) {
if (!in_array($name, $this->validMethods)) {
trigger_error("Call to invalid method " . get_class($this) . "::{$name}()", E_USER_ERROR);
}

$clause = FluentUtils::toUpperWords($name);

if ($clause == 'GROUP') {
$clause = 'GROUP BY';
Expand Down

0 comments on commit 7d70c3a

Please sign in to comment.