예제 #1
0
    public static function buildContainer(MetaClass $class, MetaClassProperty $holder)
    {
        $out = self::getHead();
        $out .= <<<EOT
namespace {$class->getDaoNamespace()};


EOT;
        $containerName = $class->getName() . ucfirst($holder->getName()) . 'DAO';
        $containerType = $holder->getRelation()->toString() . 'Linked';
        $uses = ['Hesper\\Main\\UnifiedContainer\\' . $containerType, MetaClassNameBuilder::getClassOfMetaClass($class), MetaClassNameBuilder::getClassOfMetaProperty($holder)];
        foreach ($uses as $import) {
            $out .= <<<EOT
use {$import};

EOT;
        }
        $out .= <<<EOT


EOT;
        $out .= 'final class ' . $containerName . ' extends ' . $containerType . "\n{\n";
        $className = $class->getName();
        $propertyName = strtolower($className[0]) . substr($className, 1);
        $remoteColumnName = $holder->getType()->getClass()->getTableName();
        $out .= <<<EOT
public function __construct({$className} \${$propertyName}, \$lazy = false)
{
\tparent::__construct(
\t\t\${$propertyName},
\t\t{$holder->getType()->getClassName()}::dao(),
\t\t\$lazy
\t);
}

/**
 * @return {$containerName}
**/
public static function create({$className} \${$propertyName}, \$lazy = false)
{
\treturn new self(\${$propertyName}, \$lazy);
}

EOT;
        if ($holder->getRelation()->getId() == MetaRelation::MANY_TO_MANY) {
            $out .= <<<EOT

public function getHelperTable()
{
\treturn '{$class->getTableName()}_{$remoteColumnName}';
}

public function getChildIdField()
{
\treturn '{$remoteColumnName}_id';
}

EOT;
        }
        $out .= <<<EOT

public function getParentIdField()
{
\treturn '{$class->getTableName()}_id';
}

EOT;
        $out .= "}\n";
        $out .= self::getHeel();
        return $out;
    }
