1: <?php
2:
3: /**
4: * Picon Framework
5: * http://code.google.com/p/picon-framework/
6: *
7: * Copyright (C) 2011-2012 Martin Cassidy <martin.cassidy@webquub.com>
8:
9: * Picon Framework is free software: you can redistribute it and/or modify
10: * it under the terms of the GNU General Public License as published by
11: * the Free Software Foundation, either version 3 of the License, or
12: * (at your option) any later version.
13:
14: * Picon Framework is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: * General Public License for more details.
18:
19: * You should have received a copy of the GNU General Public License
20: * along with Picon Framework. If not, see <http://www.gnu.org/licenses/>.
21: * */
22:
23: namespace picon;
24:
25: /**
26: * Based on SplEnum, this class allows enumerated types to be created.
27: *
28: * Extend this class to create your enums in the following way
29: *
30: * <code>
31: * class Fruit extends Enum
32: * {
33: * const APPLE = 1;
34: * const ORANGE = 2;
35: * const _DEFAULT = 1;
36: * }
37: * </code>
38: *
39: * _DEFAULT is optional and will be used if no paramater is passed to the constructor
40: *
41: * <code>
42: * echo new Fruit();
43: * echo new Fruit(Fruit::ORANGE);
44: * echo Fruit::valueOf("orange");
45: * </code>
46: *
47: * Will output:
48: * 1
49: * 2
50: * 2
51: *
52: * @author Martin Cassidy
53: * @package domain
54: */
55: abstract class Enum implements Identifiable, Equalable
56: {
57: private $value;
58: const DEFAULT_NAME = "_DEFAULT";
59:
60: /**
61: * Create a new enum
62: * @param Object $value The enum value
63: */
64: public function __construct($value = null)
65: {
66: $enum = new \ReflectionClass ($this);
67:
68: if($value==null)
69: {
70: if(!$enum->hasConstant(self::DEFAULT_NAME))
71: {
72: throw new \InvalidArgumentException("Cannot create enum as no value has been passed and no default is specified");
73: }
74: else
75: {
76: $value = $enum->getConstant(self::DEFAULT_NAME);
77: }
78: }
79:
80: foreach($enum->getConstants() as $name => $enumValue)
81: {
82: if($enumValue==$value)
83: {
84: $this->value = $value;
85: return;
86: }
87: }
88:
89: throw new \InvalidArgumentException("Unknown enum value ".$value);
90: }
91:
92: /**
93: * Create an enum for the given value
94: * @param String $obj A string of the value
95: * @return The enum for the value, or null if no value can be found
96: */
97: public static function valueOf($obj)
98: {
99: $enumClass = new \ReflectionClass (get_called_class());
100: foreach($enumClass->getConstants() as $enumName => $enumValue)
101: {
102: if(strtolower($enumValue)==strtolower($obj))
103: {
104: return new static($enumValue);
105: }
106: }
107: return null;
108: }
109:
110: /**
111: * Gets all the posible values for this enum
112: *
113: * @param Boolean include the default value, defaults to false
114: * @return Array of values for this enum
115: */
116: public static function values($includeDefault = false)
117: {
118: $enumClass = new \ReflectionClass (get_called_class());
119: $values = $enumClass->getConstants();
120: $processed = array();
121:
122: foreach($values as $index => $value)
123: {
124: if($index!="DEFAULT_NAME" && ($includeDefault || $index!=self::DEFAULT_NAME))
125: {
126: $processed[$index] = $value;
127: }
128: }
129:
130:
131: return $processed;
132: }
133:
134: /**
135: *
136: * @return A String representation of the enum
137: */
138: public function __toString()
139: {
140: return "".$this->value;
141: }
142:
143: /**
144: * Tests if two enums are equal
145: * @param Enum The enum to test against
146: * @return Boolean true if the enum's match, false otherwise. False if the passed object is not a valid enum
147: */
148: public function equals($enum)
149: {
150: if($enum instanceof Enum)
151: {
152: return $enum->value==$this->value;
153: }
154: return false;
155: }
156:
157: public static function getIdentifier()
158: {
159: return Identifier::forName(get_called_class());
160: }
161:
162: }
163:
164: ?>
165: