# Enumeration.class.php

Go to the documentation of this file.

    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
    00020     abstract class Enumeration extends NamedObject implements Serializable
    00021     {
    00022         protected $names = array(/* override me */);
    00023
    00024         final public function __construct($id)
    00025         {
    00026             $this->setId($id);
    00027         }
    00028
    00030
    00031         public function serialize()
    00032         {
    00033             return (string) $this->id;
    00034         }
    00035
    00036         public function unserialize($serialized)
    00037         {
    00038             $this->setId($serialized);
    00039         }
    00041
    00042         public static function getList(Enumeration $enum)
    00043         {
    00044             return $enum->getObjectList();
    00045         }
    00046
    00051         public static function getAnyId()
    00052         {
    00053             return 1;
    00054         }
    00055
    00057         public function getId()
    00058         {
    00059             return $this->id;
    00060         }
    00061
    00062         public function getObjectList()
    00063         {
    00064             $list = array();
    00065             $names = $this->getNameList();
    00066
    00067             foreach (array_keys($names) as $id)
    00068                 $list[] = new $this($id);
    00069
    00070             return $list;
    00071         }
    00072
    00073         public function toString()
    00074         {
    00075             return $this->name;
    00076         }
    00077
    00078         public function getNameList()
    00079         {
    00080             return $this->names;
    00081         }
    00082
    00086         public function setId($id)
    00087         {
    00088             $names = $this->getNameList();
    00089
    00090             if (isset($names[$id])) {
    00091                 $this->id = $id;
    00092                 $this->name = $names[$id];
    00093             } else
    00094                 throw new MissingElementException(
    00095                     'knows nothing about such id == '.$id
    00096                 );
    00097
    00098             return $this;
    00099         }
    00100     }
    00101 ?>