refactor(policy): build the registries and six engines the architecture asked for

The task 09 architecture specified FieldRegistry, OperatorRegistry, six engine
classes and a stored specificity. What shipped was a single PolicySchema
constant list, six operators, one resolver and a specificity recomputed on
every booking. Each shortcut was defensible on its own; together they left the
starred risk the task itself recorded — a field can be advertised in the form
and supplied by nobody, and the rule silently never matches.

OperatorRegistry now holds all eleven operators. The five that were missing are
real capability, not ceremony: greater_or_equal and less_or_equal make boundary
rules expressible without off-by-one, not_in is the natural way to write an
exclusion, between stops "18 to 65" needing two clauses, and days_since is the
documented operator for "more than N days since" — until now every caller
computed that by hand. between is inclusive at both ends because that is what
the Persian phrasing means and what the user will type.

FieldRegistry is now the single source: it builds the form schema and extracts
the value, so a field that exists in one and not the other is impossible. It
also declares which categories each field belongs to, which is what the closed
list per category used to do separately. Adding it immediately caught its own
first case — last_visit_at was advertised and supplied nowhere, so the guard
now populates it and days_since has something to read.

The six engines are thin on purpose. They give the call site a type — "the
pricing engine" rather than "the resolver with the string pricing" — and a
place for evaluateIsolated, which the sandbox needs to answer "what would this
one rule do". Conflict resolution and effect combination stay in
PolicyResolver: six copies of that would be six places to break.

specificity is a stored column now, computed on save with the documented
weights, and the migration backfills existing rows with the same formula. Left
at zero they would all have tied and the ordering would have changed overnight.

Field names stay as they are rather than moving to the document's dotted names
(patient.age). Stored condition_json rows point at the current names on live
clinic policies; renaming them is a data migration, and the mapping is not
one-to-one — implementation_notes.md says as much.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-08-01 15:56:50 +03:30
co-authored by Claude Opus 5
parent 60b4fb9b93
commit f2600f9922
23 changed files with 800 additions and 118 deletions
+8 -5
View File
@@ -2,7 +2,9 @@
namespace App\Tests\Policy;
use App\Policy\Service\PolicySchema;
use App\Policy\Entity\Policy;
use App\Policy\Service\FieldRegistry;
use App\Policy\Service\OperatorRegistry;
use PHPUnit\Framework\TestCase;
/**
@@ -19,10 +21,11 @@ class PolicyFieldCoverageTest extends TestCase
{
public function testEveryAdvertisedFieldIsSuppliedSomewhereInTheCode(): void
{
$fields = [];
$registry = new FieldRegistry(new OperatorRegistry());
$fields = [];
foreach (PolicySchema::FIELDS as $category => $list) {
foreach ($list as $field) {
foreach (Policy::CATEGORIES as $category) {
foreach ($registry->forCategory($category) as $field) {
$fields[$field][] = $category;
}
}
@@ -35,7 +38,7 @@ class PolicyFieldCoverageTest extends TestCase
foreach ($sources as $file => $code) {
// خودِ schema فقط نام را اعلام می‌کند؛ پر کردنش جای دیگری است.
if (str_ends_with($file, 'PolicySchema.php')) {
if (str_ends_with($file, 'PolicySchema.php') || str_ends_with($file, 'FieldRegistry.php')) {
continue;
}