Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/Tectonic/Localisation/Translator/ResourceCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -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.
*/
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -68,27 +80,32 @@ 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'];
}

/**
* Forget a given id for a given resource.
*/
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)
);
}
Expand Down
13 changes: 11 additions & 2 deletions tests/Translator/ResourceCriteriaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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());
}
}