PHPackages                             emreyarligan/enum-concern - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. emreyarligan/enum-concern

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

emreyarligan/enum-concern
=========================

A PHP package for effortless Enumeration handling with Laravel Collections 📦 ✨

1.0.7(11mo ago)21156.3k↑10.7%8[1 issues](https://github.com/emreyarligan/enum-concern/issues)1MITPHPPHP &gt;=8.1CI passing

Since Aug 20Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/emreyarligan/enum-concern)[ Packagist](https://packagist.org/packages/emreyarligan/enum-concern)[ Docs](https://github.com/emreyarligan/enum-concern)[ RSS](/packages/emreyarligan-enum-concern/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (2)Versions (10)Used By (1)

EnumConcern - A PHP Package for Effortless Enumeration Handling 📦 ✨
===================================================================

[](#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](https://laravel.com/docs/10.x/collections) to make you feel at home. 🧡

[![Latest Version](https://camo.githubusercontent.com/aedca08b62c43a3785dd06d46e533f029e39c1eaab12061219cc773937ea50e8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656d72657961726c6967616e2f656e756d2d636f6e6365726e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/emreyarligan/enum-concern)[![Total Downloads](https://camo.githubusercontent.com/0fb3151f639e4bb436282758bf08d273389e274120b1e93fe623bf623f78a02b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656d72657961726c6967616e2f656e756d2d636f6e6365726e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/emreyarligan/enum-concern)

---

Installation
------------

[](#installation)

To install EnumConcern package, require it via composer:

```
composer require emreyarligan/enum-concern
```

Now, you can use EnumConcern in your Enums.

```
namespace App\Enums;

use EmreYarligan\EnumConcern\EnumConcern;

enum TestEnum: string
{
    use EnumConcern;
    ...
    ...
}
```

---

Methods
-------

[](#methods)

MethodDescriptionParametersReturn 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
-----------

[](#basic-usage)

You can check the **Examples with All Methods** section at the bellow of the document for more details.

```
namespace App\Enums;

use EmreYarligan\EnumConcern\EnumConcern;

enum Color: string
{
    use EnumConcern;

    case RED = "Red";
    case GREEN = "Green";
    case BLUE = "Blue";

    public function translateToTurkish(): string
    {
        return match ($this) {
            self::RED    => 'Kırmızı',
            self::GREEN  => 'Yeşil',
            self::BLUE   => 'Mavi',
        };
    }

}

Color::all();
// Result: Illuminate\Support\Collection (7) [
//   [0] => 'Red',
//   [1] => 'Green',
//   [2] => 'Blue
// ]

Color::all('translateToTurkish');
// Result: Illuminate\Support\Collection (7) [
//   [0] => 'Kırmızı',
//   [1] => 'Yeşil',
//   [2] => 'Mavi
// ]

Color::has('Purple');
// false

Color::has('Mavi','translateToTurkish');
// true
```

---

Examples With All Methods
-------------------------

[](#examples-with-all-methods)

Step 1: Create Your Enum
------------------------

[](#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? 😛

```
namespace App\Enums;

use EmreYarligan\EnumConcern\EnumConcern;

enum Fruits: int
{
    use EnumConcern;

    case BANANA = 1;
    case STRAWBERRY = 2;
    case CHERRY = 3;
    case WATERMELON = 4;
    case ORANGE = 5;
    case KIWI = 6;
    case APPLE = 7;

    // Custom methods
    public function emojis(): string
    {
        return match ($this) {
            self::BANANA        => '🍌',
            self::STRAWBERRY    => '🍓',
            self::CHERRY        => '🍒',
            self::WATERMELON    => '🍉',
            self::ORANGE        => '🍊',
            self::KIWI          => '🥝',
            self::APPLE         => '🍎',
        };
    }

    public function names(): string
    {
        return match ($this) {
            self::BANANA        => 'Banana',
            self::STRAWBERRY    => 'Strawberry',
            self::CHERRY        => 'Cherry',
            self::WATERMELON    => 'Watermelon',
            self::ORANGE        => 'Orange',
            self::KIWI          => 'Kiwi',
            self::APPLE         => 'Apple',
        };
    }
}
```

**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:

```
namespace App\Enums;

use EmreYarligan\EnumConcern\EnumConcern;

enum Fruits: string
{
    use EnumConcern;

    case BANANA = "Delicious Banana";
    case STRAWBERRY = 'Red Strawberry';
    case CHERRY = "Sweet Cherry";
    case WATERMELON = "juicy watermelon";
    case ORANGE = "Tasty Orange";
    case KIWI = "Green Kiwi";
    case APPLE = "Crunchy Apple";
}
```

---

Step 2: Enum Handling with EnumConcern
--------------------------------------

[](#step-2-enum-handling-with-enumconcern)

EnumConcern provides several convenient methods to handle your Enum values.

### all() Method

[](#all-method)

Get all the Enum values as a Collection (empty $method)

```
Fruits::all();
```

Result:

```
Illuminate\Support\Collection {
  #items: array:7 [
    "BANANA" => 1
    "STRAWBERRY" => 2
    "CHERRY" => 3
    "WATERMELON" => 4
    "ORANGE" => 5
    "KIWI" => 6
    "APPLE" => 7
  ]
}

```

Get all the Enum values as a Collection using 'emojis' method

```
Fruits::all('emojis');
```

Result:

```
Illuminate\Support\Collection {
  #items: array:7 [
    "BANANA" => "🍌"
    "STRAWBERRY" => "🍓"
    "CHERRY" => "🍒"
    "WATERMELON" => "🍉"
    "ORANGE" => "🍊"
    "KIWI" => "🥝"
    "APPLE" => "🍎"
  ]
}

```

Get all the Enum values as a Collection using 'names' method.

```
Fruits::all('names');
```

Result:

```
Illuminate\Support\Collection {
  #items: array:7 [
    "BANANA" => "Banana"
    "STRAWBERRY" => "Strawberry"
    "CHERRY" => "Cherry"
    "WATERMELON" => "Watermelon"
    "ORANGE" => "Orange"
    "KIWI" => "Kiwi"
    "APPLE" => "Apple"
  ]
}

```

---

### has() method

[](#has-method)

Check if a specific value exists (empty $method)

```
Fruits::has(1);

// Result: true
```

Check if a specific value exists using 'emojis' method

```
Fruits::has('🍉', 'emojis');

// Result: true
```

Check if a specific value exists using 'names' method

```
Fruits::has('Coconut', 'names');

// Result: false
```

---

### keyByValue() method

[](#keybyvalue-method)

Get the case (key / name) for a specific value (empty $method)

```
Fruits::keyByValue(3);

// Result: "CHERRY"
```

Get the case (key / name) for a specific value using 'emojis' method

```
Fruits::keyByValue('🥝', 'emojis');

// Result: "KIWI"
```

Get the case (key / name) for a specific value using 'names' method

```
Fruits::keyByValue('Orange', 'names');

// Result: "ORANGE"
```

---

### toJson() method

[](#tojson-method)

Convert all the values to a JSON string (empty $method)

```
Fruits::toJson();

// Result: "{"BANANA":1,"STRAWBERRY":2,"CHERRY":3,"WATERMELON":4,"ORANGE":5,"KIWI":6,"APPLE":7}"
```

Convert all the values to a JSON string using 'emojis' method

```
Fruits::toJson('emojis',JSON_UNESCAPED_UNICODE);

// Result: "{"BANANA":"🍌","STRAWBERRY":"🍓",...,"APPLE":"🍎"}"
```

Convert all the values to a JSON string using 'names' method

```
Fruits::toJson('names');

// Result: "{"BANANA":"Banana","STRAWBERRY":"Strawberry","CHERRY":"Cherry","WATERMELON":"Watermelon","ORANGE":"Orange","KIWI":"Kiwi","APPLE":"Apple"}"
```

---

### toArray() method

[](#toarray-method)

Convert all the values to an array (empty $method)

```
Fruits::toArray();
```

Result:

```
array:7 [
  "BANANA" => 1
  "STRAWBERRY" => 2
  "CHERRY" => 3
  "WATERMELON" => 4
  "ORANGE" => 5
  "KIWI" => 6
  "APPLE" => 7
]

```

Convert all the values to an array using 'emojis' method

```
Fruits::toArray('emojis');
```

Result:

```
array:7 [
  "BANANA" => "🍌"
  "STRAWBERRY" => "🍓"
  "CHERRY" => "🍒"
  "WATERMELON" => "🍉"
  "ORANGE" => "🍊"
  "KIWI" => "🥝"
  "APPLE" => "🍎"
]

```

Convert all the values to an array using 'names' method

```
Fruits::toArray('names');
```

Result:

```
array:7 [
  "BANANA" => "Banana"
  "STRAWBERRY" => "Strawberry"
  "CHERRY" => "Cherry"
  "WATERMELON" => "Watermelon"
  "ORANGE" => "Orange"
  "KIWI" => "Kiwi"
  "APPLE" => "Apple"
]

```

---

### toKeyValueCollection() method

[](#tokeyvaluecollection-method)

Convert all the values to a key-value format as a Collection (empty $method)

```
Fruits::toKeyValueCollection();
```

Result:

```
Illuminate\Support\Collection {
  #items: array:7 [
    "BANANA" => array:2 [
      "key" => "BANANA"
      "value" => 1
    ]
    "STRAWBERRY" => array:2 [
      "key" => "STRAWBERRY"
      "value" => 2
    ]
    "CHERRY" => array:2 [
      "key" => "CHERRY"
      "value" => 3
    ]
    "WATERMELON" => array:2 [
      "key" => "WATERMELON"
      "value" => 4
    ]
    "ORANGE" => array:2 [
      "key" => "ORANGE"
      "value" => 5
    ]
    "KIWI" => array:2 [
      "key" => "KIWI"
      "value" => 6
    ]
    "APPLE" => array:2 [
      "key" => "APPLE"
      "value" => 7
    ]
  ]
}

```

Convert all the values to a key-value format as a Collection with keyAttributeName and valueAttributeName parameters (empty $method)

```
Fruits::toKeyValueCollection(keyAttributeName: 'foo', valueAttributeName: 'bar');
```

Result:

```
Illuminate\Support\Collection {
  #items: array:7 [
    "BANANA" => array:2 [
      "foo" => "BANANA"
      "bar" => 1
    ]
    "STRAWBERRY" => array:2 [
      "foo" => "STRAWBERRY"
      "bar" => 2
    ]
    "CHERRY" => array:2 [
      "foo" => "CHERRY"
      "bar" => 3
    ]
    "WATERMELON" => array:2 [
      "foo" => "WATERMELON"
      "bar" => 4
    ]
    "ORANGE" => array:2 [
      "foo" => "ORANGE"
      "bar" => 5
    ]
    "KIWI" => array:2 [
      "foo" => "KIWI"
      "bar" => 6
    ]
    "APPLE" => array:2 [
      "foo" => "APPLE"
      "bar" => 7
    ]
  ]
}

```

Convert all the values to a key-value format as a Collection using 'emojis' method

```
Fruits::toKeyValueCollection('emojis');
```

Result:

```
Illuminate\Support\Collection {
  #items: array:7 [
    "BANANA" => array:2 [
      "key" => "BANANA"
      "value" => "🍌"
    ]
    "STRAWBERRY" => array:2 [
      "key" => "STRAWBERRY"
      "value" => "🍓"
    ]
    "CHERRY" => array:2 [
      "key" => "CHERRY"
      "value" => "🍒"
    ]
    "WATERMELON" => array:2 [
      "key" => "WATERMELON"
      "value" => "🍉"
    ]
    "ORANGE" => array:2 [
      "key" => "ORANGE"
      "value" => "🍊"
    ]
    "KIWI" => array:2 [
      "key" => "KIWI"
      "value" => "🥝"
    ]
    "APPLE" => array:2 [
      "key" => "APPLE"
      "value" => "🍎"
    ]
  ]
}

```

Convert all the values to a key-value format as a Collection using 'emojis' method with keyAttributeName and valueAttributeName parameters

```
Fruits::toKeyValueCollection('emojis','foo','bar');
```

Result:

```
Illuminate\Support\Collection {
  #items: array:7 [
    "BANANA" => array:2 [
      "foo" => "BANANA"
      "bar" => "🍌"
    ]
    "STRAWBERRY" => array:2 [
      "foo" => "STRAWBERRY"
      "bar" => "🍓"
    ]
    "CHERRY" => array:2 [
      "foo" => "CHERRY"
      "bar" => "🍒"
    ]
    "WATERMELON" => array:2 [
      "foo" => "WATERMELON"
      "bar" => "🍉"
    ]
    "ORANGE" => array:2 [
      "foo" => "ORANGE"
      "bar" => "🍊"
    ]
    "KIWI" => array:2 [
      "foo" => "KIWI"
      "bar" => "🥝"
    ]
    "APPLE" => array:2 [
      "foo" => "APPLE"
      "bar" => "🍎"
    ]
  ]
}

```

Convert all the values to a key-value format as a Collection using 'names' method

```
Fruits::toKeyValueCollection('names');
```

Result:

```
Illuminate\Support\Collection {
  #items: array:7 [
    "BANANA" => array:2 [
      "key" => "BANANA"
      "value" => "Banana"
    ]
    "STRAWBERRY" => array:2 [
      "key" => "STRAWBERRY"
      "value" => "Strawberry"
    ]
    "CHERRY" => array:2 [
      "key" => "CHERRY"
      "value" => "Cherry"
    ]
    "WATERMELON" => array:2 [
      "key" => "WATERMELON"
      "value" => "Watermelon"
    ]
    "ORANGE" => array:2 [
      "key" => "ORANGE"
      "value" => "Orange"
    ]
    "KIWI" => array:2 [
      "key" => "KIWI"
      "value" => "Kiwi"
    ]
    "APPLE" => array:2 [
      "key" => "APPLE"
      "value" => "Apple"
    ]
  ]
}

```

Convert all the values to a key-value format as a Collection using 'names' method with keyAttributeName and valueAttributeName parameters.

```
Fruits::toKeyValueCollection('names', 'foo', 'bar');
```

Result:

```
Illuminate\Support\Collection {
  #items: array:7 [
    "BANANA" => array:2 [
      "foo" => "BANANA"
      "bar" => "Banana"
    ]
    "STRAWBERRY" => array:2 [
      "foo" => "STRAWBERRY"
      "bar" => "Strawberry"
    ]
    "CHERRY" => array:2 [
      "foo" => "CHERRY"
      "bar" => "Cherry"
    ]
    "WATERMELON" => array:2 [
      "foo" => "WATERMELON"
      "bar" => "Watermelon"
    ]
    "ORANGE" => array:2 [
      "foo" => "ORANGE"
      "bar" => "Orange"
    ]
    "KIWI" => array:2 [
      "foo" => "KIWI"
      "bar" => "Kiwi"
    ]
    "APPLE" => array:2 [
      "foo" => "APPLE"
      "bar" => "Apple"
    ]
  ]
}

```

---

### toKeyValueArray() method

[](#tokeyvaluearray-method)

Convert all the values to a key-value format as an array (empty $method)

```
Fruits::toKeyValueArray();
```

Result:

```
array:7 [
  0 => array:2 [
    "key" => "BANANA"
    "value" => 1
  ]
  1 => array:2 [
    "key" => "STRAWBERRY"
    "value" => 2
  ]
  2 => array:2 [
    "key" => "CHERRY"
    "value" => 3
  ]
  3 => array:2 [
    "key" => "WATERMELON"
    "value" => 4
  ]
  4 => array:2 [
    "key" => "ORANGE"
    "value" => 5
  ]
  5 => array:2 [
    "key" => "KIWI"
    "value" => 6
  ]
  6 => array:2 [
    "key" => "APPLE"
    "value" => 7
  ]
]

```

Convert all the values to a key-value format as an array with keyAttributeName and valueAttributeName parameters (empty $method)

```
Fruits::toKeyValueArray(keyAttributeName: 'foo', valueAttributeName: 'bar');
```

Result:

```
array:7 [
  0 => array:2 [
    "foo" => "BANANA"
    "bar" => 1
  ]
  1 => array:2 [
    "foo" => "STRAWBERRY"
    "bar" => 2
  ]
  2 => array:2 [
    "foo" => "CHERRY"
    "bar" => 3
  ]
  3 => array:2 [
    "foo" => "WATERMELON"
    "bar" => 4
  ]
  4 => array:2 [
    "foo" => "ORANGE"
    "bar" => 5
  ]
  5 => array:2 [
    "foo" => "KIWI"
    "bar" => 6
  ]
  6 => array:2 [
    "foo" => "APPLE"
    "bar" => 7
  ]
]

```

Convert all the values to a key-value format as an array using 'emojis' method

```
Fruits::toKeyValueArray('emojis');
```

Result:

```
array:7 [
  0 => array:2 [
    "key" => "BANANA"
    "value" => "🍌"
  ]
  1 => array:2 [
    "key" => "STRAWBERRY"
    "value" => "🍓"
  ]
  2 => array:2 [
    "key" => "CHERRY"
    "value" => "🍒"
  ]
  3 => array:2 [
    "key" => "WATERMELON"
    "value" => "🍉"
  ]
  4 => array:2 [
    "key" => "ORANGE"
    "value" => "🍊"
  ]
  5 => array:2 [
    "key" => "KIWI"
    "value" => "🥝"
  ]
  6 => array:2 [
    "key" => "APPLE"
    "value" => "🍎"
  ]
]

```

Convert all the values to a key-value format as an array using 'emojis' method with keyAttributeName and valueAttributeName parameters (empty $method)

```
Fruits::toKeyValueArray('emojis','foo','bar');
```

Result:

```
array:7 [
  0 => array:2 [
    "foo" => "BANANA"
    "bar" => "🍌"
  ]
  1 => array:2 [
    "foo" => "STRAWBERRY"
    "bar" => "🍓"
  ]
  2 => array:2 [
    "foo" => "CHERRY"
    "bar" => "🍒"
  ]
  3 => array:2 [
    "foo" => "WATERMELON"
    "bar" => "🍉"
  ]
  4 => array:2 [
    "foo" => "ORANGE"
    "bar" => "🍊"
  ]
  5 => array:2 [
    "foo" => "KIWI"
    "bar" => "🥝"
  ]
  6 => array:2 [
    "foo" => "APPLE"
    "bar" => "🍎"
  ]
]

```

Convert all the values to a key-value format as an array using 'names' method

```
Fruits::toKeyValueArray('names');
```

Result:

```
array:7 [
  0 => array:2 [
    "key" => "BANANA"
    "value" => "Banana"
  ]
  1 => array:2 [
    "key" => "STRAWBERRY"
    "value" => "Strawberry"
  ]
  2 => array:2 [
    "key" => "CHERRY"
    "value" => "Cherry"
  ]
  3 => array:2 [
    "key" => "WATERMELON"
    "value" => "Watermelon"
  ]
  4 => array:2 [
    "key" => "ORANGE"
    "value" => "Orange"
  ]
  5 => array:2 [
    "key" => "KIWI"
    "value" => "Kiwi"
  ]
  6 => array:2 [
    "key" => "APPLE"
    "value" => "Apple"
  ]
]

```

Convert all the values to a key-value format as an array using 'names' method with keyAttributeName and valueAttributeName parameters (empty $method)

```
Fruits::toKeyValueArray('names','foo','bar');
```

Result:

```
array:7 [
  0 => array:2 [
    "foo" => "BANANA"
    "bar" => "Banana"
  ]
  1 => array:2 [
    "foo" => "STRAWBERRY"
    "bar" => "Strawberry"
  ]
  2 => array:2 [
    "foo" => "CHERRY"
    "bar" => "Cherry"
  ]
  3 => array:2 [
    "foo" => "WATERMELON"
    "bar" => "Watermelon"
  ]
  4 => array:2 [
    "foo" => "ORANGE"
    "bar" => "Orange"
  ]
  5 => array:2 [
    "foo" => "KIWI"
    "bar" => "Kiwi"
  ]
  6 => array:2 [
    "foo" => "APPLE"
    "bar" => "Apple"
  ]
]

```

---

### randomValue() method

[](#randomvalue-method)

Get a random value from the collection of values (empty $method)

```
Fruits::randomValue();

// Result: int(4)
```

Get a random value from the collection of values using 'emojis' method

```
Fruits::randomValue('emojis');

// Result: string(4) "🍊"
```

Get a random value from the collection of values using 'names' method

```
Fruits::randomValue('names');

// Result: string(6) "Kiwi"
```

---

### randomKey() method

[](#randomkey-method)

Get a random case (key / name) from the collection of values

```
Fruits::randomKey();

// Result: string(7) "KIWI"
```

### only() Method

[](#only-method)

Get values of only certain keys as a Collection (empty $method)

```
Fruits::only(['STRAWBERRY','CHERRY','WATERMELON','ORANGE']);
```

Result:

```
Illuminate\Support\Collection {
  #items: array:4 [
    "STRAWBERRY" => 2
    "CHERRY" => 3
    "WATERMELON" => 4
    "ORANGE" => 5
  ]
}

```

Get values of only certain keys as a Collection using 'emojis' method

```
Fruits::only(['STRAWBERRY','CHERRY','WATERMELON','ORANGE'],'emojis');
```

Result:

```
Illuminate\Support\Collection {
  #items: array:4 [
    "STRAWBERRY" => "🍓"
    "CHERRY" => "🍒"
    "WATERMELON" => "🍉"
    "ORANGE" => "🍊"
  ]
}

```

Get values of only certain keys as a Collection using 'names' method

```
Fruits::only(['STRAWBERRY','CHERRY','WATERMELON','ORANGE'],'names');
```

Result:

```
Illuminate\Support\Collection {
  #items: array:4 [
    "STRAWBERRY" => "Strawberry"
    "CHERRY" => "Cherry"
    "WATERMELON" => "Watermelon"
    "ORANGE" => "Orange"
  ]
}

```

---

### onlyAsArray() Method

[](#onlyasarray-method)

Get values of only certain keys as an array (empty $method)

```
Fruits::onlyAsArray(['STRAWBERRY','CHERRY','WATERMELON','ORANGE']);
```

Result:

```
array:4 [
  "STRAWBERRY" => 2
  "CHERRY" => 3
  "WATERMELON" => 4
  "ORANGE" => 5
]

```

Get values of only certain keys as an array using 'emojis' method

```
Fruits::onlyAsArray(['STRAWBERRY','CHERRY','WATERMELON','ORANGE'],'emojis');
```

Result:

```
array:4 [
  "STRAWBERRY" => "🍓"
  "CHERRY" => "🍒"
  "WATERMELON" => "🍉"
  "ORANGE" => "🍊"
]

```

Get values of only certain keys as an array using 'names' method

```
Fruits::onlyAsArray(['STRAWBERRY','CHERRY','WATERMELON','ORANGE'],'names');
```

Result:

```
array:4 [
  "STRAWBERRY" => "Strawberry"
  "CHERRY" => "Cherry"
  "WATERMELON" => "Watermelon"
  "ORANGE" => "Orange"
]

```

---

### except() Method

[](#except-method)

Get all values except certain keys as a Collection (empty $method)

```
Fruits::except(['STRAWBERRY','CHERRY','WATERMELON','ORANGE']);
```

Result:

```
Illuminate\Support\Collection {
  #items: array:3 [
    "BANANA" => 1
    "KIWI" => 6
    "APPLE" => 7
  ]
}

```

Get all values except certain keys a Collection using 'emojis' method

```
Fruits::except(['STRAWBERRY','CHERRY','WATERMELON','ORANGE'],'emojis');
```

Result:

```
Illuminate\Support\Collection {
  #items: array:3 [
    "BANANA" => "🍌"
    "KIWI" => "🥝"
    "APPLE" => "🍎"
  ]
}

```

Get all values except certain keys a Collection using 'names' method

```
Fruits::except(['STRAWBERRY','CHERRY','WATERMELON','ORANGE'],'names');
```

Result:

```
Illuminate\Support\Collection {
  #items: array:3 [
    "BANANA" => "Banana"
    "KIWI" => "Kiwi"
    "APPLE" => "Apple"
  ]
}

```

---

### exceptAsArray() Method

[](#exceptasarray-method)

Get all values except certain keys as an array (empty $method)

```
Fruits::exceptAsArray(['STRAWBERRY','CHERRY','WATERMELON','ORANGE'])
```

Result:

```
array:3 [
  "BANANA" => 1
  "KIWI" => 6
  "APPLE" => 7
]

```

Get all values except certain keys an array using 'emojis' method

```
Fruits::exceptAsArray(['STRAWBERRY','CHERRY','WATERMELON','ORANGE'],'emojis');
```

Result:

```
array:3 [
  "BANANA" => "🍌"
  "KIWI" => "🥝"
  "APPLE" => "🍎"
]

```

Get all values except certain keys an array using 'names' method

```
Fruits::exceptAsArray(['STRAWBERRY','CHERRY','WATERMELON','ORANGE'],'names');
```

Result:

```
array:3 [
  "BANANA" => "Banana"
  "KIWI" => "Kiwi"
  "APPLE" => "Apple"
]

```

---

### first() Method

[](#first-method)

Get the first value from the Enum (empty $method)

```
Fruits::first();
// Result: int(1)
```

Get the first value from the Enum using 'emojis' method

```
Fruits::first('emojis');
// Result: "🍌"
```

Get the first value from the Enum using 'names' method

```
Fruits::first('names');
// Result: "Banana"
```

---

### last() Method

[](#last-method)

Get the last value from the Enum (empty $method)

```
Fruits::last();
// Result: 7
```

Get the last value from the Enum using 'emojis' method

```
Fruits::last('emojis');
// Result: "🍎"
```

Get the last value from the Enum using 'names' method

```
Fruits::last('names');
// Result: "Apple"
```

---

### fromValue() Method

[](#fromvalue-method)

Create an Enum object from a string value.

```
$greenEnum = Color::fromValue("Green");
// Result:
// App\Enums\Color {
//   +name: "GREEN"
//   +value: "Green"
// }
```

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

[](#valuenamepairs-method)

Get the key-value pairs of value and transformed value (if a method is specified).

```
$pairs = Color::valueNamePairs('translateToTurkish');
// Result:
// Illuminate\Support\Collection {
//    "Red" => "Kırmızı",
//    "Green" => "Yeşil",
//    "Blue" => "Mavi"
// }
```

---

### is() Method

[](#is-method)

Checks if the Enum value is equal to the given value.

```
  $enum = Fruits::BANANA;

  $enum->is(Fruits::BANANA);
  // Result: true

  $enum->is(Fruits::APPLE);
  // Result: false
```

### isNot() Method

[](#isnot-method)

Checks if the Enum value is not equal to the given value.

```
  $enum = Fruits::BANANA;

  $enum->isNot(Fruits::BANANA);
  // Result: false

  $enum->isNot(Fruits::APPLE);
  // Result: true
```

### isAny() Method

[](#isany-method)

Checks if the Enum value is equal to any of the given values.

```
  $enum = Fruits::BANANA;

  $enum->isAny(Fruits::BANANA, Fruits::APPLE);
  // Result: true

  $enum->isAny(Fruits::APPLE, Fruits::KIWI);
  // Result: false
```

### isNotAny() Method

[](#isnotany-method)

Checks if the Enum value is not equal to any of the given values.

```
  $enum = Fruits::BANANA;

  $enum->isNotAny(Fruits::BANANA, Fruits::APPLE);
  // Result: false

  $enum->isNotAny(Fruits::APPLE, Fruits::KIWI);
  // Result: true
```

Tests
-----

[](#tests)

```
composer test
```

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance71

Regular maintenance activity

Popularity47

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~92 days

Recently: every ~121 days

Total

8

Last Release

357d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3f22f2b99d70208fc8ac711eeac1ded6879057045256f6d3a4407a7bf243d7da?d=identicon)[emreyarligan](/maintainers/emreyarligan)

---

Top Contributors

[![emreyarligan](https://avatars.githubusercontent.com/u/26210131?v=4)](https://github.com/emreyarligan "emreyarligan (22 commits)")[![umutphp](https://avatars.githubusercontent.com/u/3245166?v=4)](https://github.com/umutphp "umutphp (7 commits)")[![oralunal](https://avatars.githubusercontent.com/u/1948737?v=4)](https://github.com/oralunal "oralunal (7 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (3 commits)")[![MrPunyapal](https://avatars.githubusercontent.com/u/53343069?v=4)](https://github.com/MrPunyapal "MrPunyapal (3 commits)")[![GregoireGaonach](https://avatars.githubusercontent.com/u/26883777?v=4)](https://github.com/GregoireGaonach "GregoireGaonach (3 commits)")[![serter35](https://avatars.githubusercontent.com/u/28671154?v=4)](https://github.com/serter35 "serter35 (2 commits)")[![moelkomy](https://avatars.githubusercontent.com/u/70802292?v=4)](https://github.com/moelkomy "moelkomy (1 commits)")[![jackwh](https://avatars.githubusercontent.com/u/627533?v=4)](https://github.com/jackwh "jackwh (1 commits)")

---

Tags

concernsenumenumerationlaravelpackagelaravelenumenumerationconcern

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/emreyarligan-enum-concern/health.svg)

```
[![Health](https://phpackages.com/badges/emreyarligan-enum-concern/health.svg)](https://phpackages.com/packages/emreyarligan-enum-concern)
```

###  Alternatives

[cerbero/laravel-enum

Laravel package to supercharge enum functionalities.

18989.6k](/packages/cerbero-laravel-enum)[mindtwo/native-enum

Package for using native php enums.

2626.0k1](/packages/mindtwo-native-enum)[nasyrov/laravel-enums

Laravel package for Enum implementation.

3272.5k](/packages/nasyrov-laravel-enums)[artisaninweb/laravel-enum

A provider for Enums in Laravel.

1187.5k](/packages/artisaninweb-laravel-enum)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2516.7k](/packages/iteks-laravel-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
