Download the PHP package archtechx/enums without Composer

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

Enums

A collection of enum helpers for PHP.

You can read more about the original idea on Twitter.

Installation

PHP 8.1+ is required.

Usage

InvokableCases

This helper lets you get the value of a backed enum, or the name of a pure enum, by "invoking" it — either statically (MyEnum::FOO() instead of MyEnum::FOO), or as an instance ($enum()).

That way, you can use enums as array keys:

Or access the underlying primitives for any other use cases:

The main point: this is all without having to append ->value to everything.

This approach also has decent IDE support. You get autosuggestions while typing, and then you just append ():

Apply the trait on your enum

Use static calls to get the primitive value

Invoke instances to get the primitive value

Names

This helper returns a list of case names in the enum.

Apply the trait on your enum

Use the names() method

Values

This helper returns a list of case values for backed enums, or a list of case names for pure enums (making this functionally equivalent to ::names() for pure Enums)

Apply the trait on your enum

Use the values() method

Options

This helper returns an associative array of case names and values for backed enums, or a list of names for pure enums (making this functionally equivalent to ::names() for pure Enums).

Apply the trait on your enum

Use the options() method

stringOptions()

The trait also adds the stringOptions() method that can be used for generating convenient string representations of your enum options:

For pure enums (non-backed), the name is used in place of $value (meaning that both $name and $value are the same).

Both arguments for this method are optional, the glue defaults to \n and the callback defaults to generating HTML <option> tags:

From

This helper adds from() and tryFrom() to pure enums, and adds fromName() and tryFromName() to all enums.

Important Notes:

Apply the trait on your enum

Use the from() method

Use the tryFrom() method

Use the fromName() method

Use the tryFromName() method

Metadata

This trait lets you add metadata to enum cases.

Apply the trait on your enum

Explanation:

Access the metadata

Creating meta properties

Each meta property (= attribute used on a case) needs to exist as a class.

Inside the class, you can customize a few things. For instance, you may want to use a different method name than the one derived from the class name (Description becomes description() by default). To do that, override the method() method on the meta property:

With the code above, the description of a case will be accessible as TaskStatus::INCOMPLETE->note().

Another thing you can customize is the passed value. For instance, to wrap a color name like text-{$color}-500, you'd add the following transform() method:

And now the returned color will be correctly transformed:

Use the fromMeta() method

Use the tryFromMeta() method

Recommendation: use annotations and traits

If you'd like to add better IDE support for the metadata getter methods, you can use @method annotations:

And if you're using the same meta property in multiple enums, you can create a dedicated trait that includes this @method annotation.

Comparable

This trait lets you compare enums using is(), isNot(), in() and notIn().

Apply the trait on your enum

Use the is() method

Use the isNot() method

Use the in() method

Use the notIn() method

PHPStan

To assist PHPStan when using invokable cases, you can include the PHPStan extensions into your own phpstan.neon file:

Development

Run all checks locally:

Code style will be automatically fixed by php-cs-fixer.


All versions of enums with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
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 archtechx/enums contains the following files

Loading the files please wait ....