/**
  * {@inheritDoc}
  */
 public function generateCode($model, array $parameters = array())
 {
     $fqcn = get_class($model);
     $name = FullyQualifiedName::make($fqcn)->getName();
     $modelName = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $name));
     $parameters[$modelName] = $model;
     return $this->templateEngine->render('phpdoc/' . $modelName, $parameters);
 }
 /**
  * {@inheritDoc}
  */
 public function validate($model)
 {
     $firstElement = current($model);
     $fqcn = get_class($firstElement);
     $modelType = FullyQualifiedName::make($fqcn)->getName();
     $nameCount = array();
     foreach ($model as $element) {
         $name = $element->getName();
         $nameCount[$name] = isset($nameCount[$name]) ? $nameCount[$name] + 1 : 1;
     }
     $messages = array();
     foreach ($nameCount as $name => $count) {
         if ($count > 1) {
             $messages[] = sprintf('Collection "%s" cannot have name "%s" duplicates (%s occurences)', $modelType, $name, $count);
         }
     }
     return empty($messages) ? new NoneViolation() : new SomeViolation(implode("\n", $messages));
 }
示例#3
0
    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $model = $input->getArgument('model');
        $outputFile = sprintf('%s.php', $this->joinPaths($input->getOption('output'), str_replace('\\', DIRECTORY_SEPARATOR, $model)));
        $classObject = Object::make($model);
        $file = File::make($outputFile)->setStructure($classObject);
        $file->addFullyQualifiedName(FullyQualifiedName::make('Doctrine\\ODM\\MongoDB\\Mapping\\Annotations as ODM'));
        $file->addFullyQualifiedName(FullyQualifiedName::make('Hive\\Annotations as H'));
        $fields = array_merge(['id:id'], $input->getArgument('fields'));
        foreach ($fields as $field) {
            if (!preg_match('/(\\w+)(?::(string|boolean|id|reference[s]?)(?:\\[([\\w\\\\]+),(\\w+)\\])?)?(?::(\\w+))?/i', $field, $matches)) {
                continue;
            }
            @(list($all, $name, $type, $referenceType, $linkProperty, $fake) = $matches);
            /** @var Property $property */
            $property = Property::make($name);
            $property->makePublic();
            $property->setPhpdoc(new ODMPropertyPhpdoc($name, $type, $referenceType, $linkProperty, $fake));
            $classObject->addProperty($property);
        }
        $classObject->setPhpdoc(StructurePhpdoc::make()->setDescription(Description::make(<<<'EOF'
This class is generated with the hive console.

@ODM\Document @ODM\HasLifecycleCallbacks
EOF
)));
        $classObject->addMethod(Method::make('validate')->setPhpdoc(MethodPhpdoc::make()->setDescription(Description::make(<<<'EOF'
The validate method can be used to validate properties.

@ODM\PrePersist @ODM\PreUpdate
EOF
))));
        // Generate the code
        $prettyPrinter = Build::prettyPrinter();
        $prettyPrinter->addTemplatePath(realpath($this->joinPaths(dirname(__FILE__), '../../../templates/memio/')));
        $generatedCode = $prettyPrinter->generateCode($file);
        @mkdir(dirname($outputFile), 0755, true);
        file_put_contents($outputFile, html_entity_decode(html_entity_decode($generatedCode)));
        $io = new SymfonyStyle($input, $output);
        $io->success(["Model {$model} created in {$outputFile}"]);
    }
 public function testWithAlias()
 {
     $fullyQualifiedName = FullyQualifiedName::make('\\ArrayObject')->setAlias('StdArray');
     $generatedCode = $this->prettyPrinter->generateCode($fullyQualifiedName);
     $this->assertSame('use ArrayObject as StdArray;', $generatedCode);
 }