Skip to content
This repository has been archived by the owner on Nov 2, 2024. It is now read-only.
/ php-options Public archive

Provides simple Enums. Fully typed and comparable.

Notifications You must be signed in to change notification settings

HellPat/php-options

Repository files navigation

PHP-Options

Finite Options as objects in PHP.

Motivation

Option validity & Type-safety

When you use an integer or a string to change the featureset of your class/method you have to protect you from passing in wrong arguments.

e.g. (from Symfony /~https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/Event/KernelEvent.php)

/**
 * @param int $requestType The request type the kernel is currently processing; one of
 *                         HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST
 */
public function __construct(HttpKernelInterface $kernel, Request $request, ?int $requestType)
{
    $this->kernel = $kernel;
    $this->request = $request;
    $this->requestType = $requestType;
}

/**
 * Returns the request type the kernel is currently processing.
 *
 * @return int One of HttpKernelInterface::MASTER_REQUEST and
 *             HttpKernelInterface::SUB_REQUEST
 */
public function getRequestType()
{
    return $this->requestType;
}

The usage of constants make it more comfortable and typo safe for the developer. But everyone knows that one person that just passes in int = 3.

This should throw an Exception. But do you really want to care about that? Enum for the Rescue. Invalid Options are permitted at construction. It's not possible to make an invalid option.

And then the type-declaration enforces validity!

Here a more type-safe example.

/**
- * @param int $requestType The request type the kernel is currently processing; one of
- *                         HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST
 */
-public function __construct(HttpKernelInterface $kernel, Request $request, ?int $requestType)
+public function __construct(HttpKernelInterface $kernel, Request $request, RequestType $requestType)
{
    $this->kernel = $kernel;
    $this->request = $request;
    $this->requestType = $requestType;
}

/**
 * Returns the request type the kernel is currently processing.
- *
- * @return int One of HttpKernelInterface::MASTER_REQUEST and
- *             HttpKernelInterface::SUB_REQUEST
 */
+public function getRequestType(): RequestType
{
    return $this->requestType;
}

An other example using colors:

class Mixer {
    -static function mix(string $color1, string $color2): Color {}
    +static function mix(Color $color1, Color $color2): Color {}
}

-Mixer::mix('red', 'purple');
+Mixer::mix(Color::RED(), COLOR::PURPLE());

-Mixer::mix('yellowgreenlimebutter') // invalid strings where allowed before

Strict comparison

This implementation provides a Registry for your options and fetches from there. This is totally possible and convenient:

Color::RED() === Color::RED() // returns `true`

How to declare your Options

tbd.

Naming-Convention

tbd.

Comparison with other libraries

Other libraries have been released way earlier, are more stable and used. In fact I'd never think auf Enums in PHP without there existence. Kudos to all of you.

Inspirational posts

About

Provides simple Enums. Fully typed and comparable.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages