Skip to content

Commit

Permalink
Update Parameters.php
Browse files Browse the repository at this point in the history
  • Loading branch information
dezbyte authored Jan 23, 2018
1 parent 4d88b80 commit b4d93e1
Showing 1 changed file with 58 additions and 16 deletions.
74 changes: 58 additions & 16 deletions src/Colibri/Query/Expr/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,68 @@ public function setParameters($parameters)
$this->parameters = [];

foreach ($parameters as $parameter) {
if (is_scalar($parameter)) {
if (is_bool($parameter)) {
$parameter = new Parameter($parameter, Parameter::TYPE_BOOLEAN);
} else if (is_numeric($parameter) || is_float($parameter) || is_double($parameter)) {
$parameter = new Parameter($parameter, Parameter::TYPE_NUMERIC);
} else {
$parameter = new Parameter($parameter, Parameter::TYPE_STR);
}
}

if (!($parameter instanceof Expression)) {
throw new BadArgumentException(sprintf('Bad parameter passed. Allowed either scalar values or object %s extended',
Expression::class));
}

$this->parameters[] = $parameter;
$this->append($parameter);
}

return $this;
}

/**
* @param $parameter
* @return $this
*/
public function append($parameter)
{
array_push($this->parameters, $this->normalizeParameter($parameter));

return $this;
}

/**
* @param $parameter
* @return $this
*/
public function prepend($parameter)
{
array_unshift($this->parameters, $this->normalizeParameter($parameter));

return $this;
}

/**
* @return $this
*/
public function clearParameters()
{
$this->parameters = [];

return $this;
}

/**
* @param string|int|Expression $parameter
* @return Parameter
* @throws BadArgumentException
*/
private function normalizeParameter($parameter)
{
if (is_scalar($parameter)) {
if (is_bool($parameter)) {
$parameter = new Parameter($parameter, Parameter::TYPE_BOOLEAN);
} else if (is_numeric($parameter) || is_float($parameter) || is_double($parameter)) {
$parameter = new Parameter($parameter, Parameter::TYPE_NUMERIC);
} else {
$parameter = new Parameter($parameter, Parameter::TYPE_STR);
}
}

if (!($parameter instanceof Expression)) {
throw new BadArgumentException(sprintf('Bad parameter passed. Allowed either scalar values or object %s extended',
Expression::class));
}

return $parameter;
}

/**
* @return string
Expand Down

0 comments on commit b4d93e1

Please sign in to comment.