PrimitiveIdentifier.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-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: PrimitiveIdentifier.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     class PrimitiveIdentifier extends IdentifiablePrimitive
00017     {
00018         private $info = null;
00019         
00020         private $methodName = 'getById';
00021         
00026         public function of($class)
00027         {
00028             $className = $this->guessClassName($class);
00029             
00030             Assert::isTrue(
00031                 class_exists($className, true),
00032                 "knows nothing about '{$className}' class"
00033             );
00034             
00035             $this->info = new ReflectionClass($className);
00036             
00037             Assert::isTrue(
00038                 $this->info->implementsInterface('DAOConnected'),
00039                 "class '{$className}' must implement DAOConnected interface"
00040             );
00041             
00042             $this->className = $className;
00043             
00044             return $this;
00045         }
00046         
00050         public function dao()
00051         {
00052             Assert::isNotNull(
00053                 $this->className,
00054                 'specify class name first of all'               
00055             );
00056             
00057             return call_user_func(array($this->className, 'dao'));
00058         }
00059         
00063         public function setMethodName($methodName)
00064         {
00065             if (strpos($methodName, '::') === false) {
00066                 $dao = $this->dao();
00067                 
00068                 Assert::isTrue(
00069                     method_exists($dao, $methodName),
00070                     "knows nothing about '".get_class($dao)
00071                     ."::{$methodName}' method"
00072                 );
00073             } else
00074                 ClassUtils::checkStaticMethod($methodName);
00075             
00076             $this->methodName = $methodName;
00077             
00078             return $this;
00079         }
00080         
00081         public function importValue($value)
00082         {
00083             if ($value instanceof Identifiable) {
00084                 Assert::isTrue(
00085                     ClassUtils::isInstanceOf($value, $this->className)
00086                 );
00087                 
00088                 return
00089                     $this->import(
00090                         array($this->getName() => $value->getId())
00091                     );
00092             } elseif ($value) {
00093                 Assert::isInteger($value);
00094                 
00095                 return $this->import(array($this->getName() => $value));
00096             }
00097             
00098             return parent::importValue(null);
00099         }
00100         
00101         public function import($scope)
00102         {
00103             if (!$this->className)
00104                 throw new WrongStateException(
00105                     "no class defined for PrimitiveIdentifier '{$this->name}'"
00106                 );
00107             
00108             $className = $this->className;
00109             
00110             if (
00111                 isset($scope[$this->name])
00112                 && $scope[$this->name] instanceof $className
00113             ) {
00114                 $value = $scope[$this->name];
00115                 
00116                 $this->raw = $value->getId();
00117                 $this->setValue($value);
00118                 
00119                 return $this->imported = true;
00120             }
00121             
00122             $result = parent::import($scope);
00123             
00124             if ($result === true) {
00125                 try {
00126                     $result =
00127                         (strpos($this->methodName, '::') === false)
00128                             ? $this->dao()->{$this->methodName}($this->value)
00129                             : ClassUtils::callStaticMethod(
00130                                 $this->methodName, $this->value
00131                             );
00132                     
00133                     if (!$result || !($result instanceof $className)) {
00134                         $this->value = null;
00135                         return false;
00136                     }
00137                     
00138                     $this->value = $result;
00139                     
00140                 } catch (ObjectNotFoundException $e) {
00141                     $this->value = null;
00142                     return false;
00143                 }
00144                 
00145                 return true;
00146             }
00147             
00148             return $result;
00149         }
00150     }
00151 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4