GithubHelp home page GithubHelp logo

enum-helper's Introduction

enum-helper

This library provides helpers for several enum operations:

  • compare
  • to array

It also provides an abstract EnumTestCase.

CI

Installation

composer require oskarstark/enum-helper

Usage

For example, you have the following Enum:

<?php

declare(strict_types=1);

namespace App\Enum;

enum Color: string
{
    case RED = 'red';
    case BLUE = 'blue';
}

You can use the following trait,

<?php

declare(strict_types=1);

namespace App\Enum;

+use OskarStark\Enum\Trait\Comparable;
+use OskarStark\Enum\Trait\ToArray;

enum Color: string
{
+   use Comparable;
+   use ToArray;

    case RED = 'red';
    case BLUE = 'blue';
}

Comparable Trait

This trait gives you the possibility to compare you enum with another one or a collection of enums like the following:

App\Enum\Color::RED->equals(App\Enum\Color::BLUE); // returns false
App\Enum\Color::RED->notEquals(App\Enum\Color::RED); // returns false
    App\Enum\Color::RED->equalsOneOf([
        App\Enum\Color::BLUE,
        App\Enum\Color::RED,
    ]); // returns true

For example, you want to check if a color is a nice color:

<?php

declare(strict_types=1);

namespace App\Enum;

use OskarStark\Enum\Trait\Comparable;
use OskarStark\Enum\Trait\ToArray;

enum Color: string
{
    use Comparable;
    use ToArray;

    case RED = 'red';
    case BLUE = 'blue';
    case GREEN = 'green';
    
    public function isNice(): bool
    {
        return self::equalsOneOf([
            self::BLUE,
            self::GREEN
        ]);
    }
}
App\Enum\Color::RED->isNice(); // returns false
App\Enum\Color::BLUE->isNice(); // returns true

ToArray Trait

This trait gives you the possibility to get an enum as array like the following:

For Backed Enum

App\Enum\Color::toArray(); // returns ['RED' => 'red', 'BLUE' => 'blue']

For Non-Backed Enum

App\Enum\NonBackedEnum::toArray(); // returns ['RED' => 'RED', 'BLUE' => 'BLUE']

enum-helper's People

Contributors

dependabot[bot] avatar oskarstark avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.