-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplain.php
More file actions
51 lines (42 loc) · 1.27 KB
/
Copy pathexplain.php
File metadata and controls
51 lines (42 loc) · 1.27 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
<?php
declare(strict_types=1);
use RuleFlow\Engine;
use RuleFlow\RuleSet;
require __DIR__ . '/../vendor/autoload.php';
ini_set('serialize_precision', '-1');
$rules = [
[
'name' => 'high_risk_order',
'priority' => 100,
'conditions' => [
['field' => 'order.amount', 'operator' => '>', 'value' => 1000],
['field' => 'user.risk_score', 'operator' => '<', 'value' => 60],
],
'action' => 'manual_review',
'reason' => 'High-risk order requires manual review.',
],
[
'name' => 'phone_present',
'priority' => 80,
'conditions' => [
['field' => 'user.phone', 'operator' => 'exists'],
],
'action' => 'allow',
'reason' => 'User phone number is present.',
],
];
$context = [
'user' => [
'id' => 1001,
'risk_score' => 72,
],
'order' => [
'id' => 'ORD-1001',
'amount' => 1299,
],
];
$result = Engine::make(RuleSet::fromArray($rules))->evaluateAll($context);
echo "Compact explain output:\n";
echo json_encode($result->explain(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
echo "\nFull trace output:\n";
echo json_encode($result->trace()->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;