private function generateDocumentStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null) { // Add/remove methods should use the singular form of the field name $formattedFieldName = in_array($type, array('add', 'remove')) ? Inflector::singularize($fieldName) : $fieldName; $methodName = $type . Inflector::classify($formattedFieldName); $variableName = Inflector::camelize($formattedFieldName); if ($this->hasMethod($methodName, $metadata)) { return; } $description = ucfirst($type) . ' ' . $variableName; $types = Type::getTypesMap(); $methodTypeHint = $typeHint && !isset($types[$typeHint]) ? '\\' . $typeHint . ' ' : null; $variableType = $typeHint ? $typeHint . ' ' : null; $replacements = array('<description>' => $description, '<methodTypeHint>' => $methodTypeHint, '<variableType>' => $variableType, '<variableName>' => $variableName, '<methodName>' => $methodName, '<fieldName>' => $fieldName, '<variableDefault>' => $defaultValue !== null ? ' = ' . $defaultValue : ''); $templateVar = sprintf('%sMethodTemplate', $type); $method = str_replace(array_keys($replacements), array_values($replacements), self::${$templateVar}); return $this->prefixCodeWithSpaces($method); }
/** * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @param \Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper $dialog * * @return array * @throws \InvalidArgumentException */ private function addFields(InputInterface $input, OutputInterface $output, QuestionHelper $dialog) { $fields = $this->parseFields($input->getOption('fields')); $output->writeln(array('', 'Instead of starting with a blank document, you can add some fields now.', 'Note that the primary key will be added automatically (named <comment>id</comment>).', '')); $output->write('<info>Available types:</info> '); $types = array_keys(Type::getTypesMap()); $count = 20; foreach ($types as $i => $type) { if ($count > 50) { $count = 0; $output->writeln(''); } $count += strlen($type); $output->write(sprintf('<comment>%s</comment>', $type)); $output->write(count($types) != $i + 1 ? ', ' : '.'); } $output->writeln(''); $fieldValidator = function ($type) use($types) { if (!in_array($type, $types)) { throw new \InvalidArgumentException(sprintf('Invalid type "%s".', $type)); } return $type; }; while (true) { $output->writeln(''); if (!($name = $this->askForFieldName($input, $output, $dialog, $fields))) { break; } $defaultType = 'string'; if (substr($name, -3) == '_at') { $defaultType = 'timestamp'; } $question = new Question($dialog->getQuestion('Field type', $defaultType), $defaultType); $question->setValidator($fieldValidator); $type = $dialog->ask($input, $output, $question); $fields[$name] = array('fieldName' => $name, 'type' => $type); } return $fields; }