All Projects → Cloudstek → php-enum

Cloudstek / php-enum

Licence: MIT license
Enumeration support for PHP

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to php-enum

Bscan
an asynchronous target enumeration tool
Stars: ✭ 207 (+1117.65%)
Mutual labels:  enumeration
wordlists
Aggregated wordlist pulled from commonly used tools for discovery, enumeration, fuzzing, and exploitation.
Stars: ✭ 94 (+452.94%)
Mutual labels:  enumeration
Platenum
The PHP enumeration type library
Stars: ✭ 34 (+100%)
Mutual labels:  enumeration
Activereign
A Network Enumeration and Attack Toolset for Windows Active Directory Environments.
Stars: ✭ 210 (+1135.29%)
Mutual labels:  enumeration
reosploit
A Tool that Finds, Enumerates, and Exploits Reolink Cameras.
Stars: ✭ 89 (+423.53%)
Mutual labels:  enumeration
Sudomy
Sudomy is a subdomain enumeration tool to collect subdomains and analyzing domains performing automated reconnaissance (recon) for bug hunting / pentesting
Stars: ✭ 1,572 (+9147.06%)
Mutual labels:  enumeration
Phpenums
🔩 Provides enumerations for PHP & frameworks integrations
Stars: ✭ 194 (+1041.18%)
Mutual labels:  enumeration
CEH
Exam Prep for the Ec-council Certified Ethical Hacker 312-50
Stars: ✭ 71 (+317.65%)
Mutual labels:  enumeration
ActiveDirectoryEnumeration
Enumerate AD through LDAP with a collection of helpfull scripts being bundled
Stars: ✭ 127 (+647.06%)
Mutual labels:  enumeration
Cheat-Sheet---Active-Directory
This cheat sheet contains common enumeration and attack methods for Windows Active Directory with the use of powershell.
Stars: ✭ 154 (+805.88%)
Mutual labels:  enumeration
Crosslinked
LinkedIn enumeration tool to extract valid employee names from an organization through search engine scraping
Stars: ✭ 223 (+1211.76%)
Mutual labels:  enumeration
Ntlmrecon
Enumerate information from NTLM authentication enabled web endpoints 🔎
Stars: ✭ 252 (+1382.35%)
Mutual labels:  enumeration
findcdn
findCDN is a tool created to help accurately identify what CDN a domain is using.
Stars: ✭ 64 (+276.47%)
Mutual labels:  enumeration
Dirstalk
Modern alternative to dirbuster/dirb
Stars: ✭ 210 (+1135.29%)
Mutual labels:  enumeration
roboxtractor
Extract endpoints marked as disallow in robots files to generate wordlists.
Stars: ✭ 40 (+135.29%)
Mutual labels:  enumeration
Fdsploit
File Inclusion & Directory Traversal fuzzing, enumeration & exploitation tool.
Stars: ✭ 199 (+1070.59%)
Mutual labels:  enumeration
enum-php
Enumeration implementation for PHP
Stars: ✭ 45 (+164.71%)
Mutual labels:  enumeration
Blowhole
Docker auditing and enumeration script.
Stars: ✭ 21 (+23.53%)
Mutual labels:  enumeration
Prox5
🧮 SOCKS5/4/4a 🌾 validating proxy pool and upstream SOCKS5 server for 🤽 LOLXDsoRANDum connections 🎋
Stars: ✭ 39 (+129.41%)
Mutual labels:  enumeration
php-enumeration
Implementation of enumeration classes in PHP. The better alternative for enums
Stars: ✭ 54 (+217.65%)
Mutual labels:  enumeration

PHP Enum

Enumeration support for PHP.

Build Status Coverage Status GitHub tag (latest SemVer) Downloads GitHub GitHub stars

This package adds support for enumerations to PHP, which are unfortunately not supported natively.

⚠️ Since PHP 8.1 there is finally native support for enums in PHP. Please consider upgrading to PHP 8.1+ and migrating away from this package if you require enums in your application.

Using a simple class with constants alone doesn't allow you to use type hints meaning you still have to do extensive checks whether the value is expected. This package allows you to define enumerations the same way but allows for type hinting for example your method parameter. This way you can always be sure it holds a concrete set of members and values.

Requirements

  • PHP 7.1+
  • Composer*

* Installation without composer is possible as the package consists of a single class, but is obviously not recommended.

Installation

Install the package through composer:

composer require cloudstek/php-enum

Usage

Definition

The Cloudstek\Enum\Enum base class takes care of all the work behind the scenes so all you have to do is extend your enum class from that and define your members using either properties, constants, methods or a mix of those.

Take for example this TaskStatus enum with three members: TODO, IN_PROGRESS and DONE. Each has a string value in this example but you're free to assign any kind of value you like.

use Cloudstek\Enum\Enum;

