Download the PHP package tlr/enum without Composer

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

TLR \ PHP Enum Library

Build Status

Deprecated

This package is deprecated as PHP 8.1 will support a native / primitive enum type, and solves 90% of the problems this package was meant to solve.

The source code will stay up here indefinitely, for anyone already using this library.

Introduction

A raw PHP Enum / Flags library. Heavily based on myclabs/php-enum (I did not fork, as I made some subtle changes to the core classes).

With support for Laravel (and Nova).

Key features:

Key features for Laravel:

Installation

If you are on Laravel with package auto-detection, good for you, you're all set up. If not, add Tlr\Phpnum\Laravel\EnumServiceProvider to your loaded service providers.

What is an enum?

An enumaration value - it is a value that represents one possible in a concrete list of values. It allows you to define a list of specific values, and be confident that your enum conforms to that list.

Consider the following function:

Any string can be passed in to this function - 'article', 'recipe', 'monkeys', and while it may be easy to handle many possible types, you will always have to handle any types that don't exist.

An enum would allow you to type hint a value from a pre-determined list of values that are definied in your code, so you can be sure that you are dealing with a discrete (limited) choice of values (types, in this case). For example...

Basic Usage (Enums)

Personally, I keep an application namespace specifically for all of enums in a project, and for the sake of these examples, I will be using the App\Values as the namespace for enums.

Full Docs (Enum)

Setup

You can declare the values of the enum in three ways:

As above - using const declarations when declaring a class. This should probably be considered the "default" way of declaring an enum.

In an $enum static variable.

Override the generateEnums method (the default implementation defines the above two ways of declaring the values). This could be used to load the values from a database, etc. and should be used with care. This method will only be called once, as the result of it is cached.

Instantiating an Enum

The two main ways of instantiating an enum are:

You can use any of the declared enum names to statically instantiate an enum instance. This is useful when manually declaring a value - perhaps to save to the database.

You can provide the value of the enum to the constructor. This is useful when loading values from a database, or getting input from a user.

You can also get a list of all instantiated enums with the all static method:

Getting information on an enum

You can get various bits of information out of an enum using helper methods:

The friendlyName method can be used to display values to a user. It will attempt to make the key name into words, and Title Case them. You can override this in two ways:

Override the friendlifier static method. It will be passed the key name of the enum, and should return its friendly value.

Override the friendlyNames static method, which should return an array/map with the friendly names on the left, and the ordinary values on the right.

Comparing Enums

You can compare any enum against another with the is or equals methods (they are the same, both are included in case they make more syntactical / English sense when writing code)

This comparison takes into account the class name of the enum, as well as its value, so multiple different enums will not be able to be cross-compared to the original enum.

Flag Enums

Basic Explanation

Flags (or bit masks) are a common feature of many programming langauges - PHP uses some in its standard library under the guise of some bitmask constants - see the second argument to json_encode for an example.

They are a kind of enum, where each value is an increasing power of 2 (1, 2, 4, 8, 16, etc.). While a flag is technically just an integer value, if you represent those integers in binary, you get something interesting:

If you assign a meaning to each bit position (starting from the right), then you have a way of using an integer value to store a set of switched, or flags. You can chain these together - for example, the integer 5 is 00101 - or 1 and 4 combined; the integer 31 is all of them combined; the integer 0 is none of them.

This may seem somewhat complicated at first, but once you have a feel for this, flags can be a very powerful tool, allowing you to pass a full set of option switches in a single argument, without losing any of the contextual meaning of the switches. In the above example, we may assign some names to the values (here, using the json_encode example - correct values at the time of writing).

You can pass the first three to json_encode with the | operator like so:

You can check if the flag matches a possible flag value with the & operator:

Of course you can check if it matches an exact set with ===:

There is much more you can do with th ebitwise operators and flags, but these are the basics.

Caveat

Once a flag value has been defined, it can never change in a future version of an application. If PHP decided to remove JSON_HEX_AMP and add a new possible switch, JSON_HEX_SPACE for example, they would have to add JSON_HEX_SPACE to the end of the list of values, and simply not allow the value for JSON_HEX_AMP, or ignore it if it is passed (ie. it would deprecate the value 2).

Flags should be used for core things that are not often changed.

Flags and Enums

The json_encode options are just consts set to certain values (powers of 2, as described above). The advantage of using flags with a package like this one, is that the flags, values, and validation, become encapsulated in an object, making definition, referencing, validation, and comparison easier.

The Flag class and Enum class used above share the same base class, so almost all of the same things from above apply, with a few differences for defining, and a few extra methods for comparing.

Defining Flags

To use a very simple permissions set as an example.

Using masks of flags

The following are all the same:

If we could only set the user to be able to do ONE of those things, though, it would be a bit limiting. We can assign multiple flags to one value like so:

Comparing Flags

In addition to the enum comparison methods (like $enum->is($other)), there are some specific to flags.

// @todo - comparing

// @todo - reading

Laravel

// @todo

Validation Rule

// @todo

Nova Field

// @todo


All versions of enum with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0.0
ext-json Version *
danielstjules/stringy Version ^3.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 tlr/enum contains the following files

Loading the files please wait ....