PHP code example of iteks / laravel-enum

1. Go to this page and download the library: Download iteks/laravel-enum 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/ */

    

iteks / laravel-enum example snippets


use Iteks\Attributes\Description;
use Iteks\Traits\BackedEnum;
use Iteks\Traits\HasAttributes;

enum BackedEnumShape: string
{
    use BackedEnum;
    use HasAttributes;

    #[Description('A circle is a perfectly round geometric figure, every point on the circle is equidistant from the center')]
    case RoundCircle = 'circle';
}

enum BackedEnumShape: string
{
    #[Description('A circle is a perfectly round geometric figure, every point on the circle is equidistant from the center')]
    case RoundCircle = 'circle';
}

enum BackedEnumShape: string
{
    #[Id(1)]
    case RoundCircle = 'circle';
}

enum BackedEnumShape: string
{
    #[Label('Round Circle')]
    case RoundCircle = 'circle';
}

enum BackedEnumShape: string
{
    #[Metadata(['color' => 'red', 'sides' => 0, 'type' => 'curved'])] // Metadata as array
    case RoundCircle = 'circle';

    #[Metadata('{"color": "blue", "sides": 4, "type": "polygon", "regular": true}')] // Metadata as JSON string
    case BoxSquare = 'square';
}

use Iteks\Facades\Enum;

$options = Enum::asSelectArray(BackedEnumShape::class);

// Result:
[
    ['text' => 'Round Circle', 'value' => 1],
    ['text' => 'Perfect Square', 'value' => 2],
    ['text' => 'Right-Triangle', 'value' => 3],
    ['text' => '5 Point Star', 'value' => 4],
    ['text' => 'Poly-Rectangle', 'value' => 5]
]

$label = Enum::toLabel(BackedEnumShape::RoundCircle);

// Result:
'Round Circle'

$labels = Enum::toLabels();

// Result:
['Round Circle', 'Box Square', 'Right Triangle', 'Pointed Star', 'Polygon Rectangle']

use Iteks\Facades\Enum;

$attributes = Enum::attributes(BackedEnumShape::RoundCircle);

// Result:
[
    'simpleValue' => 'circle',
    'description' => 'A circle is a perfectly round geometric figure, every point on the circle is equidistant from the center',
    'id' => 1,
    'label' => 'Round Circle',
    'metadata' => [
        'color' => 'red',
        'sides' => 0,
        'type' => 'curved'
    ]
]

$attributes = Enum::attributes(BackedEnumShape::RoundCircle, ['id', 'label']);

// Result:
[
    'id' => 1,
    'label' => 'Round Circle'
]

$attributes = Enum::attributes(BackedEnumShape::class);

// Result:
[
    'RoundCircle' => [
        'simpleValue' => 'circle',
        'description' => 'A circle is a perfectly round geometric figure, every point on the circle is equidistant from the center',
        'id' => 1,
        'label' => 'Round Circle',
        'metadata' => [,
            'color' => 'red',
            'sides' => 0,
            'type' => 'curved'
        ]
    ],
    'BoxSquare' => [
        'simpleValue' => 'square',
        'description' => 'A square is a regular quadrilateral',
        'id' => 2,
        'label' => 'Perfect Square',
        'metadata' => [
            'color' => 'blue',
            'sides' => 4,
            'type' => 'polygon',
            'regular' => true
        ]
    ],
    'RightTriangle' => [
        'simpleValue' => 'triangle',
        'description' => 'A triangle is a polygon with three edges and three vertices',
        'id' => 3,
        'label' => 'Right-Triangle',
        'metadata' => [,
            'color' => 'green',
            'sides' => 3,
            'type' => 'polygon'
        ]
    ],
    ...
]

$attributes = Enum::attributes(BackedEnumShape::class, ['description', 'label']);

// Result:
[
    'RoundCircle' => [
        'description' => 'A circle is a perfectly round geometric figure, every point on the circle is equidistant from the center',
        'label' => 'Round Circle'
    ],
    'BoxSquare' => [
        'description' => 'A square is a regular quadrilateral',
        'label' => 'Perfect Square'
    ],
    'RightTriangle' => [
        'description' => 'A triangle is a polygon with three edges and three vertices',
        'label' => 'Right-Triangle'
    ],
    ...
]

$description = Enum::description(BackedEnumShape::RoundCircle);

// Result:
'A circle is a perfectly round geometric figure, every point on the circle is equidistant from the center'

$descriptions = Enum::descriptions(BackedEnumShape::class);

// Result:
[
    'A circle is a perfectly round geometric figure, every point on the circle is equidistant from the center',
    'A square is a regular quadrilateral',
    'A triangle is a polygon with three edges and three vertices',
    'A star is a self-intersecting, equilateral polygon',
    'A rectangle is a quadrilateral with four right angles'
]

