ObjectType.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: ObjectType.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     class ObjectType extends BasePropertyType
00017     {
00018         private $className = null;
00019         
00020         public function __construct($className)
00021         {
00022             $this->className = $className;
00023         }
00024         
00028         public function getClass()
00029         {
00030             return MetaConfiguration::me()->getClassByName($this->className);
00031         }
00032         
00033         public function getClassName()
00034         {
00035             return $this->className;
00036         }
00037         
00038         public function getDeclaration()
00039         {
00040             return 'null';
00041         }
00042         
00043         public function isGeneric()
00044         {
00045             return false;
00046         }
00047         
00048         public function isMeasurable()
00049         {
00050             return false;
00051         }
00052         
00053         public function toMethods(
00054             MetaClass $class,
00055             MetaClassProperty $property,
00056             MetaClassProperty $holder = null
00057         )
00058         {
00059             return
00060                 parent::toMethods($class, $property, $holder)
00061                 .$this->toDropper($class, $property, $holder);
00062         }
00063         
00064         public function toGetter(
00065             MetaClass $class,
00066             MetaClassProperty $property,
00067             MetaClassProperty $holder = null
00068         )
00069         {
00070             if ($class->getPattern() instanceof DtoClassPattern)
00071                 return
00072                     parent::toGetter(
00073                         $class,
00074                         $property,
00075                         $holder
00076                     );
00077             
00078             $name = $property->getName();
00079             
00080             $methodName = 'get'.ucfirst($property->getName());
00081             
00082             $classHint = $property->getType()->getHint();
00083             
00084             if ($holder) {
00085                 if ($property->getType() instanceof ObjectType) {
00086                     $class = $property->getType()->getClassName();
00087                 } else {
00088                     $class = null;
00089                 }
00090                 
00091                 return <<<EOT
00092 
00096 public function {$methodName}()
00097 {
00098     return \$this->{$holder->getName()}->{$methodName}();
00099 }
00100 
00101 EOT;
00102             } else {
00103                 if ($property->getFetchStrategyId() == FetchStrategy::LAZY) {
00104                     $className = $property->getType()->getClassName();
00105                     
00106                     if ($property->isRequired()) {
00107                         $method = <<<EOT
00108 
00109 {$classHint}
00110 public function {$methodName}()
00111 {
00112     if (!\$this->{$name}) {
00113         \$this->{$name} = {$className}::dao()->getById(\$this->{$name}Id);
00114     }
00115 
00116     return \$this->{$name};
00117 }
00118 
00119 EOT;
00120                     } else {
00121                         $method = <<<EOT
00122 
00123 {$classHint}
00124 public function {$methodName}()
00125 {
00126     if (!\$this->{$name} && \$this->{$name}Id) {
00127         \$this->{$name} = {$className}::dao()->getById(\$this->{$name}Id);
00128     }
00129     
00130     return \$this->{$name};
00131 }
00132 
00133 EOT;
00134                     }
00135                     
00136                     $method .= <<<EOT
00137 
00138 public function {$methodName}Id()
00139 {
00140     return \$this->{$name}Id;
00141 }
00142 
00143 EOT;
00144                 } elseif (
00145                     $property->getRelationId() == MetaRelation::ONE_TO_MANY
00146                     || $property->getRelationId() == MetaRelation::MANY_TO_MANY
00147                 ) {
00148                         $name = $property->getName();
00149                         $methodName = ucfirst($name);
00150                         $remoteName = ucfirst($property->getName());
00151                         
00152                         $containerName = $class->getName().$remoteName.'DAO';
00153                         
00154                         $method = <<<EOT
00155 
00159 public function get{$methodName}(\$lazy = false)
00160 {
00161     if (!\$this->{$name} || (\$this->{$name}->isLazy() != \$lazy)) {
00162         \$this->{$name} = new {$containerName}(\$this, \$lazy);
00163     }
00164     
00165     return \$this->{$name};
00166 }
00167 
00171 public function fill{$methodName}(\$collection, \$lazy = false)
00172 {
00173     \$this->{$name} = new {$containerName}(\$this, \$lazy);
00174     
00175     if (!\$this->id) {
00176         throw new WrongStateException(
00177             'i do not know which object i belong to'
00178         );
00179     }
00180     
00181     \$this->{$name}->mergeList(\$collection);
00182     
00183     return \$this;
00184 }
00185 
00186 EOT;
00187                 } else {
00188                     $method = <<<EOT
00189 
00190 {$classHint}
00191 public function {$methodName}()
00192 {
00193     return \$this->{$name};
00194 }
00195 
00196 EOT;
00197                 }
00198             }
00199             
00200             return $method;
00201         }
00202         
00203         public function toSetter(
00204             MetaClass $class,
00205             MetaClassProperty $property,
00206             MetaClassProperty $holder = null
00207         )
00208         {
00209             if ($class->getPattern() instanceof DtoClassPattern)
00210                 return
00211                     parent::toSetter(
00212                         $class,
00213                         $property,
00214                         $holder
00215                     );
00216             
00217             if (
00218                 $property->getRelationId() == MetaRelation::ONE_TO_MANY
00219                 || $property->getRelationId() == MetaRelation::MANY_TO_MANY
00220             ) {
00221                 // we don't need setter in such cases
00222                 return null;
00223             }
00224             
00225             $name = $property->getName();
00226             $methodName = 'set'.ucfirst($name);
00227             $classHint = $this->getHint();
00228             
00229             if ($holder) {
00230                 return <<<EOT
00231 
00235 public function {$methodName}({$property->getType()->getClassName()} \${$name})
00236 {
00237     \$this->{$holder->getName()}->{$methodName}(\${$name});
00238     
00239     return \$this;
00240 }
00241 
00242 EOT;
00243             } else {
00244                 if ($property->getFetchStrategyId() == FetchStrategy::LAZY) {
00245                     $method = <<<EOT
00246 
00250 public function {$methodName}({$this->className} \${$name})
00251 {
00252     \$this->{$name} = \${$name};
00253     \$this->{$name}Id = \${$name}->getId();
00254 
00255     return \$this;
00256 }
00257 
00261 public function {$methodName}Id(\$id)
00262 {
00263     \$this->{$name} = null;
00264     \$this->{$name}Id = \$id;
00265 
00266     return \$this;
00267 }
00268 
00269 EOT;
00270                 } else {
00271                     $method = <<<EOT
00272 
00276 public function {$methodName}({$this->className} \${$name})
00277 {
00278     \$this->{$name} = \${$name};
00279 
00280     return \$this;
00281 }
00282 
00283 EOT;
00284                 }
00285             }
00286             
00287             return $method;
00288         }
00289         
00290         public function toDropper(
00291             MetaClass $class,
00292             MetaClassProperty $property,
00293             MetaClassProperty $holder = null
00294         )
00295         {
00296             if (
00297                 $property->getRelationId() == MetaRelation::ONE_TO_MANY
00298                 || $property->getRelationId() == MetaRelation::MANY_TO_MANY
00299             ) {
00300                 // we don't need dropper in such cases
00301                 return null;
00302             }
00303             
00304             $name = $property->getName();
00305             $methodName = 'drop'.ucfirst($name);
00306             
00307             if ($holder) {
00308                     $method = <<<EOT
00309 
00313 public function {$methodName}()
00314 {
00315     \$this->{$holder->getName()}->{$methodName}();
00316 
00317     return \$this;
00318 }
00319 
00320 EOT;
00321             } else {
00322                 if ($property->getFetchStrategyId() == FetchStrategy::LAZY) {
00323                     $method = <<<EOT
00324 
00328 public function {$methodName}()
00329 {
00330     \$this->{$name} = null;
00331     \$this->{$name}Id = null;
00332 
00333     return \$this;
00334 }
00335 
00336 EOT;
00337                 } else {
00338                     if ($class->getPattern() instanceof DtoClassPattern)
00339                         $classNamePrefix = 'Dto';
00340                     else
00341                         $classNamePrefix = null;
00342 
00343                     $method = <<<EOT
00344 
00348 public function {$methodName}()
00349 {
00350     \$this->{$name} = null;
00351 
00352     return \$this;
00353 }
00354 
00355 EOT;
00356                 }
00357             }
00358             
00359             return $method;
00360         }
00361         
00362         public function toPrimitive()
00363         {
00364             throw new UnsupportedMethodException();
00365         }
00366         
00367         public function toXsdType()
00368         {
00369             return "tns:" . $this->className;
00370         }
00371         
00372         public function toColumnType()
00373         {
00374             return $this->getClass()->getIdentifier()->getType()->toColumnType();
00375         }
00376         
00377         public function getHint()
00378         {
00379             return <<<EOT
00383 EOT;
00384         }
00385     }
00386 ?>

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