IdentifiablePrimitive.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2008 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 
00015     abstract class IdentifiablePrimitive
00016         extends PrimitiveInteger // parent class doesn't really matter here
00017     {
00018         protected $className = null;
00019         
00024         protected $scalar = false;
00025         
00026         abstract public function of($className);
00027         
00031         public function setScalar($orly = false)
00032         {
00033             $this->scalar = ($orly === true);
00034             
00035             return $this;
00036         }
00037         
00042         public function setValue($value)
00043         {
00044             $className = $this->className;
00045             
00046             Assert::isNotNull($this->className);
00047             
00048             Assert::isTrue($value instanceof $className);
00049             
00050             return parent::setValue($value);
00051         }
00052         
00053         protected static function guessClassName($class)
00054         {
00055             if (is_string($class))
00056                 return $class;
00057             elseif (is_object($class)) {
00058                 if ($class instanceof Identifiable)
00059                     return get_class($class);
00060                 elseif ($class instanceof GenericDAO)
00061                     return $class->getObjectName();
00062             }
00063             
00064             throw new WrongArgumentException('strange class given - '.$class);
00065         }
00066         
00067         public function exportValue()
00068         {
00069             if (!$this->value)
00070                 return null;
00071             
00072             return $this->value->getId();
00073         }
00074         
00075         /* void */ protected function checkNumber($number)
00076         {
00077             if ($this->scalar)
00078                 Assert::isScalar($number);
00079             else
00080                 Assert::isInteger($number);
00081         }
00082         
00083         protected function castNumber($number)
00084         {
00085             if (!$this->scalar && Assert::checkInteger($number))
00086                 return (int) $number;
00087             
00088             return $number;
00089         }
00090     }
00091 ?>