AutoClassBuilder.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     final class AutoClassBuilder extends BaseBuilder
00016     {
00017         public static function build(MetaClass $class)
00018         {
00019             $out = self::getHead();
00020             
00021             $out .= "abstract class Auto{$class->getName()}";
00022             
00023             $isNamed = false;
00024             
00025             if ($parent = $class->getParent())
00026                 $out .= " extends {$parent->getName()}";
00027             elseif (
00028                 $class->getPattern() instanceof DictionaryClassPattern
00029                 && $class->hasProperty('name')
00030             ) {
00031                 $out .= " extends NamedObject";
00032                 $isNamed = true;
00033             } elseif (!$class->getPattern() instanceof ValueObjectPattern)
00034                 $out .= " extends IdentifiableObject";
00035             
00036             if ($interfaces = $class->getInterfaces())
00037                 $out .= ' implements '.implode(', ', $interfaces);
00038             
00039             $out .= "\n{\n";
00040             
00041             foreach ($class->getProperties() as $property) {
00042                 if (!self::doPropertyBuild($class, $property, $isNamed))
00043                     continue;
00044                 
00045                 $out .=
00046                     "protected \${$property->getName()} = "
00047                     ."{$property->getType()->getDeclaration()};\n";
00048                 
00049                 if ($property->getFetchStrategyId() == FetchStrategy::LAZY) {
00050                     $out .=
00051                         "protected \${$property->getName()}Id = null;\n";
00052                 }
00053             }
00054             
00055             $valueObjects = array();
00056             
00057             foreach ($class->getProperties() as $property) {
00058                 if (
00059                     $property->getType() instanceof ObjectType
00060                     && !$property->getType()->isGeneric()
00061                     && $property->getType()->getClass()->getPattern()
00062                         instanceof ValueObjectPattern
00063                 ) {
00064                     $valueObjects[$property->getName()] =
00065                         $property->getType()->getClassName();
00066                 }
00067             }
00068             
00069             if ($valueObjects) {
00070                 $out .= <<<EOT
00071 
00072 public function __construct()
00073 {
00074 
00075 EOT;
00076                 foreach ($valueObjects as $propertyName => $className) {
00077                     $out .= "\$this->{$propertyName} = new {$className}();\n";
00078                 }
00079                 
00080                 $out .= "}\n";
00081             }
00082             
00083             foreach ($class->getProperties() as $property) {
00084                 if (!self::doPropertyBuild($class, $property, $isNamed))
00085                     continue;
00086                 
00087                 $out .= $property->toMethods($class);
00088             }
00089             
00090             $out .= "}\n";
00091             $out .= self::getHeel();
00092             
00093             return $out;
00094         }
00095         
00096         private static function doPropertyBuild(
00097             MetaClass $class,
00098             MetaClassProperty $property,
00099             $isNamed
00100         )
00101         {
00102             if (
00103                 $parentProperty =
00104                     $class->isRedefinedProperty($property->getName())
00105             ) {
00106                 // check wheter property fetch strategy becomes lazy
00107                 if (
00108                     (
00109                         $parentProperty->getFetchStrategyId()
00110                         <> $property->getFetchStrategyId()
00111                     ) && (
00112                         $property->getFetchStrategyId() === FetchStrategy::LAZY
00113                     )
00114                 )
00115                     return true;
00116                 
00117                 return false;
00118             }
00119             
00120             if ($isNamed && $property->getName() == 'name')
00121                 return false;
00122             
00123             if (
00124                 ($property->getName() == 'id')
00125                 && !$property->getClass()->getParent()
00126             )
00127                 return false;
00128             
00129             // do not redefine parent's properties
00130             if (
00131                 $property->getClass()->getParent()
00132                 && array_key_exists(
00133                     $property->getName(),
00134                     $property->getClass()->getAllParentsProperties()
00135                 )
00136             )
00137                 return false;
00138             
00139             return true;
00140         }
00141     }
00142 ?>