PropertyPath.class.php

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