From 2525fe0f94f179cbd2f6bc06b38b341ac995b3f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandru=20P=C4=83tr=C4=83nescu?= Date: Mon, 15 Feb 2021 10:17:17 +0200 Subject: [PATCH 1/3] add final to example classes and add note about from() static method --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bf1c91d..1456bd9 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ use MyCLabs\Enum\Enum; /** * Action enum */ -class Action extends Enum +final class Action extends Enum { private const VIEW = 'view'; private const EDIT = 'edit'; @@ -50,7 +50,7 @@ $action = Action::VIEW(); // or with a dynamic key: $action = Action::$key(); // or with a dynamic value: -$action = new Action($value); +$action = Action::from($value); ``` As you can see, static methods are automatically implemented to provide quick access to an enum value. @@ -73,6 +73,7 @@ function setAction(Action $action) { Static methods: +- `from()` Creates an Enum instance, checking that the value exist in the enum - `toArray()` method Returns all possible values as an array (constant name in key, constant value in value) - `keys()` Returns the names (keys) of all constants in the Enum class - `values()` Returns instances of the Enum class of all Enum constants (constant name in key, Enum instance in value) @@ -83,7 +84,7 @@ Static methods: ### Static methods ```php -class Action extends Enum +final class Action extends Enum { private const VIEW = 'view'; private const EDIT = 'edit'; @@ -99,7 +100,7 @@ Static method helpers are implemented using [`__callStatic()`](http://www.php.ne If you care about IDE autocompletion, you can either implement the static methods yourself: ```php -class Action extends Enum +final class Action extends Enum { private const VIEW = 'view'; @@ -119,7 +120,7 @@ or you can use phpdoc (this is supported in PhpStorm for example): * @method static Action VIEW() * @method static Action EDIT() */ -class Action extends Enum +final class Action extends Enum { private const VIEW = 'view'; private const EDIT = 'edit'; From e620c08f6a6e3c8407dd1d6dbee680ca11b1a5b1 Mon Sep 17 00:00:00 2001 From: Alexandru Patranescu Date: Mon, 15 Feb 2021 10:20:07 +0200 Subject: [PATCH 2/3] re-add the constructor example --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 1456bd9..dfcd491 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,8 @@ $action = Action::VIEW(); $action = Action::$key(); // or with a dynamic value: $action = Action::from($value); +// or +$action = new Action($value); ``` As you can see, static methods are automatically implemented to provide quick access to an enum value. From b50f4add068433c033f6e6be40e68a57dbda5cfe Mon Sep 17 00:00:00 2001 From: Alexandru Patranescu Date: Mon, 15 Feb 2021 12:58:34 +0200 Subject: [PATCH 3/3] add also mention in documentation for assertValidValue --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index dfcd491..1e4d1ff 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ Static methods: - `values()` Returns instances of the Enum class of all Enum constants (constant name in key, Enum instance in value) - `isValid()` Check if tested value is valid on enum set - `isValidKey()` Check if tested key is valid on enum set +- `assertValidValue()` Assert the value is valid on enum set, throwing exception otherwise - `search()` Return key for searched value ### Static methods