From f944409be1b3a35aa9282b0f82cfbb87068653eb Mon Sep 17 00:00:00 2001 From: Kirk Bushell Date: Wed, 20 May 2026 13:17:26 +1000 Subject: [PATCH] Added the ability to register specific fields for each resource. --- .../Translator/ResourceCriteria.php | 33 ++++++++++++++----- tests/Translator/ResourceCriteriaTest.php | 13 ++++++-- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/Tectonic/Localisation/Translator/ResourceCriteria.php b/src/Tectonic/Localisation/Translator/ResourceCriteria.php index ab7b821..6fee715 100755 --- a/src/Tectonic/Localisation/Translator/ResourceCriteria.php +++ b/src/Tectonic/Localisation/Translator/ResourceCriteria.php @@ -13,7 +13,7 @@ class ResourceCriteria { /** - * Stores the resources and their affiliated IDs for searching. + * Stores the resources and their associated fields and IDs for searching. */ private array $resources = []; @@ -23,10 +23,22 @@ class ResourceCriteria public function addResource(string $resource): void { if (!isset($this->resources[$resource])) { - $this->resources[$resource] = []; + $this->resources[$resource] = ['fields' => [], 'ids' => []]; } } + public function addField(string $resource, string $field): void + { + if (!isset($this->resources[$resource])) { + $message = "Resource [$resource] does not exist. Make sure you've + registered it first via ResourceCriteria::addResource('$resource').'"; + + throw new \Exception($message); + } + + $this->resources[$resource]['fields'][] = $field; + } + /** * Returns true if the resource criteria is valid. */ @@ -57,7 +69,7 @@ public function addId(string $resource, ?int $id): void throw new \Exception($message); } - $this->resources[$resource][] = $id; + $this->resources[$resource]['ids'][] = $id; } /** @@ -68,12 +80,17 @@ public function getResources(): array return array_keys($this->resources); } + public function getFields(string $resource): array + { + return $this->resources[$resource]['fields']; + } + /** * Return all ids for a given resource. */ public function getIds(string $resource): array { - return $this->resources[$resource]; + return $this->resources[$resource]['ids']; } /** @@ -81,14 +98,14 @@ public function getIds(string $resource): array */ public function forgetId(string $resource, int $id): void { - $key = array_search($id, $this->resources[$resource]); - unset($this->resources[$resource][$key]); + $key = array_search($id, $this->resources[$resource]['ids']); + unset($this->resources[$resource]['ids'][$key]); } public function forgetIds(string $resource, int ...$ids): void { - $this->resources[$resource] = array_filter( - $this->resources[$resource], + $this->resources[$resource]['ids'] = array_filter( + $this->resources[$resource]['ids'], fn($id) => !in_array($id, $ids) ); } diff --git a/tests/Translator/ResourceCriteriaTest.php b/tests/Translator/ResourceCriteriaTest.php index d0d43d3..9591952 100755 --- a/tests/Translator/ResourceCriteriaTest.php +++ b/tests/Translator/ResourceCriteriaTest.php @@ -73,8 +73,8 @@ public function testAll() $all = $this->resourceCriteria->all(); $this->assertCount(2, $all); $this->assertEquals(['resource', 'another'], array_keys($all)); - $this->assertEquals([1, 3], $all['resource']); - $this->assertEquals([1], $all['another']); + $this->assertEquals([1, 3], $all['resource']['ids']); + $this->assertEquals([1], $all['another']['ids']); } public function testForgetIds() @@ -88,4 +88,13 @@ public function testForgetIds() $this->assertCount(1, $ids = $this->resourceCriteria->getIds('resource')); $this->assertEquals([3], array_values($ids)); } + + public function testResourceFieldsCanBeAdded() + { + $this->resourceCriteria->addResource('resource'); + $this->resourceCriteria->addField('resource', 'field1'); + $this->resourceCriteria->addField('resource', 'field2'); + print_r($this->resourceCriteria->all()); + $this->assertEquals(['resource' => ['fields' => ['field1', 'field2'], 'ids' => []]], $this->resourceCriteria->all()); + } }