00001 <?php 00002 /*************************************************************************** 00003 * Copyright (C) 2004-2007 by Konstantin V. Arkhipov * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU Lesser General Public License as * 00007 * published by the Free Software Foundation; either version 3 of the * 00008 * License, or (at your option) any later version. * 00009 * * 00010 ***************************************************************************/ 00011 /* $Id: Enumeration.class.php 4687 2007-12-09 18:57:18Z voxus $ */ 00012 00021 abstract class Enumeration extends NamedObject implements Serializable 00022 { 00023 protected $names = array(/* override me */); 00024 00025 final public function __construct($id) 00026 { 00027 $this->setId($id); 00028 } 00029 00031 00032 public function __sleep() 00033 { 00034 return array('id'); 00035 } 00036 00037 public function __wakeup() 00038 { 00039 $this->setId($this->id); 00040 } 00041 00042 public function serialize() 00043 { 00044 return (string) $this->id; 00045 } 00046 00047 public function unserialize($serialized) 00048 { 00049 $this->setId($serialized); 00050 } 00052 00053 public static function getList(Enumeration $enum) 00054 { 00055 return $enum->getObjectList(); 00056 } 00057 00062 public static function getAnyId() 00063 { 00064 return 1; 00065 } 00066 00068 public function getId() 00069 { 00070 return $this->id; 00071 } 00072 00073 public function getObjectList() 00074 { 00075 $list = array(); 00076 $names = $this->getNameList(); 00077 00078 foreach (array_keys($names) as $id) 00079 $list[] = new $this($id); 00080 00081 return $list; 00082 } 00083 00084 public function toString() 00085 { 00086 return $this->name; 00087 } 00088 00089 public function getNameList() 00090 { 00091 return $this->names; 00092 } 00093 00097 public function setId($id) 00098 { 00099 $names = $this->getNameList(); 00100 00101 if (isset($names[$id])) { 00102 $this->id = $id; 00103 $this->name = $names[$id]; 00104 } else 00105 throw new MissingElementException( 00106 'knows nothing about such id == '.$id 00107 ); 00108 00109 return $this; 00110 } 00111 } 00112 ?>