onPHP

main/DAOs/ComplexBuilderDAO.class.php Source File

 

ComplexBuilderDAO.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 General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: ComplexBuilderDAO.class.php 3202 2007-04-30 19:01:43Z voxus $ */
00012 
00016     abstract class ComplexBuilderDAO extends StorableDAO
00017     {
00018         abstract protected function makeSelf(&$array, $prefix = null);
00019         
00020         public function getMapping()
00021         {
00022             $proto = call_user_func(array($this->getObjectName(), 'proto'));
00023             
00024             return $proto->getMapping();
00025         }
00026         
00027         public function getFields()
00028         {
00029             static $fields = array();
00030             
00031             $className = $this->getObjectName();
00032             
00033             if (!isset($fields[$className])) {
00034                 foreach (array_values($this->getMapping()) as $field) {
00035                     if (is_array($field))
00036                         $fields[$className] =
00037                             array_merge(
00038                                 $fields[$className],
00039                                 $field
00040                             );
00041                     elseif ($field)
00042                         $fields[$className][] = $field;
00043                 }
00044             }
00045             
00046             return $fields[$className];
00047         }
00048         
00049         public function getJoinPrefix($field, $prefix = null)
00050         {
00051             return $this->getJoinName($field, $prefix).'__';
00052         }
00053         
00054         public function getJoinName($field, $prefix = null)
00055         {
00056             return dechex(crc32($prefix.$this->getTable())).'_'.$field;
00057         }
00058         
00059         public function makeObject(&$array, $prefix = null)
00060         {
00061             return
00062                 $this->makeCascade(
00063                     $this->selfSpawn($array, $prefix),
00064                     $array,
00065                     $prefix
00066                 );
00067         }
00068         
00069         public function makeJoinedObject(&$array, $prefix = null)
00070         {
00071             return
00072                 $this->makeJoiners(
00073                     $this->selfSpawn($array, $prefix),
00074                     $array,
00075                     $prefix
00076                 );
00077         }
00078         
00080 
00081         protected function makeJoiners(
00082             /* Identifiable */ $object, &$array, $prefix = null
00083         )
00084         {
00085             return $object;
00086         }
00087         
00088         protected function makeCascade(
00089             /* Identifiable */ $object, &$array, $prefix = null
00090         )
00091         {
00092             return $object;
00093         }
00095         
00096         private function selfSpawn(&$array, $prefix = null)
00097         {
00098             if (isset($this->identityMap[$array[$prefix.'id']]))
00099                 $object = $this->identityMap[$array[$prefix.'id']];
00100             else {
00101                 $object = $this->makeSelf($array, $prefix);
00102                 $this->identityMap[$object->getId()] = $object;
00103             }
00104             
00105             return $object;
00106         }
00107     }
00108 ?>

generated by doxygen-1.5.1
for onPHP at Mon Apr 30 23:10:10 2007