protected function createPrototypeRemoveMethod(PropertyDefinition $property, PhpVariableName $reversePropertyName = null, $reversePropertyIsMany = false)
 {
     $name = $property->getName()->singularize();
     $methodName = new PhpVariableName('remove' . $name->toUpperCamelCase());
     $newType = $this->resolveElementType($property);
     $parameter = new ParameterDefinition($newType, $name);
     $method = new MethodDefinition($methodName, [$parameter]);
     $method->setVisibility(Visibility::PUBLIC_VISIBILITY());
     if (null !== $reversePropertyName) {
         $line = true === $reversePropertyIsMany ? '$%s->remove%s($this);' : '$%s->set%s(null);';
         $method->addLine(sprintf($line, $name->toLowerCamelCase(), $reversePropertyName->toUpperCamelCase()));
     }
     return $method;
 }
Ejemplo n.º 2
0
 /**
  * @param array $configuration
  * @param FileContainer $fileContainer
  * @throws \Exception
  */
 public function generate(array $configuration, FileContainer $fileContainer)
 {
     $this->configuration = $configuration;
     foreach ($configuration['entities'] as $entityName => $config) {
         if (true === $config['abstract']) {
             continue;
         }
         $relatedEntity = $this->createEntityFqcn($configuration, $entityName);
         $repositoryFile = new ClassFileDefinition($entityName, $config, $relatedEntity);
         $method = new MethodDefinition(new PhpVariableName('initMetadata'), [new ParameterDefinition(new ObjectType(new FullyQualifiedClassName('CCMBenchmark\\Ting\\Serializer\\SerializerFactoryInterface')), new PhpVariableName('serializerFactory')), new ParameterDefinition(new ArrayType(), new PhpVariableName('options'), new ArrayValue([]))]);
         $method->addLine(sprintf('
             $metadata = new Metadata($serializerFactory);
             $metadata->setEntity(%s::class);
             $metadata->setConnectionName($options[\'connection\']);
             $metadata->setDatabase($options[\'database\']);
             $metadata->setTable(\'%s\');
             ', $relatedEntity->getName(), $relatedEntity->getClassName()->toLowerSnakeCase()));
         foreach ($config['properties'] as $propertyName => $propertyConfig) {
             if ('relation' === $propertyConfig['type']) {
                 continue;
             }
             $propertyName = new PhpVariableName($propertyName);
             $fieldDefinition = ['primary' => $propertyConfig['primary'], 'autoincrement' => $propertyConfig['autoincrement'], 'fieldName' => $propertyName->toLowerCamelCase(), 'columnName' => $propertyName->toLowerSnakeCase(), 'type' => $this->transformTypeToValidTingType($propertyConfig['type'])];
             if ('enum' === $propertyConfig['type']) {
                 $fieldDefinition['serializer'] = null;
                 //todo: add a good serializer for each enumerator type
             }
             $fieldArray = array_diff_key($fieldDefinition, $this->getDefaultFieldConfiguration());
             $dataValue = $this->dataValueFactory->getDataValueFromValue($fieldArray);
             $method->addLine(sprintf('$metadata->addField(%s);', $dataValue->getPhpFormatedValue()));
         }
         $repositoryFile->setParent(new FullyQualifiedClassName('CCMBenchmark\\Ting\\Repository\\Repository'))->addInterface(new FullyQualifiedClassName('CCMBenchmark\\Ting\\Repository\\MetadataInitializer'))->addUse($relatedEntity->getFullQualifiedClassName())->addMethod($method);
         //Create
         $fileContainer->addFile($repositoryFile, [Tag::REPOSITORY()]);
     }
 }
    private function generateRelationAnnotation(ClassFileDefinition $entityFile, PropertyDefinition $property)
    {
        $definition = $property->getSourceConfiguration();
        $cardinality = $this->getCardinality($this->configuration, $property);
        switch ($cardinality) {
            //One to One
            case Cardinality::CARDINALITY_0_1:
            case Cardinality::CARDINALITY_1_0:
            case Cardinality::CARDINALITY_1_1:
                $annotationName = 'OneToOne';
                break;
                //One To Many
            //One To Many
            case Cardinality::CARDINALITY_0_N:
            case Cardinality::CARDINALITY_1_N:
                $annotationName = 'OneToMany';
                break;
                //Many To One
            //Many To One
            case Cardinality::CARDINALITY_N_0:
            case Cardinality::CARDINALITY_N_1:
                $annotationName = 'ManyToOne';
                break;
                //Many to Many
            //Many to Many
            case Cardinality::CARDINALITY_N_N:
                $annotationName = 'ManyToMany';
                break;
            default:
                throw new \Exception(sprintf('%s is not a valid cardinality', $cardinality));
        }
        $relationConfiguration = ['targetEntity' => '"' . $this->createEntityFqcn($this->configuration, $definition['entity'])->getFullQualifiedClassName() . '"'];
        $reversePropertyName = $this->getReversePropertyName($this->configuration, $property);
        if ("ManyToOne" === $annotationName && null !== $reversePropertyName && true === array_key_exists('inverse', $definition)) {
            throw new \Exception('Doctrine do not accept Inversed relation ManyToOne, Switch Cardinality to OneToMany');
        }
        if ("OneToMany" === $annotationName && null === $reversePropertyName) {
            throw new \Exception('Doctrine do not accept OneToMany, without reverse side');
        }
        if (null !== $reversePropertyName) {
            if (true === array_key_exists('inverse', $definition)) {
                $relationConfiguration['mappedBy'] = '"' . $reversePropertyName->toLowerCamelCase() . '"';
            } else {
                $relationConfiguration['inversedBy'] = '"' . $reversePropertyName->toLowerCamelCase() . '"';
            }
        }
        $formattedConf = [];
        foreach ($relationConfiguration as $key => $value) {
            $formattedConf[] = sprintf('%s=%s', $key, $value);
        }
        $property->addAnnotation(sprintf('@ORM\\%s(%s)', $annotationName, implode(', ', $formattedConf)));
        $inverseJoinColumns = $this->generateJoinColumn($definition['entity'], $property, $cardinality);
        if ($cardinality == Cardinality::CARDINALITY_N_N) {
            $entityName = $entityFile->getSourceName();
            $joinTableName = new PhpVariableName($entityName . '_' . $definition['entity']);
            $joinColumns = $this->generateJoinColumn($entityName, $property, $cardinality);
            $joinTableTemplate = <<<'ANNOT'
@ORM\JoinTable(name="%s",
 *      joinColumns={%s},
 *      inverseJoinColumns={%s}
 * )
ANNOT;
            $property->addAnnotation(sprintf($joinTableTemplate, $joinTableName->toLowerSnakeCase(), implode(', ', $joinColumns), implode(', ', $inverseJoinColumns)));
        } else {
            if (1 === count($inverseJoinColumns)) {
                $property->addAnnotation(reset($inverseJoinColumns));
            } elseif (1 < count($inverseJoinColumns)) {
                $property->addAnnotation(sprintf('@ORM\\JoinColumns({%s})', implode(', ', $inverseJoinColumns)));
            }
        }
    }
 /**
  * @return string class name without namespace
  */
 public function getName()
 {
     return $this->className->toUpperCamelCase();
 }