Skip to content

Commit

Permalink
Renamed Multi into Subset and added a few methods to Subset
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Mar 2, 2022
1 parent e19ceac commit 18cae2d
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 88 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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

## 1.4.0 - 2022-03-02

- Renamed Multi to Subset
- Added `names` method to `Subset`
- Added `values` method to `Subset`
- Added `do` method to `Subset`

## 1.3.0 - 2022-02-28

- added Multi. Currently allows you to compare against a subset of your enum.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ implemented his own version of `Makers` and `Reporters`.
- [Labels](docs/labels.md)
- [Makers](docs/makers.md)
- [Mappers](docs/mappers.md)
- [Multi](docs/multi.md)
- [Subset](docs/subset.md)
- [Properties](docs/properties.md)
- [Reporters](docs/reporters.md)
- [Value](docs/value.md)
Expand Down
39 changes: 34 additions & 5 deletions docs/multi.md → docs/subset.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# Multi
# Subset

This allows you to do actions on a subset of the enums. Currently we only support `equals`,
but in the future other features may be added.
This allows you to do certain actions on a subset of the enums.
## usage

```php
use Henzeb\Enumhancer\Concerns\Multi;
use Henzeb\Enumhancer\Concerns\Subset;

enum yourEnum {

use Multi;
use Subset;

case MY_ENUM;
case MY_OTHER_ENUM;
Expand Down Expand Up @@ -55,4 +54,34 @@ YourEnum::of(
yourEnum::MY_OTHER_ENUM
)->equals(YourEnum::MY_THIRD_ENUM); // will return false
```
### names
This method returns an array of names of the specified subset.
```php
YourEnum::of(
yourEnum::MY_ENUM,
yourEnum::MY_OTHER_ENUM
)->names(); // will return ['MY_ENUM', 'MY_OTHER_ENUM']
```

### values
This method returns an array of values of the specified subset.
This uses the [Value](value.md) trait, when enum is a `UnitEnum`.

```php
YourEnum::of(
yourEnum::MY_ENUM,
yourEnum::MY_OTHER_ENUM
)->values(); // will return ['my_enum', 'my_other_enum']
```
### do
This method allows you call a closure on each item in the subset.

```php
YourEnum::of(
yourEnum::MY_ENUM,
yourEnum::MY_OTHER_ENUM
)->do(
function(yourEnum $enum) {
print $enum->name.',';
}); // will print MY_ENUM,MY_OTHER_ENUM
```
4 changes: 2 additions & 2 deletions src/Concerns/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Henzeb\Enumhancer\Concerns;

use BackedEnum;
use Henzeb\Enumhancer\Helpers\MultiEnumMethods;
use Henzeb\Enumhancer\Helpers\EnumSubsetMethods;

