Skip to content

Commit

Permalink
Merge pull request #452 from AntonShevchuk/master
Browse files Browse the repository at this point in the history
Optimization of Db package
  • Loading branch information
Anton authored Nov 24, 2017
2 parents 6f2bb48 + e706c54 commit 6400d00
Show file tree
Hide file tree
Showing 50 changed files with 202 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/Controller/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public function setParam($param) : void
return;
}

list($type, $key) = preg_split('/[ $]+/', $param);
[$type, $key] = preg_split('/[ $]+/', $param);

$this->params[$key] = trim($type);
}
Expand Down
11 changes: 9 additions & 2 deletions src/Db/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ public static function getMeta() : array

$meta = DbProxy::fetchUniqueGroup(
'
SELECT COLUMN_NAME, DATA_TYPE, COLUMN_DEFAULT, COLUMN_KEY
SELECT
COLUMN_NAME AS `name`,
DATA_TYPE AS `type`,
COLUMN_DEFAULT AS `default`,
COLUMN_KEY AS `key`
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = ?
AND TABLE_NAME = ?',
Expand Down Expand Up @@ -287,8 +291,11 @@ function ($value) use ($self) {
);
$keyValue = implode(',', $keyValue);
$whereAndTerms[] = $self->name . '.' . $keyName . ' IN (' . $keyValue . ')';
} elseif (is_null($keyValue)) {
} elseif (null === $keyValue) {
$whereAndTerms[] = $self->name . '.' . $keyName . ' IS NULL';
} elseif ('%' === $keyValue{0} || '%' === $keyValue{-1}) {
$whereAndTerms[] = $self->name . '.' . $keyName . ' LIKE ?';
$whereParams[] = $keyValue;
} else {
$whereAndTerms[] = $self->name . '.' . $keyName . ' = ?';
$whereParams[] = $keyValue;
Expand Down
10 changes: 7 additions & 3 deletions src/Db/TableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ public static function find(...$keys) : array;
/**
* Find rows by WHERE
* // WHERE alias = 'foo'
* Table::findWhere(['alias'=>'foo']);
* Table::findWhere(['alias' => 'foo']);
* // WHERE alias IS NULL
* Table::findWhere(['alias' => null]);
* // WHERE alias IN ('foo', 'bar')
* Table::findWhere(['alias' => ['foo', 'bar']]);
* // WHERE alias LIKE 'foo%'
* Table::findWhere(['alias' => 'foo%']);
* // WHERE alias = 'foo' OR 'alias' = 'bar'
* Table::findWhere(['alias'=>'foo'], ['alias'=>'bar']);
* // WHERE (alias = 'foo' AND userId = 2) OR ('alias' = 'bar' AND userId = 4)
* Table::findWhere(['alias'=>'foo', 'userId'=> 2], ['alias'=>'foo', 'userId'=>4]);
* // WHERE alias IN ('foo', 'bar')
* Table::findWhere(['alias'=> ['foo', 'bar']]);
*
* @param mixed ...$where
*
Expand Down
2 changes: 1 addition & 1 deletion src/Proxy/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public static function getAccept(): array
// check if there is a different quality
if (strpos($a, ';q=') or strpos($a, '; q=')) {
// divide "mime/type;q=X" into two parts: "mime/type" i "X"
list($a, $q) = preg_split('/;([ ]?)q=/', $a);
[$a, $q] = preg_split('/;([ ]?)q=/', $a);
}
// remove other extension
if (strpos($a, ';')) {
Expand Down
3 changes: 1 addition & 2 deletions src/Response/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ public function render($type = 'HTML') : string
case 'JSON':
return $this->jsonSerialize();
case 'HTML':
return $this->__toString();
default:
return '';
return $this->__toString();
}
}
}
2 changes: 1 addition & 1 deletion src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function __construct()
$reverse = Cache::get('router.reverse');

if (!$routers || !$reverse) {
list($routers, $reverse) = $this->prepareRouterData();
[$routers, $reverse] = $this->prepareRouterData();
Cache::set('router.routers', $routers, Cache::TTL_NO_EXPIRY, ['system']);
Cache::set('router.reverse', $reverse, Cache::TTL_NO_EXPIRY, ['system']);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Validator/Exception/ValidatorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ class ValidatorException extends BadRequestException
/**
* @return array
*/
public function getErrors(): array
public function getErrors() : array
{
return $this->errors;
}

/**
* @param array $errors
*/
public function setErrors(array $errors)
public function setErrors(array $errors) : void
{
$this->errors = $errors;
}
Expand All @@ -54,7 +54,7 @@ public function setErrors(array $errors)
*
* @return void
*/
public function setError($name, $message)
public function setError($name, $message) : void
{
$this->errors[$name] = $message;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/AbstractCompareRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ abstract class AbstractCompareRule extends AbstractRule
*
* @return bool
*/
protected function less($what, $than): bool
protected function less($what, $than) : bool
{
if ($this->inclusive) {
return $what <= $than;
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/AbstractFilterRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function filter($input) : string
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
if (!is_scalar($input)) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class AbstractRule implements RuleInterface
/**
* @inheritdoc
*/
public function assert($input)
public function assert($input) : void
{
if (!$this->validate($input)) {
throw new ValidatorException($this->description);
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/AlphaRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AlphaRule extends AbstractCtypeRule
*
* @return bool
*/
protected function validateClean($input): bool
protected function validateClean($input) : bool
{
return ctype_alpha($input);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Validator/Rule/ArrayRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public function __construct(callable $callback)
/**
* Check input data
*
* @param string $input
* @param mixed $input
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
if (!is_array($input)) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/BetweenRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct($min, $max)
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
return $this->less($this->minValue, $input)
&& $this->less($input, $this->maxValue);
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/CallbackRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(callable $callback)
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
return (bool)call_user_func($this->callback, $input);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/ConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct($condition)
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
return (bool)$this->condition;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/ContainsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct($containsValue)
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
// for array
if (is_array($input)) {
Expand Down
12 changes: 1 addition & 11 deletions src/Validator/Rule/ContainsStrictRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ContainsStrictRule extends ContainsRule
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
// for array
if (is_array($input)) {
Expand All @@ -36,14 +36,4 @@ public function validate($input): bool
}
return false;
}

/**
* Get error template
*
* @return string
*/
public function getDescription() : string
{
return __('must contain the value "%s"', $this->containsValue);
}
}
2 changes: 1 addition & 1 deletion src/Validator/Rule/CountryCodeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class CountryCodeRule extends AbstractRule
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
return in_array(strtoupper($input), $this->countryCodeList, true);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Validator/Rule/CreditCardRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CreditCardRule extends AbstractRule
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
$input = preg_replace('([ \.-])', '', $input);

Expand All @@ -55,7 +55,7 @@ private function verifyMod10($input)
$inputLen = strlen($input);
for ($i = 0; $i < $inputLen; $i++) {
$current = $input[$i];
if ($i % 2 == 1) {
if ($i % 2 === 1) {
$current *= 2;
if ($current > 9) {
$firstDigit = $current % 10;
Expand All @@ -66,6 +66,6 @@ private function verifyMod10($input)
$sum += $current;
}

return ($sum % 10 == 0);
return ($sum % 10 === 0);
}
}
2 changes: 1 addition & 1 deletion src/Validator/Rule/DateRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct($format = null)
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
if ($input instanceof DateTime) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/DomainRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct($checkDns = false)
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
$input = (string)$input;
// check by regular expression
Expand Down
4 changes: 2 additions & 2 deletions src/Validator/Rule/EmailRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public function __construct($checkDns = false)
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
if (is_string($input) && filter_var($input, FILTER_VALIDATE_EMAIL)) {
list(, $domain) = explode('@', $input, 2);
[, $domain] = explode('@', $input, 2);
if ($this->checkDns) {
return checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/EqualsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct($compareTo)
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
return $input == $this->compareTo;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/EqualsStrictRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct($compareTo)
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
return $input === $this->compareTo;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/FloatRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FloatRule extends AbstractRule
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
return is_float(filter_var($input, FILTER_VALIDATE_FLOAT));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/InRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct($haystack)
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
if (is_array($this->haystack)) {
return in_array($input, $this->haystack, false);
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/InStrictRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class InStrictRule extends InRule
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
if (is_array($this->haystack)) {
return in_array($input, $this->haystack, true);
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Rule/IntegerRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class IntegerRule extends AbstractRule
*
* @return bool
*/
public function validate($input): bool
public function validate($input) : bool
{
return is_numeric($input) && (int)$input == $input;
}
Expand Down
Loading

0 comments on commit 6400d00

Please sign in to comment.