Skip to content

Files

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
87 lines (71 loc) · 1.91 KB

subset.md

File metadata and controls

87 lines (71 loc) · 1.91 KB

Subset

This allows you to do certain actions on a subset of the enums.

usage

use Henzeb\Enumhancer\Concerns\Subset;

enum yourEnum {

    use Subset;
    
    case MY_ENUM;
    case MY_OTHER_ENUM;
    case MY_THIRD_ENUM;   
}

Examples

equals

The equals method can come in handy when you need to compare one or more enums against a subset of your enums.

Equals works just like the method in the comparison trait.

YourEnum::of(
    yourEnum::MY_ENUM, 
    yourEnum::MY_OTHER_ENUM
)->equals(YourEnum::MY_ENUM); // will return true

YourEnum::of(
    yourEnum::MY_ENUM, 
    yourEnum::MY_OTHER_ENUM
)->equals('MY_ENUM'); // will return true

YourEnum::of(
    yourEnum::MY_ENUM, 
    yourEnum::MY_OTHER_ENUM
)->equals('my_enum'); // will return true
    
YourEnum::of(
    yourEnum::MY_ENUM, 
    yourEnum::MY_OTHER_ENUM
)->equals(YourEnum::MY_ENUM, yourEnum::MY_THIRD_ENUM); // will return true

YourEnum::of(
    yourEnum::MY_ENUM, 
    yourEnum::MY_OTHER_ENUM
)->equals('MY_ENUM', 'my_other_enum'); // will return true

YourEnum::of(
    yourEnum::MY_ENUM, 
    yourEnum::MY_OTHER_ENUM
)->equals(YourEnum::MY_THIRD_ENUM); // will return false

names

This method returns an array of names of the specified subset.

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 trait, when enum is a UnitEnum.

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.

YourEnum::of(
    yourEnum::MY_ENUM, 
    yourEnum::MY_OTHER_ENUM
)->do(
    function(yourEnum $enum) { 
        print $enum->name.',';
    }); // will print MY_ENUM,MY_OTHER_ENUM