jameshalsall/constant-resolver

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

Installs: 225

Dependents: 0

Stars: 5

Watchers: 1

Forks: 1

Open Issues: 1

Language: PHP

2.0.0 2013-07-31 13:14 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'
 * );
 */