-
Notifications
You must be signed in to change notification settings - Fork 45
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
10 changed files
with
209 additions
and
30 deletions.
There are no files selected for viewing
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,72 @@ | ||
<?php | ||
|
||
/* | ||
* Opulence | ||
* | ||
* @link https://www.opulencephp.com | ||
* @copyright Copyright (C) 2017 David Young | ||
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md | ||
*/ | ||
|
||
namespace Opulence\QueryBuilders\Expression; | ||
|
||
use Opulence\QueryBuilders\InvalidQueryException; | ||
|
||
/** | ||
* Expression is designed to be used for setting values in INSERT and UPDATE statements | ||
* It is not intended to be used in WHERE clauses or as columns in SELECT queries | ||
*/ | ||
class Expression | ||
{ | ||
/** @var string The expression to use */ | ||
protected $expression = ''; | ||
|
||
/** @var array[] */ | ||
protected $values = []; | ||
|
||
/** | ||
* Expression constructor. | ||
* | ||
* @param string $expression | ||
* @param mixed ...$values | ||
* | ||
* @throws InvalidQueryException | ||
*/ | ||
public function __construct(string $expression, ...$values) | ||
{ | ||
$this->expression = $expression; | ||
|
||
foreach ($values as $value) { | ||
if (is_scalar($value)) { | ||
$value = [$value, \PDO::PARAM_STR]; | ||
} | ||
|
||
if (!is_array($value) || count($value) !== 2) { | ||
throw new InvalidQueryException('Incorrect number of items in expression value array'); | ||
} | ||
|
||
if (!array_key_exists(0, $value) || !array_key_exists(1, $value)) { | ||
throw new InvalidQueryException('Incorrect keys in expression value array'); | ||
} | ||
|
||
if (!is_scalar($value[0]) || !is_numeric($value[1]) || $value[1] < 0) { | ||
throw new InvalidQueryException('Incorrect expression values'); | ||
} | ||
|
||
$this->values[] = $value; | ||
} | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getParameters() : array | ||
{ | ||
return $this->values; | ||
} | ||
|
||
public function getSql() : string | ||
{ | ||
return $this->expression; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.