1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
4
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
5
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace midgard\portable\storage; |
9
|
|
|
|
10
|
|
|
use midgard_query_builder; |
11
|
|
|
|
12
|
|
|
class collection |
13
|
|
|
{ |
14
|
|
|
private readonly string $classname; |
15
|
|
|
|
16
|
16 |
|
public function __construct(string $classname) |
17
|
|
|
{ |
18
|
16 |
|
$this->classname = $classname; |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
2 |
|
public function is_empty(string $guid) : bool |
22
|
|
|
{ |
23
|
2 |
|
$qb = $this->get_qb($guid); |
24
|
2 |
|
return $qb->count() == 0; |
25
|
|
|
} |
26
|
|
|
|
27
|
15 |
|
public function find(string $guid, array $constraints) : array |
28
|
|
|
{ |
29
|
15 |
|
$qb = $this->get_qb($guid); |
30
|
15 |
|
$this->apply_qb_constraints($qb, $constraints); |
31
|
15 |
|
return $qb->execute(); |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
public function delete(string $guid, array $constraints) : int |
35
|
|
|
{ |
36
|
1 |
|
$qb = $this->get_qb($guid); |
37
|
1 |
|
$this->apply_qb_constraints($qb, $constraints); |
38
|
1 |
|
$params = $qb->execute(); |
39
|
1 |
|
$deleted_count = 0; |
40
|
1 |
|
foreach ($params as $param) { |
41
|
1 |
|
if ($param->delete()) { |
42
|
1 |
|
$deleted_count++; |
43
|
|
|
} |
44
|
|
|
} |
45
|
1 |
|
return $deleted_count; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function purge(string $guid, array $constraints) : int |
49
|
|
|
{ |
50
|
1 |
|
$qb = $this->get_qb($guid); |
51
|
1 |
|
$this->apply_qb_constraints($qb, $constraints); |
52
|
1 |
|
$params = $qb->execute(); |
53
|
1 |
|
$purged_count = 0; |
54
|
1 |
|
foreach ($params as $param) { |
55
|
1 |
|
if ($param->purge()) { |
56
|
1 |
|
$purged_count++; |
57
|
|
|
} |
58
|
|
|
} |
59
|
1 |
|
return $purged_count; |
60
|
|
|
} |
61
|
|
|
|
62
|
16 |
|
private function get_qb(string $guid) : midgard_query_builder |
63
|
|
|
{ |
64
|
16 |
|
$qb = new midgard_query_builder(connection::get_fqcn($this->classname)); |
65
|
16 |
|
$qb->add_constraint('parentguid', '=', $guid); |
66
|
16 |
|
return $qb; |
67
|
|
|
} |
68
|
|
|
|
69
|
15 |
|
private function apply_qb_constraints(midgard_query_builder $qb, array $constraints) |
70
|
|
|
{ |
71
|
15 |
|
foreach ($constraints as $name => $value) { |
72
|
14 |
|
$qb->add_constraint($name, '=', $value); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|