Download the PHP package spatie/enum without Composer

On this page you can find all versions of the php package spatie/enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?
spatie/enum
Rate from 1 - 5
Rated 1.59 based on 82 reviews

Informations about the package enum

PHP Enum

Latest Version on Packagist License Postcardware

PHP from Packagist Build Status Total Downloads

This package offers strongly typed enums in PHP. In this package, enums are always objects, never constant values on their own. This allows for proper static analysis and refactoring in IDEs.

Here's how enums are created with this package:

And this is how they are used:

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

Usage

This is how an enum can be defined.

This is how they are used:

Autocompletion Refactoring

Creating an enum from a value

When an enum value doesn't exist, you'll get a BadMethodCallException. If you would prefer not catching an exception, you can use:

When an enum value doesn't exist in this case, $status will be null.

The only time you want to construct an enum from a value is when unserializing them from eg. a database.

If you want to get the value of an enum to store it, you can do this:

Note that value is a read-only property, it cannot be changed.

Enum values

By default, the enum value is its method name. You can however override it, for example if you want to store enums as integers in a database, instead of using their method name.

An enum value doesn't have to be a string, as you can see in the example it can also be an int.

Note that you don't need to override all values. Rather, you only need to override the ones that you want to be different from the default.

If you have a logic that should be applied to all method names to get the value, like lowercase them, you can return a Closure.

Enum labels

Enums can be given a label, you can do this by overriding the labels method.

Note that you don't need to override all labels, the default label will be the enum's value.

If you have a logic that should be applied to all method names to get the label, like lowercase them, you can return a Closure as in the value example.

You can access an enum's label like so:

Note that label is a read-only property, it cannot be changed.

Comparing enums

Enums can be compared using the equals method:

You can pass several enums to the equals method, it will return true if the current enum equals one of the given values.

Phpunit Assertions

This package provides an abstract class Spatie\Enum\Phpunit\EnumAssertions with some basic/common assertions if you have to test enums.

Faker Provider

Possibly you are using faker and want to generate random enums. Because doing so with default faker is a lot of copy'n'paste we've got you covered with a faker provider Spatie\Enum\Faker\FakerEnumProvider.

Testing

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of enum with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
ext-json Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package spatie/enum contains the following files

Loading the files please wait ....