Skip to content
Merged
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
14 changes: 10 additions & 4 deletions src/Blacklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
'Service' => [],
];

/** @var array<int, array{from: AttributeName, to: AttributeName[]}> */
private array $rules;

/**
* Create a new Blacklist instance.
* Format: ['FROM_ATTRIBUTE' => ['TO_ATTRIBUTE1', 'TO_ATTRIBUTE2'], ...]
Expand All @@ -37,6 +40,7 @@
public function __construct(public array $data)
{
assert(!empty($data));
$this->rules = $this->buildRules($data);
}

public function isForbidden(Relation $relation): bool
Expand All @@ -54,7 +58,7 @@ public function isForbidden(Relation $relation): bool
*/
private function check(AttributeName $from, AttributeName $to): bool
{
foreach ($this->getRules() as $rule) {
foreach ($this->rules as $rule) {
if ($rule['from'] === $from) {
return in_array($to, $rule['to'], true);
}
Expand All @@ -64,14 +68,16 @@ private function check(AttributeName $from, AttributeName $to): bool
}

/**
* Get rules from configuration, converting string names to AttributeName enums.
* Build rules from configuration, converting string names to AttributeName enums.
*
* @param array<string, array<string>> $data
*
* @return array<int, array{from: AttributeName, to: AttributeName[]}>
*/
private function getRules(): array
private function buildRules(array $data): array
{
$rules = [];
foreach ($this->data as $fromValue => $toValues) {
foreach ($data as $fromValue => $toValues) {
try {
$from = AttributeName::from($fromValue);
$to = array_map(
Expand Down