$descriptions = Enum::descriptions(BackedEnumShape::class, 'name');

// Result:
[
    'RoundCircle' => 'A circle is a perfectly round geometric figure, every point on the circle is equidistant from the center',
    'BoxSquare' => 'A square is a regular quadrilateral',
    ...
]

$id = Enum::id(BackedEnumShape::PointedStar);

// Result:
4

$ids = Enum::ids(BackedEnumShape::class);

// Result:
[1, 2, 3, 4, 5]

$ids = Enum::ids(BackedEnumShape::class, 'value');

// Result:
['circle' => 1, 'square' => 2, 'triangle' => 3, 'star' => 4, 'rectangle' => 5]

$label = Enum::label(BackedEnumShape::PolygonRectangle);

// Result:
'Poly-Rectangle'

$labels = Enum::labels(BackedEnumShape::class);

// Result:
['Round Circle', 'Perfect Square', 'Right-Triangle', '5 Point Star', 'Poly-Rectangle']

$allMetadata = Enum::metadata(BackedEnumShape::BoxSquare);

// Result:
[
    'color' => 'blue',
    'sides' => 4,
    'type' => 'polygon',
    'regular' => true
]

$specific = Enum::metadata(BackedEnumShape::BoxSquare, 'color');

// Result:
'blue'

$filteredMetadata = Enum::metadata(BackedEnumShape::BoxSquare, ['color', 'type']);

// Result:
[
    'color' => 'blue',
    'type' => 'polygon'
]

$allMetadatum = Enum::metadatum(BackedEnumShape::class);

// Result:
[
    ['color' => 'red', 'sides' => 0, 'type' => 'curved'],
    ['color' => 'blue', 'sides' => 4, 'type' => 'polygon', 'regular' => true],
    ['color' => 'green', 'sides' => 3, 'type' => 'polygon'],
    ['color' => 'yellow', 'points' => 5, 'type' => 'star', 'symmetrical' => true],
    ['color' => 'purple', 'sides' => 4, 'type' => 'polygon', 'regular' => false]
]

$specific = Enum::metadatum(BackedEnumShape::class, 'color');

// Result:
['red', 'blue', 'green', 'yellow', 'purple']

$keyBy = Enum::metadatum(BackedEnumShape::class, 'color', 'value');

// Result:
[
    'circle' => 'red',
    'square' => 'blue',
    'triangle' => 'green',
    'star' => 'yellow',
    'rectangle' => 'purple'
]

$filteredMetadatum = Enum::metadatum(BackedEnumShape::class, ['color', 'sides']);

// Result:
[
    ['color' => 'red', 'sides' => 0],
    ['color' => 'blue', 'sides' => 4],
    ['color' => 'green', 'sides' => 3],
    ['color' => 'yellow'],
    ['color' => 'purple', 'sides' => 4]
]

use Iteks\Traits\BackedEnum;

