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,17 +356,8 @@ class Builder extends BuilderAbstract
*/ */
public function raw(string $raw) : Builder public function raw(string $raw) : Builder
{ {
if ($this->isReadOnly) { if (!$this->isValidReadOnly($raw)) {
$test = strtolower($raw); throw new \Exception();
if (strpos($test, 'insert') !== false
|| strpos($test, 'update') !== false
|| strpos($test, 'drop') !== false
|| strpos($test, 'delete') !== false
|| strpos($test, 'create') !== false
|| strpos($test, 'alter') !== false) {
throw new \Exception();
}
} }
$this->type = QueryType::RAW; $this->type = QueryType::RAW;
@ -375,6 +366,37 @@ class Builder extends BuilderAbstract
return $this; 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);
if (strpos($test, 'insert') !== false
|| strpos($test, 'update') !== false
|| strpos($test, 'drop') !== false
|| strpos($test, 'delete') !== false
|| strpos($test, 'create') !== false
|| strpos($test, 'alter') !== false
) {
return false;
}
return true;
}
/** /**
* Make raw column selection. * Make raw column selection.
* *
@ -459,36 +481,31 @@ 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)) {
$i = 0; $colums = [$columns];
foreach ($columns as $key => $column) { $operator = [$operator];
if (isset($operator[$i]) && !in_array(strtolower($operator[$i]), self::OPERATORS)) { $values = [$values];
throw new \InvalidArgumentException('Unknown operator.'); $boolean = [$boolean];
} }
$this->wheres[self::getPublicColumnName($column)][] = [ $i = 0;
'column' => $column, foreach ($columns as $key => $column) {
'operator' => $operator[$i], if (isset($operator[$i]) && !in_array(strtolower($operator[$i]), self::OPERATORS)) {
'value' => $values[$i],
'boolean' => $boolean[$i],
];
$i++;
}
} elseif (is_string($columns)) {
if (isset($operator) && !in_array(strtolower($operator), self::OPERATORS)) {
throw new \InvalidArgumentException('Unknown operator.'); throw new \InvalidArgumentException('Unknown operator.');
} }
$this->wheres[self::getPublicColumnName($columns)][] = ['column' => $columns, 'operator' => $operator, 'value' => $values, $this->wheres[self::getPublicColumnName($column)][] = [
'boolean' => $boolean,]; 'column' => $column,
} else { 'operator' => $operator[$i],
throw new \InvalidArgumentException(); 'value' => $values[$i],
'boolean' => $boolean[$i],
];
$i++;
} }
return $this; return $this;

View File

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