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;
 }
Esempio 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()]);
     }
 }