-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_filtering.php
More file actions
123 lines (96 loc) · 3.79 KB
/
Copy path03_filtering.php
File metadata and controls
123 lines (96 loc) · 3.79 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* Filtering and searching records.
*
* The Filter builder provides a fluent API for building queries
* that work identically across all adapters.
*/
declare(strict_types=1);
require __DIR__.'/../vendor/autoload.php';
use KDuma\SimpleDAL\Adapter\Database\DatabaseAdapter;
use KDuma\SimpleDAL\Contracts\Query\SortDirection;
use KDuma\SimpleDAL\DataStore;
use KDuma\SimpleDAL\Entity\CollectionEntityDefinition;
use KDuma\SimpleDAL\Query\Filter;
$store = new DataStore(
adapter: new DatabaseAdapter(new PDO('sqlite::memory:')),
entities: [
new CollectionEntityDefinition(
name: 'certificates',
indexedFields: ['status', 'subject.commonName'],
),
],
);
$certs = $store->collection('certificates');
// ── Seed some data ──
$certs->create(['subject' => ['commonName' => 'example.com'], 'status' => 'active', 'not_after' => '2027-01-01', 'priority' => 1], id: 'cert-01');
$certs->create(['subject' => ['commonName' => 'test.com'], 'status' => 'active', 'not_after' => '2026-06-01', 'priority' => 3], id: 'cert-02');
$certs->create(['subject' => ['commonName' => 'staging.com'], 'status' => 'revoked', 'not_after' => '2025-12-01', 'priority' => 2], id: 'cert-03');
$certs->create(['subject' => ['commonName' => 'internal.local'], 'status' => 'active', 'not_after' => '2026-09-01', 'priority' => 4], id: 'cert-04');
$certs->create(['subject' => ['commonName' => 'api.example.com'], 'status' => 'expired', 'not_after' => '2025-01-01', 'priority' => 5], id: 'cert-05');
// ── Simple equality filter ──
echo "Active certificates:\n";
$active = $certs->filter(Filter::where('status', '=', 'active'));
foreach ($active as $r) {
echo " {$r->id}: {$r->get('subject.commonName')}\n";
}
// ── Nested field filter (dot notation) ──
echo "\nCertificates for example.com:\n";
$results = $certs->filter(Filter::where('subject.commonName', '=', 'example.com'));
foreach ($results as $r) {
echo " {$r->id}: {$r->get('subject.commonName')}\n";
}
// ── Comparison operators ──
echo "\nExpiring before 2026-07-01:\n";
$expiring = $certs->filter(
Filter::where('not_after', '<', '2026-07-01'),
);
foreach ($expiring as $r) {
echo " {$r->id}: expires {$r->get('not_after')}\n";
}
// ── String contains ──
echo "\nDomains containing 'example':\n";
$results = $certs->filter(
Filter::where('subject.commonName', 'contains', 'example'),
);
foreach ($results as $r) {
echo " {$r->id}: {$r->get('subject.commonName')}\n";
}
// ── Multiple conditions (AND) ──
echo "\nActive + expiring before 2026-07-01:\n";
$results = $certs->filter(
Filter::where('status', '=', 'active')
->andWhere('not_after', '<', '2026-07-01'),
);
foreach ($results as $r) {
echo " {$r->id}: {$r->get('subject.commonName')} (expires {$r->get('not_after')})\n";
}
// ── IN operator ──
echo "\nActive or expired:\n";
$results = $certs->filter(
Filter::where('status', 'in', ['active', 'expired']),
);
foreach ($results as $r) {
echo " {$r->id}: {$r->get('status')}\n";
}
// ── Sorting ──
echo "\nAll certs sorted by priority (desc):\n";
$results = $certs->filter(
(new Filter)->orderBy('priority', SortDirection::Desc),
);
foreach ($results as $r) {
echo " {$r->id}: priority={$r->get('priority')}\n";
}
// ── Limit + offset (pagination) ──
echo "\nPage 2 (2 per page, sorted by priority asc):\n";
$results = $certs->filter(
(new Filter)->orderBy('priority', SortDirection::Asc)->limit(2)->offset(2),
);
foreach ($results as $r) {
echo " {$r->id}: priority={$r->get('priority')}\n";
}
// ── Count with filter ──
$activeCount = $certs->count(Filter::where('status', '=', 'active'));
echo "\nActive count: {$activeCount}\n";
echo "Total count: {$certs->count()}\n";
echo "\nDone.\n";