Download the PHP package greg0ire/enum without Composer

On this page you can find all versions of the php package greg0ire/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?

Informations about the package enum

Enums

This package holds a simple class that may be used as an ancestor for your enum classes.

Build Status

Installation

composer require greg0ire/enum

Usage

Basic usage

Extend the Greg0ire\Enum\AbstractEnum, define your enum key values as constants, and Bob's your uncle. You can make the class abstract or final, as you see fit.

Then, you may use the DaysOfWeek class for input validation:

Both methods have an assert* counterpart that will throw a Greg0ire\Enum\Exception\InvalidEnumValue exception:

Additionally, you may get all the constants in your class as a hash:

You may also get all the keys in your class as an array:

Or the key with the enum class prefix:

If you would like to get the keys from a value:

If you have many keys with the same value you will get an array, and a value otherwise.

Advanced usage

If you need to get the constants from a class you cannot modify, or from an interface, or even from several classes / interfaces, you may override AbstractEnum::getEnumTypes().

For example, if you have the following class and interface :

You can get all three constants by creating this Enum :

Alternatively, you can specify a prefix for each type to avoid getting FQCNs in the hash keys.

Get label from a service

If you want to get the constant label behind an enum value, you can instantiate the GetLabel class and invoke it.

To enable translation, require the symfony/translation component and pass a Symfony\Contracts\Translation\TranslationInterface instance on the GetLabel constructor

If you're using Symfony, alias the service and simply inject it. If translations are enabled, the TranslatorInterface will be automatically injected.

Integration with other libraries

greg0ire/enum integrates with other libraries. The list is available in the suggest section of the Composer dependency manifest.

Symfony validator

This package provides a "ready to use" symfony validator. You have to require the symfony/validator package to get it working.

Another example with annotations:

Note: You will have to get doctrine/annotations and doctrine/cache packages to get it working.

Symfony form

This package provides a "ready to use" symfony form type. You have to require the symfony/form package to get it working.

Twig extension

This package comes with an EnumExtension Twig class. It contains a filter and some functions. You have to require the twig/twig package to get it working.

Filter

The enum_label filter will try to return the constant label corresponding to the given value.

This filter relies on the Greg0ire\Enum\Bridge\Symfony\Translator\GetLabel service.

It will try to translate it if possible. To enable translation, require the symfony/translation component and pass a Symfony\Contracts\Translation\TranslationInterface instance on the GetLabel constructor. GetLabel instance will be injected on the EnumExtension constructor.

If translation is not available, you will have the default label with class prefixing.

Usage:

Functions

The 3 available twig functions are ports of some AbstractEnum methods that can be useful in a twig template:

The arguments are exactly the same except you have to specify the targeted class first (as enum_label filter).

Here is a concrete example with enum_get_constants function:

Twig extension as a service

On Symfony projects, the extension can be autoloaded. First, you have to require the symfony/framework-bundle and symfony/twig-bundle packages, or use Symfony fullstack.

Then, register the bundle in the kernel of your application:

That's all. You can now directly use the filter.

Contributing

see CONTRIBUTING.md

Credits

This is a shameless rip-off of this Stack Overflow answer, with one modification: the getConstants method has been made public so that it is available for building choice widgets, for instance. If you want to give credit to someone for this, give it to Brian Cline


All versions of enum with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1 || ^8.0
doctrine/inflector Version ^1.4 || ^2.0
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 greg0ire/enum contains the following files

Loading the files please wait ....