ObjectType.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     class ObjectType extends BasePropertyType
00016     {
00017         private $className = null;
00018         
00019         public function getPrimitiveName()
00020         {
00021             return 'identifier';
00022         }
00023         
00024         public function __construct($className)
00025         {
00026             $this->className = $className;
00027         }
00028         
00032         public function getClass()
00033         {
00034             return MetaConfiguration::me()->getClassByName($this->className);
00035         }
00036         
00037         public function getClassName()
00038         {
00039             return $this->className;
00040         }
00041         
00042         public function getDeclaration()
00043         {
00044             return 'null';
00045         }
00046         
00047         public function isGeneric()
00048         {
00049             return false;
00050         }
00051         
00052         public function isMeasurable()
00053         {
00054             return false;
00055         }
00056         
00057         public function toMethods(
00058             MetaClass $class,
00059             MetaClassProperty $property,
00060             MetaClassProperty $holder = null
00061         )
00062         {
00063             return
00064                 parent::toMethods($class, $property, $holder)
00065                 .$this->toDropper($class, $property, $holder);
00066         }
00067         
00068         public function toGetter(
00069             MetaClass $class,
00070             MetaClassProperty $property,
00071             MetaClassProperty $holder = null
00072         )
00073         {
00074             $name = $property->getName();
00075             
00076             $methodName = 'get'.ucfirst($property->getName());
00077             
00078             $classHint = $property->getType()->getHint();
00079             
00080             if ($holder) {
00081                 if ($property->getType() instanceof ObjectType) {
00082                     $class = $property->getType()->getClassName();
00083                 } else {
00084                     $class = null;
00085                 }
00086                 
00087                 return <<<EOT
00088 
00092 public function {$methodName}()
00093 {
00094     return \$this->{$holder->getName()}->{$methodName}();
00095 }
00096 
00097 EOT;
00098             } else {
00099                 if ($property->getFetchStrategyId() == FetchStrategy::LAZY) {
00100                     $className = $property->getType()->getClassName();
00101                     
00102                     $isEnumeration =
00103                         $property->getType()->getClass()->getPattern()
00104                         instanceof EnumerationClassPattern;
00105                     
00106                     $fetchObjectString = $isEnumeration
00107                         ? "new {$className}(\$this->{$name}Id)"
00108                         : "{$className}::dao()->getById(\$this->{$name}Id)";
00109                     
00110                     $method = <<<EOT
00111 
00112 {$classHint}
00113 public function {$methodName}()
00114 {
00115     if (!\$this->{$name} && \$this->{$name}Id) {
00116         \$this->{$name} = {$fetchObjectString};
00117     }
00118     
00119     return \$this->{$name};
00120 }
00121 
00122 public function {$methodName}Id()
00123 {
00124     return \$this->{$name}Id;
00125 }
00126 
00127 EOT;
00128                 } elseif (
00129                     $property->getRelationId() == MetaRelation::ONE_TO_MANY
00130                     || $property->getRelationId() == MetaRelation::MANY_TO_MANY
00131                 ) {
00132                         $name = $property->getName();
00133                         $methodName = ucfirst($name);
00134                         $remoteName = ucfirst($property->getName());
00135                         
00136                         $containerName = $class->getName().$remoteName.'DAO';
00137                         
00138                         $method = <<<EOT
00139 
00143 public function get{$methodName}(\$lazy = false)
00144 {
00145     if (!\$this->{$name} || (\$this->{$name}->isLazy() != \$lazy)) {
00146         \$this->{$name} = new {$containerName}(\$this, \$lazy);
00147     }
00148     
00149     return \$this->{$name};
00150 }
00151 
00155 public function fill{$methodName}(\$collection, \$lazy = false)
00156 {
00157     \$this->{$name} = new {$containerName}(\$this, \$lazy);
00158     
00159     if (!\$this->id) {
00160         throw new WrongStateException(
00161             'i do not know which object i belong to'
00162         );
00163     }
00164     
00165     \$this->{$name}->mergeList(\$collection);
00166     
00167     return \$this;
00168 }
00169 
00170 EOT;
00171                 } else {
00172                     $method = <<<EOT
00173 
00174 {$classHint}
00175 public function {$methodName}()
00176 {
00177     return \$this->{$name};
00178 }
00179 
00180 EOT;
00181                 }
00182             }
00183             
00184             return $method;
00185         }
00186         
00187         public function toSetter(
00188             MetaClass $class,
00189             MetaClassProperty $property,
00190             MetaClassProperty $holder = null
00191         )
00192         {
00193             if (
00194                 $property->getRelationId() == MetaRelation::ONE_TO_MANY
00195                 || $property->getRelationId() == MetaRelation::MANY_TO_MANY
00196             ) {
00197                 // we don't need setter in such cases
00198                 return null;
00199             }
00200             
00201             $name = $property->getName();
00202             $methodName = 'set'.ucfirst($name);
00203             $classHint = $this->getHint();
00204             
00205             if ($holder) {
00206                 return <<<EOT
00207 
00211 public function {$methodName}({$property->getType()->getClassName()} \${$name})
00212 {
00213     \$this->{$holder->getName()}->{$methodName}(\${$name});
00214     
00215     return \$this;
00216 }
00217 
00218 EOT;
00219             } else {
00220                 if ($property->getFetchStrategyId() == FetchStrategy::LAZY) {
00221                     $method = <<<EOT
00222 
00226 public function {$methodName}({$this->className} \${$name})
00227 {
00228     \$this->{$name} = \${$name};
00229     \$this->{$name}Id = \${$name}->getId();
00230 
00231     return \$this;
00232 }
00233 
00237 public function {$methodName}Id(\$id)
00238 {
00239     \$this->{$name} = null;
00240     \$this->{$name}Id = \$id;
00241 
00242     return \$this;
00243 }
00244 
00245 EOT;
00246                 } else {
00247                     $method = <<<EOT
00248 
00252 public function {$methodName}({$this->className} \${$name})
00253 {
00254     \$this->{$name} = \${$name};
00255 
00256     return \$this;
00257 }
00258 
00259 EOT;
00260                 }
00261             }
00262             
00263             return $method;
00264         }
00265         
00266         public function toDropper(
00267             MetaClass $class,
00268             MetaClassProperty $property,
00269             MetaClassProperty $holder = null
00270         )
00271         {
00272             if (
00273                 $property->getRelationId() == MetaRelation::ONE_TO_MANY
00274                 || $property->getRelationId() == MetaRelation::MANY_TO_MANY
00275             ) {
00276                 // we don't need dropper in such cases
00277                 return null;
00278             }
00279             
00280             $name = $property->getName();
00281             $methodName = 'drop'.ucfirst($name);
00282             
00283             if ($holder) {
00284                     $method = <<<EOT
00285 
00289 public function {$methodName}()
00290 {
00291     \$this->{$holder->getName()}->{$methodName}();
00292 
00293     return \$this;
00294 }
00295 
00296 EOT;
00297             } else {
00298                 if ($property->getFetchStrategyId() == FetchStrategy::LAZY) {
00299                     $method = <<<EOT
00300 
00304 public function {$methodName}()
00305 {
00306     \$this->{$name} = null;
00307     \$this->{$name}Id = null;
00308 
00309     return \$this;
00310 }
00311 
00312 EOT;
00313                 } else {
00314                     $method = <<<EOT
00315 
00319 public function {$methodName}()
00320 {
00321     \$this->{$name} = null;
00322 
00323     return \$this;
00324 }
00325 
00326 EOT;
00327                 }
00328             }
00329             
00330             return $method;
00331         }
00332         
00333         public function toColumnType()
00334         {
00335             return $this->getClass()->getIdentifier()->getType()->toColumnType();
00336         }
00337         
00338         public function getHint()
00339         {
00340             return <<<EOT
00344 EOT;
00345         }
00346     }
00347 ?>