Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Enum Laravel Package

commerceguys/enum

View on GitHub
Deep Wiki
Context7

enum

Build Status

A PHP 5.4+ enumeration library.

Class constants are frequently used to denote sets of allowed values. By grouping them in an enumeration class, we gain the ability to add helper methods, list all possible values and validate values against them.

A commerceguys/addressing example:

namespace CommerceGuys\Addressing\Enum;

use CommerceGuys\Enum\AbstractEnum;

/**
 * Enumerates available locality types.
 */
final class LocalityType extends AbstractEnum
{
    const CITY = 'city';
    const DISTRICT = 'district';

    // We can provide a getDefault() method here, or anything else.
}

LocalityType::getAll(); // ['CITY' => 'city', 'DISTRICT' => 'district']
LocalityType::getKey('city'); // 'CITY'
LocalityType::exists('city'); // true
LocalityType::assertExists('invalid value'); // InvalidArgumentException
LocalityType::assertAllExist(['district', 'invalid value']); // InvalidArgumentException

Meanwhile, on the AddressFormat:

// The AddressFormatInterface is now free of LOCALITY_TYPE_ constants.
class AdressFormat implements AddressFormatInterface
{
    public function setLocalityType($localityType)
    {
        LocalityType::assertExists($localityType);
        $this->localityType = $localityType;
    }
}

The reason why this library was made instead of reusing myclabs/php-enum was that we didn't want to allow enumerations to be instantiated.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
aimeos/prisma
besmartand-pro/php-quality-config
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor
spatie/laravel-javascript-views