PropertyPath.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 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: PropertyPath.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class PropertyPath
00017     {
00018         private $root = null;
00019         private $path = null;
00020         
00021         private static $daos    = array();
00022         private static $protos  = array(); // zergs suck anyway ;-)
00023         
00024         private $properties = array();
00025         
00026         public function __construct($root, $path)
00027         {
00028             Assert::isString($path, 'non-string path given');
00029             
00030             if (is_object($root))
00031                 $className = get_class($root);
00032             else {
00033                 Assert::isTrue(
00034                     class_exists($root, true),
00035                     'inexistant class given'
00036                 );
00037                 $className = $root;
00038             }
00039             
00040             $this->root = $className;
00041             $this->path = $path;
00042             
00043             $this->fetchHelpers($className);
00044             
00045             $proto = self::$protos[$className];
00046             
00047             $path = explode('.', $path);
00048             
00049             for ($i = 0, $size = count($path); $i < $size; ++$i) {
00050                 $this->properties[$i]
00051                     = $property
00052                     = $proto->getPropertyByName($path[$i]);
00053                 
00054                 if ($className = $property->getClassName()) {
00055                     $this->fetchHelpers($className);
00056                     $proto = self::$protos[$className];
00057                 } elseif ($i < $size) {
00058                     continue;
00059                 } else {
00060                     throw new WrongArgumentException('corrupted path');
00061                 }
00062             }
00063         }
00064         
00065         public function getPath()
00066         {
00067             return $this->path;
00068         }
00069         
00070         public function getRoot()
00071         {
00072             return $this->root;
00073         }
00074         
00078         public function getFinalProto()
00079         {
00080             return self::$protos[$this->getFinalProperty()->getClassName()];
00081         }
00082         
00086         public function getFinalDao()
00087         {
00088             return self::$daos[$this->getFinalProperty()->getClassName()];
00089         }
00090         
00094         public function getFinalProperty()
00095         {
00096             return end($this->properties);
00097         }
00098         
00099         /* void */ private function fetchHelpers($className)
00100         {
00101             if (isset(self::$protos[$className], self::$daos[$className]))
00102                 return /* boo */;
00103             
00104             self::$protos[$className] = call_user_func(array($className, 'proto'));
00105             self::$daos[$className] = call_user_func(array($className, 'dao'));
00106             
00107             Assert::isTrue(
00108                 (self::$protos[$className] instanceof AbstractProtoClass)
00109                 && (self::$daos[$className] instanceof ComplexBuilderDAO)
00110             );
00111         }
00112     }
00113 ?>

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