Skip to content

Commit

Permalink
Release version 1.1.0
Browse files Browse the repository at this point in the history
PHP5 compatible copy of 2.1.0
  • Loading branch information
shadowhand committed Oct 31, 2016
1 parent 362b077 commit 29a42b7
Show file tree
Hide file tree
Showing 42 changed files with 500 additions and 1,720 deletions.
362 changes: 88 additions & 274 deletions src/EasyDB.php

Large diffs are not rendered by default.

152 changes: 47 additions & 105 deletions src/EasyStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,29 @@
namespace ParagonIE\EasyDB;

use RuntimeException;

/**
* Class EasyStatement
* @package ParagonIE\EasyDB
*/
class EasyStatement
{

/**
* @var array
*/
private $parts = [];

/**
* @var EasyStatement
*/
private $parent;

/**
* Open a new statement.
*
* @return self
*/
public static function open(): self
public static function open()
{
return new static();
}

/**
* Alias for andWith().
*
Expand All @@ -39,11 +34,10 @@ public static function open(): self
*
* @return self
*/
public function with(string $condition, ...$values): self
public function with($condition, ...$values)
{
return $this->andWith($condition, ...$values);
}

/**
* Add a condition that will be applied with a logical "AND".
*
Expand All @@ -52,17 +46,11 @@ public function with(string $condition, ...$values): self
*
* @return self
*/
public function andWith(string $condition, ...$values): self
public function andWith($condition, ...$values)
{
$this->parts[] = [
'type' => 'AND',
'condition' => $condition,
'values' => $values,
];

$this->parts[] = ['type' => 'AND', 'condition' => $condition, 'values' => $values];
return $this;
}

/**
* Add a condition that will be applied with a logical "OR".
*
Expand All @@ -71,17 +59,11 @@ public function andWith(string $condition, ...$values): self
*
* @return self
*/
public function orWith(string $condition, ...$values): self
public function orWith($condition, ...$values)
{
$this->parts[] = [
'type' => 'OR',
'condition' => $condition,
'values' => $values,
];

$this->parts[] = ['type' => 'OR', 'condition' => $condition, 'values' => $values];
return $this;
}

/**
* Alias for andIn().
*
Expand All @@ -90,11 +72,10 @@ public function orWith(string $condition, ...$values): self
*
* @return self
*/
public function in(string $condition, array $values): self
public function in($condition, array $values)
{
return $this->andIn($condition, $values);
}

/**
* Add an IN condition that will be applied with a logical "AND".
*
Expand All @@ -105,11 +86,10 @@ public function in(string $condition, array $values): self
*
* @return self
*/
public function andIn(string $condition, array $values): self
public function andIn($condition, array $values)
{
return $this->andWith($this->unpackCondition($condition, \count($values)), ...$values);
}

/**
* Add an IN condition that will be applied with a logical "OR".
*
Expand All @@ -120,69 +100,54 @@ public function andIn(string $condition, array $values): self
*
* @return self
*/
public function orIn(string $condition, array $values): self
public function orIn($condition, array $values)
{
return $this->orWith($this->unpackCondition($condition, \count($values)), ...$values);
}

/**
* Alias for andGroup().
*
* @return self
*/
public function group(): self
public function group()
{
return $this->andGroup();
}

/**
* Start a new grouping that will be applied with a logical "AND".
*
* Exit the group with endGroup().
*
* @return self
*/
public function andGroup(): self
public function andGroup()
{
$group = new self($this);

$this->parts[] = [
'type' => 'AND',
'condition' => $group,
];

$this->parts[] = ['type' => 'AND', 'condition' => $group];
return $group;
}

/**
* Start a new grouping that will be applied with a logical "OR".
*
* Exit the group with endGroup().
*
* @return self
*/
public function orGroup(): self
public function orGroup()
{
$group = new self($this);

$this->parts[] = [
'type' => 'OR',
'condition' => $group,
];

$this->parts[] = ['type' => 'OR', 'condition' => $group];
return $group;
}

/**
* Alias for endGroup().
*
* @return self
*/
public function end(): self
public function end()
{
return $this->endGroup();
}

/**
* Exit the current grouping and return the parent statement.
*
Expand All @@ -191,84 +156,64 @@ public function end(): self
* @throws RuntimeException
* If the current statement has no parent context.
*/
public function endGroup(): self
public function endGroup()
{
if (empty($this->parent)) {
throw new RuntimeException('Already at the top of the statement');
}

return $this->parent;
}

/**
* Compile the current statement into PDO-ready SQL.
*
* @return string
*/
public function sql(): string
public function sql()
{
return \array_reduce(
$this->parts,
function (string $sql, array $part): string {
if ($this->isGroup($part['condition'])) {
// (...)
$statement = '(' . $part['condition']->sql() . ')';
} else {
// foo = ?
$statement = $part['condition'];
}

if ($sql) {
switch ($part['type']) {
case 'AND':
case 'OR':
$statement = $part['type'] . ' ' . $statement;
break;
default:
throw new RuntimeException(
\sprintf('Invalid joiner %s', $part['type'])
);
}
return \array_reduce($this->parts, function ($sql, array $part) {
if ($this->isGroup($part['condition'])) {
// (...)
$statement = '(' . $part['condition']->sql() . ')';
} else {
// foo = ?
$statement = $part['condition'];
}
if ($sql) {
switch ($part['type']) {
case 'AND':
case 'OR':
$statement = $part['type'] . ' ' . $statement;
break;
default:
throw new RuntimeException(\sprintf('Invalid joiner %s', $part['type']));
}

return \trim($sql . ' ' . $statement);
},
''
);
}
return \trim($sql . ' ' . $statement);
}, '');
}

/**
* Get all of the parameters attached to this statement.
*
* @return array
*/
public function values(): array
public function values()
{
return \array_reduce(
$this->parts,
function (array $values, array $part): array {
if ($this->isGroup($part['condition'])) {
return \array_merge(
$values,
$part['condition']->values()
);
}
return \array_merge($values, $part['values']);
},
[]
);
return \array_reduce($this->parts, function (array $values, array $part) {
if ($this->isGroup($part['condition'])) {
return \array_merge($values, $part['condition']->values());
}
return \array_merge($values, $part['values']);
}, []);
}

/**
* Convert the statement to a string.
*
* @return string
*/
public function __toString(): string
public function __toString()
{
return $this->sql();
}

/**
* Don't instantiate directly. Instead, use open() (static method).
*
Expand All @@ -279,23 +224,20 @@ protected function __construct(EasyStatement $parent = null)
{
$this->parent = $parent;
}

/**
* Check if a condition is a sub-group.
*
* @param mixed $condition
*
* @return bool
*/
protected function isGroup($condition): bool
protected function isGroup($condition)
{
if (!\is_object($condition)) {
return false;
}

return $condition instanceof EasyStatement;
}

/**
* Replace a grouped placeholder with a list of placeholders.
*
Expand All @@ -306,10 +248,10 @@ protected function isGroup($condition): bool
*
* @return string
*/
private function unpackCondition(string $condition, int $count): string
private function unpackCondition($condition, $count)
{
// Replace a grouped placeholder with an matching count of placeholders.
$params = '?' . \str_repeat(', ?', $count - 1);
return \str_replace('?*', $params, $condition);
}
}
}
4 changes: 2 additions & 2 deletions src/Exception/ConstructorFailed.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace ParagonIE\EasyDB\Exception;

class ConstructorFailed extends \RuntimeException
{

}
}
3 changes: 2 additions & 1 deletion src/Exception/InvalidIdentifier.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace ParagonIE\EasyDB\Exception;

class InvalidIdentifier extends \InvalidArgumentException
{
}
}
3 changes: 2 additions & 1 deletion src/Exception/QueryError.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace ParagonIE\EasyDB\Exception;

class QueryError extends \RuntimeException
{
}
}

0 comments on commit 29a42b7

Please sign in to comment.