Skip to content

Commit

Permalink
Rename method to resolveItem()
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Oct 5, 2024
1 parent 8ab79e7 commit 8d24cc4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
14 changes: 7 additions & 7 deletions src/CasesCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ public function pluck(callable|string $value, callable|string $key = null): arra

foreach ($this->cases as $case) {
if ($key === null) {
$result[] = $case->resolveCaseItem($value);
$result[] = $case->resolveItem($value);
} else {
$result[$case->resolveCaseItem($key)] = $case->resolveCaseItem($value);
$result[$case->resolveItem($key)] = $case->resolveItem($value);
}
}

Expand Down Expand Up @@ -173,7 +173,7 @@ public function keyBy(callable|string $key): static
$keyed = [];

foreach ($this->cases as $case) {
$keyed[$case->resolveCaseItem($key)] = $case;
$keyed[$case->resolveItem($key)] = $case;
}

return new static($keyed);
Expand All @@ -197,7 +197,7 @@ public function groupBy(callable|string $key): static
$grouped = [];

foreach ($this->cases as $case) {
$grouped[$case->resolveCaseItem($key)][] = $case;
$grouped[$case->resolveItem($key)][] = $case;
}

foreach ($grouped as $key => $cases) {
Expand All @@ -215,7 +215,7 @@ public function groupBy(callable|string $key): static
public function filter(callable|string $filter): static
{
/** @phpstan-ignore method.nonObject */
$callback = is_callable($filter) ? $filter : fn(mixed $case) => $case->resolveCaseItem($filter) === true;
$callback = is_callable($filter) ? $filter : fn(mixed $case) => $case->resolveItem($filter) === true;

return new static(array_filter($this->cases, $callback));
}
Expand Down Expand Up @@ -269,7 +269,7 @@ public function sortBy(callable|string $key): static
{
$cases = $this->cases;

uasort($cases, fn(mixed $a, mixed $b) => $a->resolveCaseItem($key) <=> $b->resolveCaseItem($key));
uasort($cases, fn(mixed $a, mixed $b) => $a->resolveItem($key) <=> $b->resolveItem($key));

return new static($cases);
}
Expand Down Expand Up @@ -299,7 +299,7 @@ public function sortByDesc(callable|string $key): static
{
$cases = $this->cases;

uasort($cases, fn(mixed $a, mixed $b) => $b->resolveCaseItem($key) <=> $a->resolveCaseItem($key));
uasort($cases, fn(mixed $a, mixed $b) => $b->resolveItem($key) <=> $a->resolveItem($key));

return new static($cases);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/SelfAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function metaNames(): array
* @return TItemValue
* @throws ValueError
*/
public function resolveCaseItem(callable|string $item): mixed
public function resolveItem(callable|string $item): mixed
{
return match (true) {
is_callable($item) => $item($this),
Expand Down
6 changes: 3 additions & 3 deletions tests/BackedEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@
});

it('retrieves a case item')
->expect(fn(string $item, mixed $value) => BackedEnum::one->resolveCaseItem($item) === $value)
->expect(fn(string $item, mixed $value) => BackedEnum::one->resolveItem($item) === $value)
->toBeTrue()
->with([
['name', 'one'],
Expand All @@ -434,10 +434,10 @@
]);

it('retrieves the item of a case using a closure')
->expect(BackedEnum::one->resolveCaseItem(fn(BackedEnum $case) => $case->color()))
->expect(BackedEnum::one->resolveItem(fn(BackedEnum $case) => $case->color()))
->toBe('red');

it('throws a value error when attempting to retrieve an invalid item', fn() => BackedEnum::one->resolveCaseItem('invalid'))
it('throws a value error when attempting to retrieve an invalid item', fn() => BackedEnum::one->resolveItem('invalid'))
->throws(ValueError::class, '"invalid" is not a valid meta for enum "Cerbero\Enum\BackedEnum"');

it('retrieves the value of a backed case or the name of a pure case', function() {
Expand Down
6 changes: 3 additions & 3 deletions tests/PureEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@
});

it('retrieves the item of a case')
->expect(fn(string $item, mixed $value) => PureEnum::one->resolveCaseItem($item) === $value)
->expect(fn(string $item, mixed $value) => PureEnum::one->resolveItem($item) === $value)
->toBeTrue()
->with([
['name', 'one'],
Expand All @@ -450,10 +450,10 @@
]);

it('retrieves the item of a case using a closure')
->expect(PureEnum::one->resolveCaseItem(fn(PureEnum $case) => $case->color()))
->expect(PureEnum::one->resolveItem(fn(PureEnum $case) => $case->color()))
->toBe('red');

it('throws a value error when attempting to retrieve an invalid item', fn() => PureEnum::one->resolveCaseItem('invalid'))
it('throws a value error when attempting to retrieve an invalid item', fn() => PureEnum::one->resolveItem('invalid'))
->throws(ValueError::class, '"invalid" is not a valid meta for enum "Cerbero\Enum\PureEnum"');

it('retrieves the value of a backed case or the name of a pure case', function() {
Expand Down

0 comments on commit 8d24cc4

Please sign in to comment.