Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
87.50% covered (success)
87.50%
7 / 8
CRAP
96.30% covered (success)
96.30%
26 / 27
Enum
0.00% covered (danger)
0.00%
0 / 1
88.89% covered (success)
88.89%
8 / 9
13
96.30% covered (success)
96.30%
26 / 27
 __construct( $value = null )
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 getDefaultValueIfNeccessary( $value )
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 guardValidValue( $value )
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 guardValueIsScalar( $value )
0.00% covered (danger)
0.00%
0 / 1
3.14
75.00% covered (success)
75.00%
3 / 4
 guardValueIsInConstants( $value )
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 getDefaultValue()
100.00% covered (success)
100.00%
1 / 1
1  
 
 getValues()
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 __toString()
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 equals( $other_value )
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
/**
 * Enum type
 *
 * @author hwoltersdorf
 */
namespace hollodotme\Types;
/**
 * Class Enum
 *
 * @package hollodotme\Types
 */
abstract class Enum
{
    /** @var mixed */
    private $value;
    /**
     * @param null|mixed $value
     *
     * @throws \InvalidArgumentException
     */
    final public function __construct( $value = null )
    {
        $value = $this->getDefaultValueIfNeccessary( $value );
        $this->guardValidValue( $value );
        $this->value = $value;
    }
    /**
     * @param mixed|null $value
     *
     * @return mixed
     */
    private function getDefaultValueIfNeccessary( $value )
    {
        if ( is_null( $value ) )
        {
            $value = $this->getDefaultValue();
        }
        return $value;
    }
    /**
     * @param mixed $value
     *
     * @throws \InvalidArgumentException
     */
    private function guardValidValue( $value )
    {
        $this->guardValueIsScalar( $value );
        $this->guardValueIsInConstants( $value );
    }
    /**
     * @param mixed $value
     *
     * @throws \InvalidArgumentException
     */
    private function guardValueIsScalar( $value )
    {
        if ( !is_scalar( $value ) && !is_null( $value ) )
        {
            throw new \InvalidArgumentException( 'Value is not scalar or null: ' . gettype( $value ) );
        }
    }
    /**
     * @param mixed $value
     *
     * @throws \InvalidArgumentException
     */
    private function guardValueIsInConstants( $value )
    {
        if ( !in_array( $value, $this->getValues(), true ) )
        {
            throw new \InvalidArgumentException(
                $value . ' is not defined as class constant of enum ' . get_class( $this )
            );
        }
    }
    /**
     * @return mixed
     */
    abstract public function getDefaultValue();
    /**
     * @return array
     */
    final public function getValues()
    {
        $ref_class = new \ReflectionClass( $this );
        $ref_constants = $ref_class->getConstants();
        return array_values( $ref_constants );
    }
    /**
     * @return string
     */
    final public function __toString()
    {
        return strval( $this->value );
    }
    /**
     * @param mixed $other_value
     *
     * @return bool
     */
    final public function equals( $other_value )
    {
        return ($this->value === $other_value);
    }
}