-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
150 additions
and
122 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
Classes/VirtualObject/Persistence/Backend/AbstractSqlExpression.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Cundd\Rest\VirtualObject\Persistence\Backend; | ||
|
||
use Cundd\Rest\VirtualObject\Persistence\QueryInterface; | ||
|
||
abstract class AbstractSqlExpression implements SqlExpressionInterface | ||
{ | ||
private $expression = ''; | ||
private $boundVariables = []; | ||
|
||
/** | ||
* Expression constructor | ||
* | ||
* @param string $sqlExpression | ||
* @param array $boundVariables | ||
*/ | ||
public function __construct(string $sqlExpression = '', array $boundVariables = []) | ||
{ | ||
$this->expression = $sqlExpression; | ||
$this->boundVariables = $boundVariables; | ||
} | ||
|
||
public function setExpression(string $expression): SqlExpressionInterface | ||
{ | ||
$this->expression = $expression; | ||
|
||
return $this; | ||
} | ||
|
||
public function appendSql( | ||
string $expression, | ||
string $combinator = QueryInterface::COMBINATOR_AND | ||
): SqlExpressionInterface { | ||
$this->assertCombinator($combinator); | ||
|
||
if ($this->expression) { | ||
$this->expression .= ' ' . strtoupper($combinator) . ' ' . $expression; | ||
} else { | ||
$this->expression = $expression; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function getExpression(): string | ||
{ | ||
return $this->expression; | ||
} | ||
|
||
public function getBoundVariables(): array | ||
{ | ||
return $this->boundVariables; | ||
} | ||
|
||
public function bindVariable(string $key, $value): SqlExpressionInterface | ||
{ | ||
$this->boundVariables[$key] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return $this->getExpression(); | ||
} | ||
|
||
/** | ||
* @param $combinator | ||
*/ | ||
public static function assertCombinator($combinator) | ||
{ | ||
if (!is_string($combinator)) { | ||
throw new \InvalidArgumentException( | ||
sprintf( | ||
'Logical combinator must be of type string, \'%s\' given', | ||
is_object($combinator) ? get_class($combinator) : gettype($combinator) | ||
) | ||
); | ||
} | ||
if (!in_array(strtoupper($combinator), [QueryInterface::COMBINATOR_AND, QueryInterface::COMBINATOR_OR])) { | ||
throw new \InvalidArgumentException( | ||
sprintf( | ||
'Logical combinator must be either \'%s\' or \'%s\'', | ||
QueryInterface::COMBINATOR_AND, | ||
QueryInterface::COMBINATOR_OR | ||
) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Classes/VirtualObject/Persistence/Backend/SqlExpressionInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Cundd\Rest\VirtualObject\Persistence\Backend; | ||
|
||
use Cundd\Rest\VirtualObject\Persistence\QueryInterface; | ||
|
||
interface SqlExpressionInterface | ||
{ | ||
/** | ||
* Set the SQL expression string | ||
* | ||
* @param string $expression | ||
* @return self | ||
*/ | ||
public function setExpression(string $expression): self; | ||
|
||
/** | ||
* Append the string to the SQL expression | ||
* | ||
* @param string $expression | ||
* @param string $combinator | ||
* @return self | ||
*/ | ||
public function appendSql(string $expression, string $combinator = QueryInterface::COMBINATOR_AND): self; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getExpression(): string; | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getBoundVariables(): array; | ||
|
||
/** | ||
* Bind the variable to the SQL WHERE-clause | ||
* | ||
* @param string $key | ||
* @param string|int|float $value | ||
* @return self | ||
*/ | ||
public function bindVariable(string $key, $value): self; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters