Skip to content
PHP Class that resolves class constant values back to their semantic names.
PHP
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
src/JamesHalsall/ConstantResolver
.gitignore
README.md
composer.json
phpunit.xml

README.md

constant-resolver

PHP Class that resolves class constant values back to their semantic names.

Installation

Composer

Add "jameshalsall/constant-resolver" to your composer.json require section.

Usage

Example class:

<?php

class SomeClass
{
    const MY_CONSTANT_NAME = 1;

    ...
}
<?php

use JamesHalsall\ConstantResolver;

$someClass = new SomeClass();
$resolver  = new ConstantResolver($someClass);

// returns 'SomeClass::MY_CONSTANT_NAME'
$constant = $resolver->resolve(1);

Example

<?php

$httpErrorCodes = new HttpErrorCodes();

$resolver = new ConstantResolver($enumerableClass);

// returns 'HttpErrorCodes::NOT_FOUND'
$resolver->resolve(404);

Same example with array return:

<?php

...

$resolver->setReturnType(ConstantResolver::RETURN_ARRAY);

/**
 * returns array(
 *     'NOT_FOUND' => 'HttpErrorCodes::NOT_FOUND'
 * );
 */
Something went wrong with that request. Please try again.