Reducing complexity

This commit is contained in:
Dennis Eichhorn 2017-10-29 14:03:32 +01:00
parent 7e11c3ede3
commit 3442d1d817
2 changed files with 56 additions and 39 deletions

View File

@ -356,7 +356,32 @@ class Builder extends BuilderAbstract
*/ */
public function raw(string $raw) : Builder public function raw(string $raw) : Builder
{ {
if ($this->isReadOnly) { if (!$this->isValidReadOnly($raw)) {
throw new \Exception();
}
$this->type = QueryType::RAW;
$this->raw = rtrim($raw, ';');
return $this;
}
/**
* Tests if a string contains a non read only component in case the builder is read only.
* If the builder is not read only it will always return true
*
* @param string $raw Raw query
*
* @return bool
*
* @since 1.0.0
*/
private function isValidReadOnly($raw) : bool
{
if (!$this->isReadOnly) {
return true;
}
$test = strtolower($raw); $test = strtolower($raw);
if (strpos($test, 'insert') !== false if (strpos($test, 'insert') !== false
@ -364,15 +389,12 @@ class Builder extends BuilderAbstract
|| strpos($test, 'drop') !== false || strpos($test, 'drop') !== false
|| strpos($test, 'delete') !== false || strpos($test, 'delete') !== false
|| strpos($test, 'create') !== false || strpos($test, 'create') !== false
|| strpos($test, 'alter') !== false) { || strpos($test, 'alter') !== false
throw new \Exception(); ) {
} return false;
} }
$this->type = QueryType::RAW; return true;
$this->raw = rtrim($raw, ';');
return $this;
} }
/** /**
@ -459,12 +481,17 @@ class Builder extends BuilderAbstract
*/ */
public function where($columns, $operator = null, $values = null, $boolean = 'and') : Builder public function where($columns, $operator = null, $values = null, $boolean = 'and') : Builder
{ {
// TODO: handle $value is null -> operator NULL
if (isset($operator) && !is_array($operator) && !in_array(strtolower($operator), self::OPERATORS)) { if (isset($operator) && !is_array($operator) && !in_array(strtolower($operator), self::OPERATORS)) {
throw new \InvalidArgumentException('Unknown operator.'); throw new \InvalidArgumentException('Unknown operator.');
} }
if (is_array($columns)) { if (is_string($columns)) {
$colums = [$columns];
$operator = [$operator];
$values = [$values];
$boolean = [$boolean];
}
$i = 0; $i = 0;
foreach ($columns as $key => $column) { foreach ($columns as $key => $column) {
if (isset($operator[$i]) && !in_array(strtolower($operator[$i]), self::OPERATORS)) { if (isset($operator[$i]) && !in_array(strtolower($operator[$i]), self::OPERATORS)) {
@ -480,16 +507,6 @@ class Builder extends BuilderAbstract
$i++; $i++;
} }
} elseif (is_string($columns)) {
if (isset($operator) && !in_array(strtolower($operator), self::OPERATORS)) {
throw new \InvalidArgumentException('Unknown operator.');
}
$this->wheres[self::getPublicColumnName($columns)][] = ['column' => $columns, 'operator' => $operator, 'value' => $values,
'boolean' => $boolean,];
} else {
throw new \InvalidArgumentException();
}
return $this; return $this;
} }

View File

@ -71,18 +71,18 @@ class Header extends HeaderAbstract
throw new LockException('HTTP header'); throw new LockException('HTTP header');
} }
$key = strtolower($key);
if (!$overwrite && isset($this->header[$key])) {
return false;
} elseif ($overwrite || !isset($this->header[$key])) {
if (self::isSecurityHeader($key) && isset($this->header[$key])) { if (self::isSecurityHeader($key) && isset($this->header[$key])) {
throw new \Exception('Cannot change security headers.'); throw new \Exception('Cannot change security headers.');
} }
unset($this->header[$key]); $key = strtolower($key);
if (!$overwrite && isset($this->header[$key])) {
return false;
} }
unset($this->header[$key]);
if (!isset($this->header[$key])) { if (!isset($this->header[$key])) {
$this->header[$key] = []; $this->header[$key] = [];
} }