Skip to content

Commit

Permalink
added cases method to Subset
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Mar 4, 2022
1 parent 18cae2d commit ef0efe1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `Enumhancer` will be documented in this file

## 1.4.1 - 2022-03-04

- added `cases` method to `Subset`

## 1.4.0 - 2022-03-02

- Renamed Multi to Subset
Expand Down
11 changes: 11 additions & 0 deletions docs/subset.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ YourEnum::of(
yourEnum::MY_OTHER_ENUM
)->values(); // will return ['my_enum', 'my_other_enum']
```

### cases
This method returns the all the cases of the specified subset.

```php
YourEnum::of(
yourEnum::MY_ENUM,
yourEnum::MY_OTHER_ENUM
)->cases(); // will return [yourEnum::MY_ENUM, yourEnum::MY_OTHER_ENUM]
```

### do
This method allows you call a closure on each item in the subset.

Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/EnumSubset.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public function equals(BackedEnum|UnitEnum|string ...$enum): bool;
public function names(): array;

public function values(): array;

public function cases(): array;
}
5 changes: 5 additions & 0 deletions src/Helpers/EnumSubsetMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@ public function values(): array
fn(mixed $enum) => $enum->value ?? $enum->value(), $this->enums
);
}

public function cases(): array
{
return $this->enums;
}
}
1 change: 1 addition & 0 deletions tests/Fixtures/EnhancedUnitEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ enum EnhancedUnitEnum

case ENUM;
case ANOTHER_ENUM;
case THIRD_ENUM;
}
20 changes: 20 additions & 0 deletions tests/Unit/Helpers/EnumSubsetMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ function (EnhancedUnitEnum $enum) use (&$enums) {
$this->assertEquals(EnhancedUnitEnum::cases(), $enums);
}

public function providesTestCasesForReturningSubsetOfCases(): array
{
return [
[[EnhancedUnitEnum::ENUM]],
[[EnhancedUnitEnum::ENUM, EnhancedUnitEnum::THIRD_ENUM]]
];
}

/**
* @return void
* @dataProvider providesTestCasesForReturningSubsetOfCases
*/
public function testCasesShouldReturnSubsetOfCases(array $cases)
{
$this->assertEquals(
$cases,
(new EnumSubsetMethods(EnhancedUnitEnum::class, ...$cases))->cases()
);
}

private function getNames(array $cases): array
{
return array_map(fn($enum) => $enum->name, $cases);
Expand Down

0 comments on commit ef0efe1

Please sign in to comment.