jameshalsall/constant-resolver

Provides functionality for mapping class constant values back to their semantic names

2.0.0 2013-07-31 13:14 UTC

This package is not auto-updated.

Last update: 2024-11-18 16:15:38 UTC


README

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'
 * );
 */