00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 final class AutoClassBuilder extends BaseBuilder
00017 {
00018 public static function build(MetaClass $class)
00019 {
00020 $out = self::getHead();
00021
00022 $out .= "abstract class Auto{$class->getName()}";
00023
00024 $isNamed = false;
00025
00026 if ($parent = $class->getParent())
00027 $out .= " extends {$parent->getName()}";
00028 elseif (
00029 $class->getPattern() instanceof DictionaryClassPattern
00030 && $class->hasProperty('name')
00031 ) {
00032 $out .= " extends NamedObject";
00033 $isNamed = true;
00034 } elseif (!$class->getPattern() instanceof ValueObjectPattern)
00035 $out .= " extends IdentifiableObject";
00036
00037 if ($interfaces = $class->getInterfaces())
00038 $out .= ' implements '.implode(', ', $interfaces);
00039
00040 $out .= "\n{\n";
00041
00042 foreach ($class->getProperties() as $property) {
00043 if (!self::doPropertyBuild($property, $isNamed))
00044 continue;
00045
00046 $out .=
00047 "protected \${$property->getName()} = "
00048 ."{$property->getType()->getDeclaration()};\n";
00049
00050 if ($property->getFetchStrategyId() == FetchStrategy::LAZY) {
00051 $out .=
00052 "protected \${$property->getName()}Id = null;\n";
00053 }
00054 }
00055
00056 $valueObjects = array();
00057
00058 foreach ($class->getProperties() as $property) {
00059 if (
00060 $property->getType() instanceof ObjectType
00061 && !$property->getType()->isGeneric()
00062 && $property->getType()->getClass()->getPattern()
00063 instanceof ValueObjectPattern
00064 ) {
00065 $valueObjects[$property->getName()] =
00066 $property->getType()->getClassName();
00067 }
00068 }
00069
00070 if ($valueObjects) {
00071 $out .= <<<EOT
00072
00073 public function __construct()
00074 {
00075
00076 EOT;
00077 foreach ($valueObjects as $propertyName => $className) {
00078 $out .= "\$this->{$propertyName} = new {$className}();\n";
00079 }
00080
00081 $out .= "}\n";
00082 }
00083
00084 $out .= self::buildSerializers($class);
00085
00086 foreach ($class->getProperties() as $property) {
00087 if (!self::doPropertyBuild($property, $isNamed))
00088 continue;
00089
00090 $out .= $property->toMethods($class);
00091 }
00092
00093 $out .= "}\n";
00094 $out .= self::getHeel();
00095
00096 return $out;
00097 }
00098
00099 private static function buildSerializers(MetaClass $class)
00100 {
00101 $slackers = array();
00102
00103 foreach ($class->getProperties() as $property) {
00104 if ($property->getFetchStrategyId() == FetchStrategy::LAZY) {
00105 $slackers[] = $property;
00106 }
00107 }
00108
00109 if (!$slackers)
00110 return null;
00111
00112 $out = <<<EOT
00113
00114 public function __sleep()
00115 {
00116 \$properties = get_object_vars(\$this);
00117
00118 unset(
00119
00120 EOT;
00121 $unsetters = array();
00122
00123 foreach ($slackers as $property) {
00124 $unsetters[] = "\$properties['{$property->getName()}']";
00125 }
00126
00127 $out .= implode(",\n", $unsetters);
00128
00129 $out .= <<<EOT
00130
00131 );
00132
00133 return array_keys(\$properties);
00134 }
00135
00136 EOT;
00137 return $out;
00138 }
00139
00140 private static function doPropertyBuild(
00141 MetaClassProperty $property,
00142 $isNamed
00143 )
00144 {
00145 if ($isNamed && $property->getName() == 'name')
00146 return false;
00147
00148 if (
00149 ($property->getName() == 'id')
00150 && !$property->getClass()->getParent()
00151 )
00152 return false;
00153
00154
00155 if (
00156 $property->getClass()->getParent()
00157 && array_key_exists(
00158 $property->getName(),
00159 $property->getClass()->getParentsProperties()
00160 )
00161 )
00162 return false;
00163
00164 return true;
00165 }
00166 }
00167 ?>