PHP code example of emreyarligan / enum-concern

1. Go to this page and download the library: Download emreyarligan/enum-concern library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

emreyarligan / enum-concern example snippets


namespace App\Enums;

use EmreYarligan\EnumConcern\EnumConcern;

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


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


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',
        };
    }
}

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";
}

Fruits::all();


Fruits::all('emojis');

Fruits::all('names');

Fruits::has(1);

// Result: true

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

// Result: true


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

// Result: false

Fruits::keyByValue(3);

// Result: "CHERRY"

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

// Result: "KIWI"

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

// Result: "ORANGE"

Fruits::toJson();

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

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

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

Fruits::toJson('names');

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

Fruits::toArray();

Fruits::toArray('emojis');

Fruits::toArray('names');

Fruits::toKeyValueCollection();

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

Fruits::toKeyValueCollection('emojis');

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

Fruits::toKeyValueCollection('names');

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

Fruits::toKeyValueArray();

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

Fruits::toKeyValueArray('emojis');

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

Fruits::toKeyValueArray('names');

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

Fruits::randomValue();

// Result: int(4)

Fruits::randomValue('emojis');

// Result: string(4) "🍊"

Fruits::randomValue('names');

// Result: string(6) "Kiwi"

Fruits::randomKey();

// Result: string(7) "KIWI"

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

  $enum = Fruits::BANANA;

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

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

  $enum = Fruits::BANANA;

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

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

  $enum = Fruits::BANANA;

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

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

  $enum = Fruits::BANANA;

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

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

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

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

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

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
    ]
  ]
}

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
    ]
  ]
}

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" => "🍎"
    ]
  ]
}

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" => "🍎"
    ]
  ]
}

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"
    ]
  ]
}

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"
    ]
  ]
}

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


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

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" => "🍎"
  ]
]

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" => "🍎"
  ]
]

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"
  ]
]

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"
  ]
]

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

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

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

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

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

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