Giter VIP home page Giter VIP logo

enum-php's People

Contributors

ababkov avatar cupoftea696 avatar jodiedunlop avatar lachlankrautz avatar potsed avatar shoman4eg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

enum-php's Issues

Add support for rust like pattern matching

Intro

Pattern matching can greatly simplify logical branching. Take this example from rust:

// Value is set to the return from the first matching branch.
value = match number {
      // Match a single value
      1 => println!("One!"),
      // Match several values
      2 | 3 | 5 | 7 | 11 => println!("This is a prime"),
      // Match an inclusive range
      13...19 => println!("A teen"),
      // Handle the rest of cases
      _ => println!("Ain't special"),
}

Proposal

Add functions to Enum to support pattern matching behaviour.

Enum::match

/**
 * Match enum keys and return callback evaluation or default
 *
 * @param array $patterns
 * @param callable|mixed|null $default
 * @return mixed
 */
public function match(array $patterns, $default = null)
{
    foreach (array_keys($patterns) as $key) {
        // check each key is valid for enum
        // throw if invalid
    }
    $match = array_key_exists($this->key(), $patterns)
        ? $patterns[$this->key()]
        : $default;

    return is_callable($match)
        ? $match($this)
        : $match;
}

This allows us to write clean switch case replacements:

// Get the evaluated match price or default to 0.00
$cost = $device->match([
    // Use values from the enum
    Devices::PHONE => function (Enum $phone) {
        return 100.00 + $phone->value();
    },
    // Regular closure
    Devices::TABLET => function () use ($tabletTax) {
        return 10000.00 + $tabletTax;
    },
    // Static value
    Devices::WATCH => 300.00
], 0.00);

Enum::matchStrict

Same as match but doesn't have 'default' and throws exception if $patterns array isn't exhaustive ie doesn't cover every enum key.

Enum::matchBitmap / Enum::matchBitmapStrict

Same as above but support bitwise matching eg $enum->key() & $patternKey. This would allow pretty fancy pattern usage:

// Get the evaluated match price or default to 0.00
$cost = $device->matchBitmap([
    // Use values from the enum
    Devices::PHONE | Devices::SMART_PHONE | Devices::BRICK => function (Enum $phone) {
        return 100.00 + $phone->value();
    },
    // Regular closure
    Devices::TABLET | Devices::LARGE_TABLET => function () use ($tabletTax) {
        return 10000.00 + $tabletTax;
    },
    // Static value
    Devices::WATCH => 300.00
]);

Enum::key(); casts to a string breaking === comparisons to int keys

Enum assumes keys are stored as strings and breaks comparisons for int keys.

class Schema extends Enum
{
    public const VERSION_01 = 1;
    public const VERSION_02 = 2;
}

$version1 = Schema::VERSION_01();

$version1->is(Schema::VERSION_01);

// throws "Enum or string expected but integer given."

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.