From b4d93e1d5c9ce8d5927c725b97f31cc0a6185989 Mon Sep 17 00:00:00 2001 From: Ivan Hontarenko <17449370+dezzPro@users.noreply.github.com> Date: Tue, 23 Jan 2018 20:05:28 +0200 Subject: [PATCH] Update Parameters.php --- src/Colibri/Query/Expr/Parameters.php | 74 +++++++++++++++++++++------ 1 file changed, 58 insertions(+), 16 deletions(-) diff --git a/src/Colibri/Query/Expr/Parameters.php b/src/Colibri/Query/Expr/Parameters.php index f53a6f6..65a0649 100644 --- a/src/Colibri/Query/Expr/Parameters.php +++ b/src/Colibri/Query/Expr/Parameters.php @@ -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