Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RecordTypes as Enum #1

Closed
uteq opened this issue Sep 22, 2016 · 0 comments
Closed

RecordTypes as Enum #1

uteq opened this issue Sep 22, 2016 · 0 comments

Comments

@uteq
Copy link
Contributor

uteq commented Sep 22, 2016

You might consider changing the RecordTypes to a (simulated) Enum datatype.
The Abstract class for this could be:

abstract class AbstractEnum
{
    private static $constCacheArray = NULL;

    // Prevent instance
    private function __construct() {}

    private static function getConstants() 
    {
        if (self::$constCacheArray == NULL) {
            self::$constCacheArray = [];
        }

        $calledClass = get_called_class();

        if (!array_key_exists($calledClass, self::$constCacheArray)) {
            $reflect = new ReflectionClass($calledClass);
            self::$constCacheArray[$calledClass] = $reflect->getConstants();
        }
        return self::$constCacheArray[$calledClass];
    }

    public static function isValidKey($name, $strict = false) 
    {
        $constants = self::getConstants();

        if ($strict) {
            return array_key_exists($name, $constants);
        }

        $keys = array_map('strtolower', array_keys($constants));

        return in_array(strtolower($name), $keys);
    }

    public static function isValidValue($value, $strict = true) 
    {
        $values = array_values(self::getConstants());

        return in_array($value, $values, $strict);
    }
}

The RecordTypes class should than extend the AbstractEnum

class RecordTypes extends AbstractEnum
{
    protected static $namespace = __NAMESPACE__.'\\RecordType\\';

    const BH10 = 'BH10';
    const BT10 = 'BT10';
    const FH10 = 'FH10';
    const FH20 = 'FH20';
    const FH30 = 'FH30';
    const FH40 = 'FH40';
    const FH50 = 'FH50';
    const FH60 = 'FH60';
    const FH70 = 'FH70';
    const FH80 = 'FH80';
    const FR10 = 'FR10';
    const FR20 = 'FR20';
    const FH3 = 'FH3';

    public static function get($key = '')
    {
        if($key) {
            if (!is_string($key)) {
                throw new \InvalidArgumentException(sprintf(
                    '%s: expects a string argument; received "%s"',
                    __METHOD__,
                    (is_object($key) ? get_class($key) : gettype($key))
                ));
            }

            if(isset(self::$key)){
                $result = self::$namespace.self::$key;
            }
        }

        return $result;
    }
}

Then use it like this:

switch ($formattedLine['type']) {
    case RecordTypes::FR10:
        // Do something
    }
}

Nothing changes in the implementation for for example the AbstractReader but the code becomes more readable.

if(!$recordType = RecordTypes::get($data[0])){
    throw new RecordTypeException(
        sprintf(
            'ICF record type "%s" does not exist; Allowed ICF record types are %s',
            $recordType,
            implode(', ', array_keys(RecordTypes::get()))
        )
    );
}

If you would like me to create a pull request for this I will.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant