<?php
namespace App\Model\Enums;

use Exception;
use ReflectionClass;

abstract class BaseEnum
{
    protected $value;

    /**
     * BaseEnum constructor.
     * @param $value
     * @throws InvalidEnumValue
     */
    public function __construct($value)
    {
        if(!static::isValidValue($value)) {
            throw new InvalidEnumValue("$value is not valid value.");
        }

        $this->value = $value;
    }

    protected static function isValidValue($value) {
        $reflection = new ReflectionClass(static::class);

        return in_array($value, $reflection->getStaticProperties());
    }

    public function __toString()
    {
        return $this->value;
    }
}

class InvalidEnumValue extends Exception {}