/**
 * @method static self TODO()
 * @method static self IN_PROGRESS()
 * @method static self DONE()
 */
class TaskStatus extends Enum
{
    private const TODO = 'todo';
    private const IN_PROGRESS = 'in_progress';
    private const DONE = 'done';
}

The doctype is only required for autocompletion in IDEs, not for the enum to function.

Make sure you define your members as either private or protected to avoid confusion leading to direct access to a member's value instead of an instance, causing exceptions when your code expects an instance and not the value (such as the example below).

TaskStatus::TODO !== TaskStatus::TODO()
class Task
{
    /** @var TaskStatus */
    private $status;

    /**
     * Set status
     *
     * @param TaskStatus $status
     */
    public function setStatus(TaskStatus $status)
    {
        $this->status = $status;
    }

    // ..
}

Or if you need to be more flexible, the get method will intelligently return the member by name or if an object is given, check that it's the correct type.

class Task
{
    /** @var TaskStatus */
    private $status;

    /**
     * Set status
     *
     * @param TaskStatus|string $status
     * 
     * @throws \UnexpectedValueException On unknown status.
     */
    public function setStatus($status)
    {
        $this->status = TaskStatus::get($status);
    }

    // ..
}

To read more about ways to define your members and how to name them, please see docs/definition.md.

Comparison

With enums you're always dealing with a single instance per member therefore you can compare them directly.

// Compare by instance
TaskStatus::TODO() === TaskStatus::TODO();                 // true
TaskStatus::TODO() === TaskStatus::get('todo');            // true
TaskStatus::get('TODO') === TaskStatus::get('todo');       // true
TaskStatus::TODO() === TaskStatus::get(TaskStatus::TODO()) // true

TaskStatus::TODO() === TaskStatus::DONE();                 // false
TaskStatus::TODO() === TaskStatus::get('done');            // false

// Compare by value
(string) TaskStatus::TODO() === 'todo';                    // true
TaskStatus::TODO()->getValue() === 'todo';                 // true

Inheritance

You should always define your enums as final classes to prevent other classes from inheriting from it. If you want other classes inheriting it, consider making it abstract and write final concrete classes that inherit from it.

Without making it final, your code could accept inherited enums when all you expected was the base class. This could lead to nasty bugs.

For example consider these enums:

use Cloudstek\Enum\Enum;

class FooEnum extends Enum
{
    private const FOO = 'foo';
}

class BarEnum extends FooEnum
{
    private const BAR = 'bar';
}

Without making FooEnum final, your code could unintentionally accept BarEnum as well even though it is expecting FooEnum.

class Foo
{
    public function doSomething(FooEnum $foo)
    {
        // Do something...
    }
}

$foo = new Foo();
$foo->doSomething(FooEnum::FOO()); // Allowed and OK, we were expecting FooEnum
$foo->doSomething(BarEnum::BAR()); // Allowed but not OK, we got BarEnum!

To prevent this and to make sure we always get FooEnum we should mark it final. Which doesn't mean it can't inherit anything else.

use Cloudstek\Enum\Enum;

abstract class BaseEnum extends Enum
{
    private const HELLO = 'world';
}

final class FooEnum extends BaseEnum
{
    private const FOO = 'foo';
}

final class BarEnum extends BaseEnum
{
    private const BAR = 'bar';
}

Now we're sure we only get instances of FooEnum.

class Foo
{
    public function doSomething(FooEnum $foo)
    {
      // Do something...
    }
}

$foo = new Foo();
$foo->doSomething(FooEnum::FOO()); // Allowed and OK, we were expecting FooEnum
$foo->doSomething(BarEnum::BAR()); // Fatal error

But in case we really don't care, as long as its base type is BaseEnum, we have to change the parameter type to BaseEnum explicitly like so:

class Foo
{
    public function doSomething(BaseEnum $foo)
    {
      // Do something...
    }
}

$foo = new Foo();
$foo->doSomething(FooEnum::FOO()); // OK
$foo->doSomething(BarEnum::BAR()); // OK

Storing data

If you store data containing an enum and you want to convert it back into an enum later, make sure to store the member name using getName() instead of storing its value. If you only care about the value, just store the value using getValue() or by casting it to a string (if possible).

// Update task
$status = TaskStatus::TODO();

$db->update($task, [
    'status' => $status->getName() // 'status' => 'todo'
]);
// Fetch task
$taskRow = $db->tasks->fetchOne(13); // [..., 'status' => 'todo', ...]

$task = new Task();
// ..
$task->setStatus(TaskStatus::get($taskRow['status']));

// or if you call TaskStatus::get() in Task::setStatus()
$task->setStatus($taskRow['status']);

Support

You can support this project by contributing to open issues, submitting pull requests, giving this project a or telling your friends about it.

If you have any ideas or issues, please open up an issue!

Related projects

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].