| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the miBadger package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author Michael Webbers <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @version 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace miBadger\Enum; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * The enum class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @see http://tools.ietf.org/html/rfc5424 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @since 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class Enum | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	/** @var int|double|string|boolean The value. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	private $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 * Construct a enum with the given value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * @param int|double|string|boolean $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * @throws \UnexpectedValueException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 	public function __construct($value) | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 		if (!in_array($value, static::toArray())) { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 			throw new \UnexpectedValueException($value . ' is not a valid enum value.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 		$this->value = $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * Returns the string representation of the enum object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 * @return string the string representation of the enum object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	public function __toString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		return $this->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * Returns the ordinal. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 * @return int the ordinal. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	public function getOrdinal() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$ordinal = array_search($this->value, array_values(static::toArray())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		return $ordinal !== false ? $ordinal : -1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * Returns the name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * @return string the name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	public function getName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$name = array_search($this->value, static::toArray()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		return $name !== false ? $name : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * Returns the value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * @return mixed the value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	public function getValue() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		return $this->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 * Returns the enum objects. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 * @return array<string,integer|double|string|boolean> the enum objects. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	public static function toArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		return (new \ReflectionClass(\get_called_class()))->getConstants(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * Returns the enum object with the specified name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 * @return Enum the enum object with the specified name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * @throws \UnexpectedValueException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	public static function valueOf($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		if (!defined('static::' . $name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			throw new \UnexpectedValueException($name . ' is not a valid enum name.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		return new static(constant('static::' . $name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  |