Skip to content

Commit

Permalink
removed bindings count check
Browse files Browse the repository at this point in the history
  • Loading branch information
FacedSID committed Sep 5, 2017
1 parent 1c495d8 commit 08eabd9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/Exceptions/QueryMapperException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class QueryMapperException extends \Exception
{
public static function wrongBindingsNumber($inQuery, $count)
{
return new static('Wrong bindings count. Bindings found in query: '.$inQuery.' and given '.$count);
}

public static function multipleBindingsType()
{
return new static('Both named and unnamed bindings found');
Expand Down
28 changes: 28 additions & 0 deletions src/Query/Mapper/AbstractMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,30 @@ abstract class AbstractMapper
*/
protected function checkBindings(string $query, array $bindings)
{
$bindingsCountInQuery = $this->countBindingsFromQuery($query);
$bindingsCount = count($bindings);

if ($bindingsCountInQuery !== $bindingsCount) {
throw QueryMapperException::wrongBindingsNumber($bindingsCountInQuery, $bindingsCount);
}

$this->checkBindingsPolicy($bindings);
}

/**
* Counts bindings in query by given pattern.
*
* @param string $query
*
* @return int
*/
protected function countBindingsFromQuery(string $query)
{
preg_match_all($this->getBindingPattern(), $query, $matches);

return count($matches[0] ?? []);
}

/**
* Escapes values.
*
Expand All @@ -40,6 +61,13 @@ protected function escapeBindings(array $bindings): array
return $bindings;
}

/**
* Should return pattern to count bindings in query.
*
* @return string
*/
abstract protected function getBindingPattern(): string;

/**
* Should check bindings policy.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Query/Mapper/NamedMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function bind(string $query, array $bindings): string
return $query;
}

protected function getBindingPattern(): string
{
return '/:[a-zA-Z0-9]+/';
}

protected function checkBindingsPolicy(array $bindings)
{
$keys = array_keys($bindings);
Expand Down
14 changes: 7 additions & 7 deletions src/Query/Mapper/UnnamedMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ class UnnamedMapper extends AbstractMapper implements QueryMapperInterface
public function bind(string $query, array $bindings): string
{
$this->checkBindings($query, $bindings);
$escapedBindings = $this->escapeBindings($bindings);

if (!empty($bindings)) {
$escapedBindings = $this->escapeBindings($bindings);
$query = str_replace('?', '%s', $query);

$query = str_replace('?', '%s', $query);

return call_user_func_array('sprintf', array_merge([$query], $escapedBindings));
}
return call_user_func_array('sprintf', array_merge([$query], $escapedBindings));
}

return $query;
protected function getBindingPattern(): string
{
return '/\?/';
}

protected function checkBindingsPolicy(array $bindings)
Expand Down

0 comments on commit 08eabd9

Please sign in to comment.