trait Comparison
{
Expand All @@ -12,7 +12,7 @@ trait Comparison
*/
final public function equals(self|string ...$equals): bool
{
return (new MultiEnumMethods(self::class, $this))
return (new EnumSubsetMethods(self::class, $this))
->equals(...$equals);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/Enhancers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

trait Enhancers
{
use Comparison, Labels, Mappers, Properties, Value, Multi;
use Comparison, Labels, Mappers, Properties, Value, Subset;
}
14 changes: 0 additions & 14 deletions src/Concerns/Multi.php

This file was deleted.

14 changes: 14 additions & 0 deletions src/Concerns/Subset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Henzeb\Enumhancer\Concerns;

use Henzeb\Enumhancer\Contracts\EnumSubset;
use Henzeb\Enumhancer\Helpers\EnumSubsetMethods;

trait Subset
{
final public static function of(self ...$enums): EnumSubset
{
return new EnumSubsetMethods(self::class, ...($enums ?:self::cases()));
}
}
18 changes: 18 additions & 0 deletions src/Contracts/EnumSubset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Henzeb\Enumhancer\Contracts;

use Closure;
use UnitEnum;
use BackedEnum;

interface EnumSubset
{
public function do(Closure $callable): void;

public function equals(BackedEnum|UnitEnum|string ...$enum): bool;

public function names(): array;

public function values(): array;
}
11 changes: 0 additions & 11 deletions src/Contracts/MultiEnum.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Henzeb\Enumhancer\Helpers;

use Closure;
use UnitEnum;
use BackedEnum;
use Henzeb\Enumhancer\Contracts\MultiEnum;
use Henzeb\Enumhancer\Contracts\EnumSubset;

class MultiEnumMethods implements MultiEnum
class EnumSubsetMethods implements EnumSubset
{
private array $enums;

Expand All @@ -17,13 +18,20 @@ public function __construct(private string $enumType, UnitEnum|BackedEnum ...$en
$this->enums = $enums;
}

public function do(Closure $callable): void
{
foreach($this->enums as $enum) {
$callable($enum);
}
}

public function equals(string|UnitEnum|BackedEnum ...$equals): bool
{
EnumCheck::matches($this->enumType, ...$equals);

foreach($this->enums as $enum) {
foreach ($this->enums as $enum) {

if($this->compare($enum, ...$equals)) {
if ($this->compare($enum, ...$equals)) {
return true;
}
}
Expand All @@ -33,7 +41,7 @@ public function equals(string|UnitEnum|BackedEnum ...$equals): bool

private function compare(UnitEnum|BackedEnum $enum, string|UnitEnum|BackedEnum ...$equals): bool
{
foreach($equals as $equal) {
foreach ($equals as $equal) {
if ($enum->name === $equal) {
return true;
}
Expand All @@ -42,7 +50,7 @@ private function compare(UnitEnum|BackedEnum $enum, string|UnitEnum|BackedEnum .
return true;
}

if(method_exists($enum,'value') && $enum->value() === $equal) {
if (method_exists($enum, 'value') && $enum->value() === $equal) {
return true;
}

Expand All @@ -52,4 +60,16 @@ private function compare(UnitEnum|BackedEnum $enum, string|UnitEnum|BackedEnum .
}
return false;
}

public function names(): array
{
return array_map(fn(UnitEnum $enum) => $enum->name, $this->enums);
}

public function values(): array
{
return array_map(
fn(mixed $enum) => $enum->value ?? $enum->value(), $this->enums
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Henzeb\Enumhancer\Tests\Fixtures\EnhancedBackedEnum;


class MultiTest extends TestCase
class SubsetTest extends TestCase
{
public function testOfSubsetShouldEquals(): void
{
Expand Down
102 changes: 102 additions & 0 deletions tests/Unit/Helpers/EnumSubsetMethodsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Unit\Helpers;

use PHPUnit\Framework\TestCase;
use Henzeb\Enumhancer\Helpers\EnumSubsetMethods;
use Henzeb\Enumhancer\Tests\Fixtures\EnhancedUnitEnum;
use Henzeb\Enumhancer\Tests\Fixtures\IntBackedMakersEnum;
use Henzeb\Enumhancer\Tests\Fixtures\StringBackedMakersEnum;

class EnumSubsetMethodsTest extends TestCase
{

public function testShouldThrowErrorWithWrongEnumType(): void
{
$this->expectError();
(new EnumSubsetMethods(IntBackedMakersEnum::class, EnhancedUnitEnum::ENUM));
}


public function testEqualsShouldReturnNullWhenNoEnumsPassed()
{
$this->assertFalse(
(new EnumSubsetMethods(IntBackedMakersEnum::class))
->equals(IntBackedMakersEnum::TEST)
);
}

public function testEqualsShouldReturnTrue()
{
$this->assertTrue(
(new EnumSubsetMethods(IntBackedMakersEnum::class, IntBackedMakersEnum::TEST))
->equals(IntBackedMakersEnum::TEST)
);
}

public function testEqualsMultiShouldReturnTrue()
{
$this->assertTrue(
(new EnumSubsetMethods(IntBackedMakersEnum::class, ...IntBackedMakersEnum::cases()))
->equals(IntBackedMakersEnum::TEST)
);
}

public function testNamesShouldReturnArrayOfNames()
{
$this->assertEquals(
$this->getNames(IntBackedMakersEnum::cases()),
(new EnumSubsetMethods(IntBackedMakersEnum::class, ...IntBackedMakersEnum::cases()))
->names()
);
}

public function testValueShouldReturnArrayOfValuesStringBacked()
{
$this->assertEquals(
$this->getValues(StringBackedMakersEnum::cases()),
(new EnumSubsetMethods(StringBackedMakersEnum::class, ...StringBackedMakersEnum::cases()))
->values()
);
}

public function testValueShouldReturnArrayOfValuesIntBacked()
{
$this->assertEquals(
$this->getValues(IntBackedMakersEnum::cases()),
(new EnumSubsetMethods(IntBackedMakersEnum::class, ...IntBackedMakersEnum::cases()))
->values()
);
}

public function testValueShouldReturnArrayOfValuesUnitEnums()
{
$this->assertEquals(
$this->getValues(EnhancedUnitEnum::cases()),
(new EnumSubsetMethods(EnhancedUnitEnum::class, ...EnhancedUnitEnum::cases()))
->values()
);
}

public function testShouldRunClosureOnArrayOfEnums()
{
$enums = [];
(new EnumSubsetMethods(EnhancedUnitEnum::class, ...EnhancedUnitEnum::cases()))
->do(
function (EnhancedUnitEnum $enum) use (&$enums) {
$enums[] = $enum;
}
);
$this->assertEquals(EnhancedUnitEnum::cases(), $enums);
}

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

private function getValues(array $cases): array
{
return array_map(fn($enum) => $enum->value ?? $enum->value(), $cases);
}
}
47 changes: 0 additions & 47 deletions tests/Unit/Helpers/MultiEnumMethodsTest.php

This file was deleted.

0 comments on commit 18cae2d

Please sign in to comment.