rexlabsio / enum-php Goto Github PK
View Code? Open in Web Editor NEWEnumeration implementation for PHP
License: MIT License
Enumeration implementation for PHP
License: MIT License
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"),
}
Add functions to Enum
to support pattern matching behaviour.
/**
* 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);
Same as match but doesn't have 'default' and throws exception if $patterns
array isn't exhaustive ie doesn't cover every enum key.
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 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."
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.