Download the PHP package emreyarligan/enum-concern without Composer
On this page you can find all versions of the php package emreyarligan/enum-concern. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download emreyarligan/enum-concern
More information about emreyarligan/enum-concern
Files in emreyarligan/enum-concern
Package enum-concern
Short Description A PHP package for effortless Enumeration handling with Laravel Collections ๐ฆ โจ
License MIT
Homepage https://github.com/emreyarligan/enum-concern
Informations about the package enum-concern
EnumConcern - A PHP Package for Effortless Enumeration Handling ๐ฆ โจ
EnumConcern is a PHP package designed to enhance the usage of PHP's Enum feature with a set of convenient methods. This package includes a Trait file that enables easy handling of Enums.
Powered by Laravel Collections to make you feel at home. ๐งก
Installation
To install EnumConcern package, require it via composer:
Now, you can use EnumConcern in your Enums.
Methods
Method | Description | Parameters | Return Type |
---|---|---|---|
all |
Get all the values as a Collection. | method = '' (optional) |
Collection |
allAsArray |
Get all the values as an array. | method = '' (optional) |
array |
has |
Check if a specific value exists. | value , method = '' (optional) |
bool |
caseExists |
Check if a specific case (key / name) exists. | value , method = '' (optional) |
bool |
allCasesExists |
Check if all the given cases (keys / names) exist. | cases , method = '' (optional) |
bool |
anyCaseExists |
Check if any of the given cases (keys / names) exist. | cases , method = '' (optional) |
bool |
caseByValue |
Get the case (key / name) for a specific value. | value , method = '' (optional) |
string |
toJson |
Convert all the values to a JSON string. | method = '' (optional),jsonEncodeOption (optional) |
string |
toArray |
Convert all the values to an array. | method = '' (optional) |
array |
toKeyValueCollection |
Convert all the values to a key-value format as a Collection. | method = '' (optional) |
Collection |
toKeyValueArray |
Convert all the values to a key-value format as an array. | method = '' (optional) |
array |
randomValue |
Get a random value from the collection of values. | method = '' (optional) |
mixed |
randomCase |
Get a random case (key / name) from the collection of values. | None | string |
casesCollection |
Get all the cases (keys / names) of the Enum as a Collection. | None | Collection |
casesArray |
Get all the cases (keys / names) of the Enum as an array. | None | array |
allToArray |
Get all the values as an array. (Alias for toArray method) |
method = '' (optional) |
array |
only |
Get a subset of the values as a Collection, only including the specified cases (keys / names). | cases ,method = '' (optional) |
Collection |
onlyAsArray |
Get a subset of the values as an array, only including the specified cases (keys / names). | cases , method = '' (optional) |
array |
except |
Get a subset of the values as a Collection, excluding the specified cases (keys / names). | cases , method = '' (optional) |
Collection |
exceptAsArray |
Get a subset of the values as an array, excluding the specified cases (keys / names). | cases , method = '' (optional) |
array |
first |
Get the first value in the Enum. | method = '' (optional) |
mixed |
last |
Get the last value in the Enum. | method = '' (optional) |
mixed |
fromValue |
Create an Enum object from a string value. | value |
object |
valueNamePairs |
Get the key-value pairs of value and transformed value (if a method is specified). | method = '' (optional) |
Collection |
is |
Check if the Enum object is equal to the given object. | object |
bool |
isNot |
Check if the Enum object is not equal to the given object. | object |
bool |
isAny |
Check if the Enum object is equal to any of the given objects. | objects |
bool |
isNotAny |
Check if the Enum object is not equal to any of the given objects. | objects |
bool |
Basic Usage
You can check the Examples with All Methods section at the bellow of the document for more details.
Examples With All Methods
Step 1: Create Your Enum
Create an Enum class and uses the EnumConcern Trait.
Here's an example for this paper. I created a trait about fruits for the example, isn't it ingenious? ๐
Note: This README includes examples that are valid for both int
and string
types of Enum values. The EnumConcern Trait handles both types of Enum values in the same way. This allows you to use the EnumConcern Trait for both types of Enum in your project and facilitate the handling of Enum values.
Here's string Enum example:
Step 2: Enum Handling with EnumConcern
EnumConcern provides several convenient methods to handle your Enum values.
all() Method
Get all the Enum values as a Collection (empty $method)
Result:
Get all the Enum values as a Collection using 'emojis' method
Result:
Get all the Enum values as a Collection using 'names' method.
Result:
has() method
Check if a specific value exists (empty $method)
Check if a specific value exists using 'emojis' method
Check if a specific value exists using 'names' method
keyByValue() method
Get the case (key / name) for a specific value (empty $method)
Get the case (key / name) for a specific value using 'emojis' method
Get the case (key / name) for a specific value using 'names' method
toJson() method
Convert all the values to a JSON string (empty $method)
Convert all the values to a JSON string using 'emojis' method
Convert all the values to a JSON string using 'names' method
toArray() method
Convert all the values to an array (empty $method)
Result:
Convert all the values to an array using 'emojis' method
Result:
Convert all the values to an array using 'names' method
Result:
toKeyValueCollection() method
Convert all the values to a key-value format as a Collection (empty $method)
Result:
Convert all the values to a key-value format as a Collection with keyAttributeName and valueAttributeName parameters (empty $method)
Result:
Convert all the values to a key-value format as a Collection using 'emojis' method
Result:
Convert all the values to a key-value format as a Collection using 'emojis' method with keyAttributeName and valueAttributeName parameters
Result:
Convert all the values to a key-value format as a Collection using 'names' method
Result:
Convert all the values to a key-value format as a Collection using 'names' method with keyAttributeName and valueAttributeName parameters.
Result:
toKeyValueArray() method
Convert all the values to a key-value format as an array (empty $method)
Result:
Convert all the values to a key-value format as an array with keyAttributeName and valueAttributeName parameters (empty $method)
Result:
Convert all the values to a key-value format as an array using 'emojis' method
Result:
Convert all the values to a key-value format as an array using 'emojis' method with keyAttributeName and valueAttributeName parameters (empty $method)
Result:
Convert all the values to a key-value format as an array using 'names' method
Result:
Convert all the values to a key-value format as an array using 'names' method with keyAttributeName and valueAttributeName parameters (empty $method)
Result:
randomValue() method
Get a random value from the collection of values (empty $method)
Get a random value from the collection of values using 'emojis' method
Get a random value from the collection of values using 'names' method
randomKey() method
Get a random case (key / name) from the collection of values
only() Method
Get values of only certain keys as a Collection (empty $method)
Result:
Get values of only certain keys as a Collection using 'emojis' method
Result:
Get values of only certain keys as a Collection using 'names' method
Result:
onlyAsArray() Method
Get values of only certain keys as an array (empty $method)
Result:
Get values of only certain keys as an array using 'emojis' method
Result:
Get values of only certain keys as an array using 'names' method
Result:
except() Method
Get all values except certain keys as a Collection (empty $method)
Result:
Get all values except certain keys a Collection using 'emojis' method
Result:
Get all values except certain keys a Collection using 'names' method
Result:
exceptAsArray() Method
Get all values except certain keys as an array (empty $method)
Result:
Get all values except certain keys an array using 'emojis' method
Result:
Get all values except certain keys an array using 'names' method
Result:
first() Method
Get the first value from the Enum (empty $method)
Get the first value from the Enum using 'emojis' method
Get the first value from the Enum using 'names' method
last() Method
Get the last value from the Enum (empty $method)
Get the last value from the Enum using 'emojis' method
Get the last value from the Enum using 'names' method
fromValue() Method
Create an Enum object from a string value.
If the value "Green" exists in the Color
Enum, this method will return the corresponding Enum object. If not, it will throw an InvalidArgumentException
.
valueNamePairs() Method
Get the key-value pairs of value and transformed value (if a method is specified).
is() Method
Checks if the Enum value is equal to the given value.
isNot() Method
Checks if the Enum value is not equal to the given value.
isAny() Method
Checks if the Enum value is equal to any of the given values.
isNotAny() Method
Checks if the Enum value is not equal to any of the given values.