예제 #2
0
    public static function build(MetaClass $class)
    {
        $out = self::getHead();
        $out .= <<<EOT
namespace {$class->getAutoNamespace()};


EOT;
        $uses = [IdentifiableObject::class, MetaClassNameBuilder::getClassOfMetaClass($class)];
        foreach ($class->getProperties() as $property) {
            $dependency = null;
            if ($property->getType() instanceof ObjectType) {
                if ($property->getRelationId() == MetaRelation::ONE_TO_MANY | $property->getRelationId() == MetaRelation::MANY_TO_MANY) {
                    $dependency = MetaClassNameBuilder::getContainerClassOfMetaProperty($property);
                } else {
                    $dependency = MetaClassNameBuilder::getClassOfMetaProperty($property);
                }
            } elseif ($property->getType() instanceof BooleanType && $property->isOptional()) {
                $dependency = Assert::class;
            }
            if (!is_null($dependency) && !in_array($dependency, $uses)) {
                $uses[] = $dependency;
            }
        }
        foreach ($uses as $import) {
            $out .= <<<EOT
use {$import};

EOT;
        }
        $out .= <<<EOT

abstract class Auto{$class->getName()}
EOT;
        $isNamed = false;
        if ($parent = $class->getParent()) {
            $out .= " extends \\{$parent->getNamespace()}\\{$parent->getName()}";
        } elseif ($class->getPattern() instanceof DictionaryClassPattern && $class->hasProperty('name')) {
            $out .= " extends NamedObject";
            $isNamed = true;
        } elseif (!$class->getPattern() instanceof ValueObjectPattern) {
            $out .= " extends IdentifiableObject";
        }
        if ($interfaces = $class->getInterfaces()) {
            $out .= ' implements ' . implode(', ', $interfaces);
        }
        $out .= "\n{\n";
        foreach ($class->getProperties() as $property) {
            if (!self::doPropertyBuild($class, $property, $isNamed)) {
                continue;
            }
            if ($property->getFetchStrategyId() == FetchStrategy::LAZY) {
                $out .= "protected \${$property->getName()}Id = null;\n";
            } else {
                $out .= "protected \${$property->getName()} = " . "{$property->getType()->getDeclaration()};\n";
            }
        }
        $valueObjects = [];
        foreach ($class->getProperties() as $property) {
            if ($property->getType() instanceof ObjectType && !$property->getType()->isGeneric() && $property->getType()->getClass()->getPattern() instanceof ValueObjectPattern) {
                $valueObjects[$property->getName()] = $property->getType()->getClassName();
            }
        }
        if ($valueObjects) {
            $out .= <<<EOT

public function __construct()
{

EOT;
            foreach ($valueObjects as $propertyName => $className) {
                $out .= "\$this->{$propertyName} = new {$className}();\n";
            }
            $out .= "}\n";
        }
        foreach ($class->getProperties() as $property) {
            if (!self::doPropertyBuild($class, $property, $isNamed)) {
                continue;
            }
            $out .= $property->toMethods($class);
        }
        $out .= "}\n";
        $out .= self::getHeel();
        return $out;
    }
예제 #3
0
 /**
  * @param MetaClass $holder
  *
  * @return LightMetaProperty
  */
 public function toLightProperty(MetaClass $holder)
 {
     $className = null;
     $businessClassName = null;
     if ($this->getRelationId() == MetaRelation::ONE_TO_MANY || $this->getRelationId() == MetaRelation::MANY_TO_MANY) {
         // collections
         $primitiveName = 'identifierList';
         $businessClassName = MetaClassNameBuilder::getClassOfMetaProperty($this, true);
     } elseif ($this->isIdentifier()) {
         if ($this->getType() instanceof IntegerType) {
             $primitiveName = 'integerIdentifier';
             $className = $holder->getName();
         } elseif ($this->getType() instanceof StringType) {
             $primitiveName = 'scalarIdentifier';
             $className = $holder->getName();
         } elseif ($this->getType() instanceof UuidType) {
             $primitiveName = 'uuidIdentifier';
             $className = $holder->getName();
         } else {
             $primitiveName = $this->getType()->getPrimitiveName();
         }
         $businessClassName = MetaClassNameBuilder::getClassOfMetaClass($holder, true);
     } elseif (!$this->isIdentifier() && !$this->getType()->isGeneric() && $this->getType() instanceof ObjectType) {
         $pattern = $this->getType()->getClass()->getPattern();
         if ($pattern instanceof EnumerationClassPattern) {
             $primitiveName = 'enumeration';
             $businessClassName = MetaClassNameBuilder::getClassOfMetaProperty($this, true);
         } elseif ($pattern instanceof EnumClassPattern) {
             $primitiveName = 'enum';
             $businessClassName = MetaClassNameBuilder::getClassOfMetaProperty($this, true);
         } elseif ($pattern instanceof RegistryClassPattern) {
             $primitiveName = 'registry';
             $businessClassName = MetaClassNameBuilder::getClassOfMetaProperty($this, true);
         } elseif (($pattern instanceof DictionaryClassPattern || $pattern instanceof StraightMappingPattern) && ($identifier = $this->getType()->getClass()->getIdentifier())) {
             $businessClassName = MetaClassNameBuilder::getClassOfMetaProperty($this, true);
             if ($identifier->getType() instanceof IntegerType) {
                 $primitiveName = 'integerIdentifier';
             } elseif ($identifier->getType() instanceof StringType) {
                 $primitiveName = 'scalarIdentifier';
             } elseif ($identifier->getType() instanceof UuidType) {
                 $primitiveName = 'uuidIdentifier';
             } else {
                 $primitiveName = $this->getType()->getPrimitiveName();
             }
         } else {
             $primitiveName = $this->getType()->getPrimitiveName();
         }
     } else {
         if ($this->getType() instanceof ObjectType) {
             $businessClassName = MetaClassNameBuilder::getClassOfMetaProperty($this, true);
         }
         $primitiveName = $this->getType()->getPrimitiveName();
     }
     $inner = false;
     if ($this->getType() instanceof ObjectType) {
         $className = $this->getType()->getClassName();
         if (!$this->getType()->isGeneric()) {
             $class = $this->getType()->getClass();
             $pattern = $class->getPattern();
             if ($pattern instanceof InternalClassPattern) {
                 $className = $holder->getName();
             }
             if (($pattern instanceof InternalClassPattern || $pattern instanceof ValueObjectPattern) && $className != $holder->getName()) {
                 $inner = true;
             }
         }
     }
     $propertyClassName = $inner ? InnerMetaProperty::class : LightMetaProperty::class;
     if (empty($businessClassName)) {
         $businessClassName = $className;
     }
     if ($this->getType() instanceof IntegerType) {
         $size = $this->getType()->getSize();
     } elseif ($this->getType() instanceof ObjectType && $this->getRelationId() == MetaRelation::ONE_TO_ONE && ($identifier = $this->getType()->getClass()->getIdentifier()) && $this->getType()->isMeasurable()) {
         $size = $identifier->getType()->getSize();
     } elseif ($this->getType()->isMeasurable()) {
         $size = $this->size;
     } else {
         $size = null;
     }
     return call_user_func_array([$propertyClassName, 'fill'], [new $propertyClassName(), $this->getName(), $this->getName() != $this->getRelationColumnName() ? $this->getRelationColumnName() : null, $primitiveName, $businessClassName, $size, $this->isRequired(), $this->getType()->isGeneric(), $inner, $this->getRelationId(), $this->getFetchStrategyId()]);
 }