-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpression.php
More file actions
70 lines (58 loc) · 1.74 KB
/
Expression.php
File metadata and controls
70 lines (58 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/*
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2021 David Young
* @license https://github.com/opulencephp/Opulence/blob/1.2/LICENSE.md
*/
namespace Opulence\QueryBuilders;
/**
* 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;
}
}