|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the BenGorFile package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
|
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace BenGorFile\File\Infrastructure\Persistence\Sql; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Beñat Espiña <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class SqlListOfIdsSpecification implements SqlQuerySpecification, SqlCountSpecification |
|
19
|
|
|
{ |
|
20
|
|
|
private $ids; |
|
21
|
|
|
private $offset; |
|
22
|
|
|
private $limit; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(array $ids, $offset, $limit) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->ids = $ids; |
|
27
|
|
|
$this->offset = $offset; |
|
28
|
|
|
$this->limit = $limit; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function buildQuery() |
|
32
|
|
|
{ |
|
33
|
|
|
$whereClause = $this->buildWhere(); |
|
34
|
|
|
$limitClause = $this->buildLimit(); |
|
35
|
|
|
$query = "SELECT * FROM file f $whereClause ORDER BY f.updated_on DESC $limitClause"; |
|
36
|
|
|
$parameters = $this->buildParameters(); |
|
37
|
|
|
|
|
38
|
|
|
return [$query, $parameters]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function buildCount() |
|
42
|
|
|
{ |
|
43
|
|
|
$whereClause = $this->buildWhere(); |
|
44
|
|
|
$limitClause = $this->buildLimit(); |
|
45
|
|
|
$query = "SELECT COUNT(*) FROM file f $whereClause ORDER BY f.updated_on DESC $limitClause"; |
|
46
|
|
|
|
|
47
|
|
|
return [$query, $this->buildParameters()]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private function buildWhere() |
|
51
|
|
|
{ |
|
52
|
|
|
$inString = $this->generateInString($this->ids); |
|
53
|
|
|
$whereClause = !empty($this->ids) ? 'WHERE f.id IN (' . $inString . ') ' : ''; |
|
54
|
|
|
|
|
55
|
|
|
return $whereClause; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function buildLimit() |
|
59
|
|
|
{ |
|
60
|
|
|
$limitClause = $this->limit > 0 ? 'LIMIT :limit OFFSET :offset' : ''; |
|
61
|
|
|
|
|
62
|
|
|
return $limitClause; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function buildParameters() |
|
66
|
|
|
{ |
|
67
|
|
|
$parameters = []; |
|
68
|
|
|
if (!empty($this->ids)) { |
|
69
|
|
|
$parameters = array_merge($this->generateInParameters($this->ids), $parameters); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($this->limit > 0) { |
|
73
|
|
|
$parameters = array_merge([':limit' => $this->limit, ':offset' => $this->offset], $parameters); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $parameters; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function generateInString(array $ids) |
|
80
|
|
|
{ |
|
81
|
|
|
$in = ''; |
|
82
|
|
|
foreach ($ids as $i => $item) { |
|
83
|
|
|
$in .= ":id$i,"; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return rtrim($in, ','); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function generateInParameters(array $ids) |
|
90
|
|
|
{ |
|
91
|
|
|
$productVariantIdParams = []; |
|
92
|
|
|
foreach ($ids as $i => $item) { |
|
93
|
|
|
$productVariantIdParams[":id$i"] = $item; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $productVariantIdParams; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|