Skip to content

Commit f5c8f38

Browse files
committed
Improved creating WHERE conditions
1 parent ffe4b09 commit f5c8f38

20 files changed

+1048
-133
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
],
1111
"require": {
1212
"php": "^5.6",
13-
"pechynho/php-utils": "0.0.1",
13+
"pechynho/php-utils": "0.0.2",
1414
"ext-pdo": "*"
1515
},
1616
"autoload": {

src/Criteria/AbstractCriterion.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
4+
namespace Pechynho\DbWrap\Criteria;
5+
6+
/**
7+
* @author Jan Pech <[email protected]>
8+
*/
9+
abstract class AbstractCriterion implements ICriterion
10+
{
11+
/** @var string */
12+
protected $table;
13+
14+
/** @var string */
15+
protected $column;
16+
17+
/**
18+
* AbstractCriterion constructor.
19+
* @param string $column
20+
*/
21+
public function __construct($column)
22+
{
23+
$this->column = $column;
24+
}
25+
26+
/**
27+
* @return string
28+
*/
29+
public function getColumn()
30+
{
31+
return $this->column;
32+
}
33+
34+
/**
35+
* @param string $column
36+
* @return self
37+
*/
38+
public function setColumn($column)
39+
{
40+
$this->column = $column;
41+
return $this;
42+
}
43+
44+
/**
45+
* @return string
46+
*/
47+
public function getTable()
48+
{
49+
return $this->table;
50+
}
51+
52+
/**
53+
* @param string $table
54+
* @return self
55+
*/
56+
public function setTable($table)
57+
{
58+
$this->table = $table;
59+
return $this;
60+
}
61+
62+
/**
63+
* @return string
64+
*/
65+
protected function getColumnForExpression()
66+
{
67+
$columnForExpression = "";
68+
if (!empty($this->table))
69+
{
70+
$columnForExpression .= "{$this->table}.";
71+
}
72+
$columnForExpression .= $this->column;
73+
return $columnForExpression;
74+
}
75+
}

src/Criteria/Between.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
4+
namespace Pechynho\DbWrap\Criteria;
5+
6+
/**
7+
* @author Jan Pech <[email protected]>
8+
*/
9+
class Between extends AbstractCriterion
10+
{
11+
/** @var int */
12+
private $lowerLimit;
13+
14+
/** @var int */
15+
private $upperLimit;
16+
17+
/**
18+
* Between constructor.
19+
* @param string $column
20+
* @param int $lowerLimit
21+
* @param int $upperLimit
22+
*/
23+
public function __construct($column, $lowerLimit, $upperLimit)
24+
{
25+
parent::__construct($column);
26+
$this->lowerLimit = $lowerLimit;
27+
$this->upperLimit = $upperLimit;
28+
}
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
public function buildExpression()
34+
{
35+
$expression = "({$this->getColumnForExpression()} BETWEEN :{$this->column}_lower_limit AND :{$this->column}_upper_limit)";
36+
return $expression;
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function getParameters()
43+
{
44+
return [
45+
$this->column . "_lower_limit" => $this->lowerLimit,
46+
$this->column . "_upper_limit" => $this->upperLimit
47+
];
48+
}
49+
50+
/**
51+
* @return int
52+
*/
53+
public function getLowerLimit()
54+
{
55+
return $this->lowerLimit;
56+
}
57+
58+
/**
59+
* @param int $lowerLimit
60+
* @return Between
61+
*/
62+
public function setLowerLimit($lowerLimit)
63+
{
64+
$this->lowerLimit = $lowerLimit;
65+
return $this;
66+
}
67+
68+
/**
69+
* @return int
70+
*/
71+
public function getUpperLimit()
72+
{
73+
return $this->upperLimit;
74+
}
75+
76+
/**
77+
* @param int $upperLimit
78+
* @return Between
79+
*/
80+
public function setUpperLimit($upperLimit)
81+
{
82+
$this->upperLimit = $upperLimit;
83+
return $this;
84+
}
85+
}

src/Criteria/Equals.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
4+
namespace Pechynho\DbWrap\Criteria;
5+
6+
/**
7+
* @author Jan Pech <[email protected]>
8+
*/
9+
class Equals extends OperatorCriterion
10+
{
11+
protected $operator = "=";
12+
}

src/Criteria/Expression.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
4+
namespace Pechynho\DbWrap\Criteria;
5+
6+
/**
7+
* @author Jan Pech <[email protected]>
8+
*/
9+
class Expression implements ICriterion
10+
{
11+
/** @var string */
12+
private $expression;
13+
14+
/** @var array */
15+
private $parameters;
16+
17+
/**
18+
* Expression constructor.
19+
* @param string $expression
20+
* @param array $parameters
21+
*/
22+
public function __construct($expression, $parameters)
23+
{
24+
$this->expression = $expression;
25+
$this->parameters = $parameters;
26+
}
27+
28+
/**
29+
* @return string
30+
*/
31+
public function getExpression()
32+
{
33+
return $this->expression;
34+
}
35+
36+
/**
37+
* @param string $expression
38+
* @return Expression
39+
*/
40+
public function setExpression($expression)
41+
{
42+
$this->expression = $expression;
43+
return $this;
44+
}
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
public function buildExpression()
50+
{
51+
return $this->expression;
52+
}
53+
54+
/**
55+
* @inheritDoc
56+
*/
57+
public function getParameters()
58+
{
59+
return $this->parameters;
60+
}
61+
62+
/**
63+
* @param array $parameters
64+
* @return Expression
65+
*/
66+
public function setParameters($parameters)
67+
{
68+
$this->parameters = $parameters;
69+
return $this;
70+
}
71+
}

src/Criteria/GreaterOrEqualsThan.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
4+
namespace Pechynho\DbWrap\Criteria;
5+
6+
/**
7+
* @author Jan Pech <[email protected]>
8+
*/
9+
class GreaterOrEqualsThan extends OperatorCriterion
10+
{
11+
protected $operator = ">=";
12+
}

src/Criteria/GreaterThan.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
4+
namespace Pechynho\DbWrap\Criteria;
5+
6+
/**
7+
* @author Jan Pech <[email protected]>
8+
*/
9+
class GreaterThan extends OperatorCriterion
10+
{
11+
protected $operator = ">";
12+
}

src/Criteria/ICriterion.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace Pechynho\DbWrap\Criteria;
5+
6+
/**
7+
* @author Jan Pech <[email protected]>
8+
*/
9+
interface ICriterion
10+
{
11+
/**
12+
* @return string
13+
*/
14+
public function buildExpression();
15+
16+
/**
17+
* @return array
18+
*/
19+
public function getParameters();
20+
}

src/Criteria/In.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
4+
namespace Pechynho\DbWrap\Criteria;
5+
6+
7+
use Pechynho\Utility\Strings;
8+
9+
/**
10+
* @author Jan Pech <[email protected]>
11+
*/
12+
class In extends AbstractCriterion
13+
{
14+
/** @var array */
15+
private $values;
16+
17+
/** @var array */
18+
private $parameters = [];
19+
20+
/**
21+
* In constructor.
22+
* @param string $column
23+
* @param array $values
24+
*/
25+
public function __construct($column, $values)
26+
{
27+
parent::__construct($column);
28+
$this->setValues($values);
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function buildExpression()
35+
{
36+
$expression = "{$this->getColumnForExpression()} IN (";
37+
foreach ($this->parameters as $name => $vale)
38+
{
39+
$expression .= ":$name, ";
40+
}
41+
$expression = Strings::remove($expression, Strings::length($expression) - 2) . ")";
42+
return $expression;
43+
}
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
public function getParameters()
49+
{
50+
return $this->parameters;
51+
}
52+
53+
/**
54+
* @return array
55+
*/
56+
public function getValues()
57+
{
58+
return $this->values;
59+
}
60+
61+
/**
62+
* @param array $values
63+
* @return In
64+
*/
65+
public function setValues($values)
66+
{
67+
$this->values = $values;
68+
$index = 1;
69+
foreach ($this->values as $value)
70+
{
71+
$parameterName = $this->column . "_" . $index;
72+
$this->parameters[$parameterName] = $value;
73+
$index++;
74+
}
75+
return $this;
76+
}
77+
}

0 commit comments

Comments
 (0)