enum BackedEnumShape: string
{
    use BackedEnum;
    ...

$options = BackedEnumShape::asSelectArray();

// Result:
[
    ['text' => 'Round Circle', 'value' => 1],
    ['text' => 'Perfect Square', 'value' => 2],
    ['text' => 'Right-Triangle', 'value' => 3],
    ['text' => '5 Point Star', 'value' => 4],
    ['text' => 'Poly-Rectangle', 'value' => 5]
]

$enum = BackedEnumShape::fromName('RoundCircle');

// Result:
// The enum instance of BackedEnumShape::RoundCircle
{
    +name: "RoundCircle"
    +value: "circle"
}

$name = BackedEnumShape::name('square');

// Result:
'BoxSquare'

$names = BackedEnumShape::names();

// Result:
[
    'RoundCircle',
    'BoxSquare',
    'RightTriangle',
    'PointedStar',
    'PolygonRectangle'
]

$label = BackedEnumShape::toLabel('triangle');

// Result:
'Right-Triangle'

$labels = BackedEnumShape::toLabels();

// Result:
[
    'Round Circle',
    'Box Square',
    'Right Triangle',
    'Pointed Star',
    'Polygon Rectangle'
]

$enum = BackedEnumShape::tryFromName('RoundCircle');

// Result:
// The enum instance of BackedEnumShape::RoundCircle
{
    +name: "RoundCircle"
    +value: "circle"
}

$value = BackedEnumShape::value('RoundCircle');

// Result:
'circle'

$values = BackedEnumShape::values();

// Result:
[
    'circle',
    'square',
    'triangle',
    'star',
    'rectangle'
]

use Iteks\Traits\BackedEnum;
use Iteks\Traits\HasAttributes;

enum BackedEnumShape: string
{
    use BackedEnum;
    use HasAttributes;
    ...

$attributes = BackedEnumShape::attributes('RoundCircle');

// Result:
[
    'simpleValue' => 'circle',
    'description' => 'A circle is a perfectly round geometric figure, every point on the circle is equidistant from the center',
    'id' => 1,
    'label' => 'Round Circle',
    'metadata' => [
        'color' => 'red',
        'sides' => 0,
        'type' => 'curved'
    ]
]

$attributes = BackedEnumShape::attributes('RoundCircle', ['id', 'label']);

// Result:
[
    'id' => 1,
    'label' => 'Round Circle'
]

$attributes = BackedEnumShape::attributes();

// Result:
[
    'RoundCircle' => [
        'simpleValue' => 'circle',
        'description' => 'A circle is a perfectly round geometric figure, every point on the circle is equidistant from the center',
        'id' => 1,
        'label' => 'Round Circle',
        'metadata' => [,
            'color' => 'red',
            'sides' => 0,
            'type' => 'curved'
        ]
    ],
    'BoxSquare' => [
        'simpleValue' => 'square',
        'description' => 'A square is a regular quadrilateral',
        'id' => 2,
        'label' => 'Perfect Square',
        'metadata' => [
            'color' => 'blue',
            'sides' => 4,
            'type' => 'polygon',
            'regular' => true
        ]
    ],
    'RightTriangle' => [
        'simpleValue' => 'triangle',
        'description' => 'A triangle is a polygon with three edges and three vertices',
        'id' => 3,
        'label' => 'Right-Triangle',
        'metadata' => [,
            'color' => 'green',
            'sides' => 3,
            'type' => 'polygon'
        ]
    ],
    ...
]

$attributes = BackedEnumShape::attributes(['description', 'label']);

// Result:
[
    'RoundCircle' => [
        'description' => 'A circle is a perfectly round geometric figure, every point on the circle is equidistant from the center',
        'label' => 'Round Circle'
    ],
    'BoxSquare' => [
        'description' => 'A square is a regular quadrilateral',
        'label' => 'Perfect Square'
    ],
    'RightTriangle' => [
        'description' => 'A triangle is a polygon with three edges and three vertices',
        'label' => 'Right-Triangle'
    ],
    ...
]

$description = BackedEnumShape::description('RoundCircle');

// Result:
'A square is a regular quadrilateral, all sides have equal length and all angles are 90 degrees'

$descriptions = BackedEnumShape::descriptions();

// Result:
[
    'A circle is a perfectly round geometric figure, every point on the circle is equidistant from the center',
    'A square is a regular quadrilateral, all sides have equal length and all angles are 90 degrees',
    'A triangle is a polygon with three edges and three vertices',
    'A star is a self-intersecting, equilateral polygon',
    'A rectangle is a quadrilateral with four right angles'
]

$descriptions = BackedEnumShape::descriptions('name');

// Result:
[
    'RoundCircle' => 'A circle is a perfectly round geometric figure, every point on the circle is equidistant from the center',
    'BoxSquare' => 'A square is a regular quadrilateral',
    ...
]


$id = BackedEnumShape::id('PointedStar');

// Result:
4

$ids = BackedEnumShape::ids();

// Result:
[1, 2, 3, 4, 5]

$ids = BackedEnumShape::ids('value');

// Result:
['circle' => 1, 'square' => 2, 'triangle' => 3, 'star' => 4, 'rectangle' => 5]

$label = BackedEnumShape::PolygonRectangle->label();

// Result:
'Poly-Rectangle'

$labels = BackedEnumShape::labels();

// Result:
['Round Circle', 'Perfect Square', 'Right-Triangle', '5 Point Star', 'Poly-Rectangle']

$allMetadata = BackedEnumShape::metadata('BoxSquare');

// Result:
[
    'color' => 'blue',
    'sides' => 4,
    'type' => 'polygon',
    'regular' => true
]

$specific = BackedEnumShape::metadata('BoxSquare', 'color');

// Result:
'blue'

$filteredMetadata = BackedEnumShape::metadata('BoxSquare', ['color', 'type']);

// Result:
[
    'color' => 'blue',
    'type' => 'polygon'
]


$allMetadatum = BackedEnumShape::metadatum();
php
$specific = BackedEnumShape::metadatum('color');
php
// Result:
['red', 'blue', 'green', 'yellow', 'purple']
php
// Access metadata values directly
$color = BackedEnumShape::RoundCircle->meta()->color; // 'red'
$sides = BackedEnumShape::RoundCircle->meta()->sides; // 0
$type = BackedEnumShape::RoundCircle->meta()->type; // 'curved'

// Safely access non-existent properties
$unknown = BackedEnumShape::RoundCircle->meta()->nonexistent; // null

// Access nested JSON string metadata
$regular = BackedEnumShape::BoxSquare->meta()->regular; // true