Commit 3ef6dc37 authored by Jan Kuchař's avatar Jan Kuchař

readme: updated to match last changes

parent 33146da5
Pipeline #14372 passed with stages
in 1 minute
# grifart/enum
repositories: [GRIFART GitLab](https://gitlab.grifart.cz/jkuchar1/grifart-enum), [GitHub](https://github.com/grifart/enum)
Enumeration value object. Enumerate values and behaviour with type-safety.
Repositories [gitlab.grifart.cz](https://gitlab.grifart.cz/jkuchar1/grifart-enum) and [github.com](https://github.com/grifart/enum).
Sponsored by [grifart.com](https://grifart.com).
## Introduction
Enums represent predefined set of values. The available values are defined statically by each enum class. Each value is represented by an instance of this class in a flyweight manner.
- This enum allows you to add individal behaviour to every enum value (as in Java). This allows you to transform your `switch`es/`if`s into more readable composition. (see example bellow)
- Checks enum annotations if phpdoc-declared methods are properly declared (will generate docblock for you in exception)
- This enum allows you to add individual behaviour for every enum value (as in Java). This allows you to transform your `switch`es/`if`s into more readable composition. (see example bellow)
- Checks enum annotations if phpdoc-declared methods are properly declared (will generate docblock for you when not specified or incorrect)
- `===`, `==` and usage of `switch`es is supported
- string or integer scalar keys are supported
- Easily access scalar value of enum `DayOfWeek::MONDAY()->toScalar()` or `(string) DayOfWeek::MONDAY()`
Common with other enum implementations:
Also includes:
- You can type-hint: `function setCurrentDay(DayOfWeek $day) {`
- You can get a list of all the possible values
- It is type safe. By annotating your enumeration type, you are guaranteed that there will be no other values then you declared. `function translateTo(DayOfWeek $day) { ...`
- You can get a list of all the possible values `Enum::getAvailableValues()`
## Installation
......@@ -21,6 +30,22 @@ composer require grifart/enum
This library uses [**semantic versioning 2.0**](https://semver.org/spec/v2.0.0.html).
You can safely use `^` constrain in you `composer.json`.
## Requirements
This library requires PHP 7.1 and later.
## Project status & release process
While this library is still under development, it is well tested and should be stable enough to use in production environments.
The current releases are numbered 0.x.y. When a non-breaking change is introduced (adding new methods, optimizing existing code, etc.), y is incremented.
When a breaking change is introduced, a new 0.x version cycle is always started.
It is therefore safe to lock your project to a given release cycle, such as 0.1.*.
If you need to upgrade to a newer release cycle, check the release history for a list of changes introduced by each further 0.x.0 version.
## Let code speak: individual behaviour for each value
Let's **refactor** following existing code:
......@@ -44,12 +69,14 @@ $monday = DayOfWeek::MONDAY();
function nextDay(DayOfWeek $dayOfWeek): DayOfWeek
{
if($dayOfWeek === DayOfWeek::MONDAY()) {
return DayOfWeek::TUESDAY();
} else if (...) {
...
}
switch($dayOfWeek) {
case DayOfWeek::MONDAY():
return DayOfWeek::TUESDAY();
case DayOfWeek::TUESDAY():
// ...
}
throw new ShouldNotHappenException();
}
......@@ -73,12 +100,15 @@ final class DayOfWeek extends \Grifart\Enum\Enum
public function nextDay(): self
{
if($this === self::MONDAY()) {
return self::TUESDAY();
} else if (...) {
...
switch($this) {
case self::MONDAY():
return self::TUESDAY();
case self::TUESDAY():
// ...
}
throw new ShouldNotHappenException();
}
......@@ -115,7 +145,7 @@ abstract class DayOfWeek extends \Grifart\Enum\Enum
protected const TUESDAY = 'tuesday';
// ...
public abstract function nextDay(): self;
abstract public function nextDay(): self;
/** @return static[] */
......@@ -134,7 +164,7 @@ abstract class DayOfWeek extends \Grifart\Enum\Enum
{
public function nextDay(): DayOfWeek
{
return return DayOfWeek::WEDNESDAY();
return DayOfWeek::WEDNESDAY();
}
},
];
......@@ -151,6 +181,10 @@ More use cases:
- day of week
- tracking life-cycle
## Migrating legacy code
This guide show how to migrate from classes with constants to `\Grifart\Enum` with ease. [Continue to example](tests/Example/MigratingLegacyCode/readme.md)
## Big thanks
- [David Grudl](https://github.com/dg) for making [Nette Tester](https://github.com/nette/tester)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment