PrimitiveEnumeration.class.php

Go to the documentation of this file.
00001 <?php
00002 /*****************************************************************************
00003  *   Copyright (C) 2006-2008 by Ivan Y. Khvostishkov, 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 
00015     class PrimitiveEnumeration extends IdentifiablePrimitive
00016     {
00017         public function getList()
00018         {
00019             if ($this->value)
00020                 return $this->value->getObjectList();
00021             elseif ($this->default)
00022                 return $this->default->getObjectList();
00023             else {
00024                 $object = new $this->className(
00025                     call_user_func(array($this->className, 'getAnyId'))
00026                 );
00027                 
00028                 return $object->getObjectList();
00029             }
00030             
00031             Assert::isUnreachable();
00032         }
00033 
00038         public function of($class)
00039         {
00040             $className = $this->guessClassName($class);
00041             
00042             Assert::classExists($className);
00043             
00044             Assert::isInstance($className, 'Enumeration');
00045             
00046             $this->className = $className;
00047             
00048             return $this;
00049         }
00050         
00051         public function importValue(/* Identifiable */ $value)
00052         {
00053             if ($value)
00054                 Assert::isEqual(get_class($value), $this->className);
00055             else
00056                 return parent::importValue(null);
00057             
00058             return $this->import(array($this->getName() => $value->getId()));
00059         }
00060         
00061         public function import($scope)
00062         {
00063             if (!$this->className)
00064                 throw new WrongStateException(
00065                     "no class defined for PrimitiveEnumeration '{$this->name}'"
00066                 );
00067             
00068             $result = parent::import($scope);
00069             
00070             if ($result === true) {
00071                 try {
00072                     $this->value = new $this->className($this->value);
00073                 } catch (MissingElementException $e) {
00074                     $this->value = null;
00075                     
00076                     return false;
00077                 }
00078                 
00079                 return true;
00080             }
00081             
00082             return $result;
00083         }
00084     }
00085 ?>