Newscoop\PluginGeneratorBundle\Command\Validators::validateVendor PHP Method

validateVendor() public static method

public static validateVendor ( $vendor )
    public static function validateVendor($vendor)
    {
        if (!preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $vendor)) {
            throw new \InvalidArgumentException('The vendor contains invalid characters.');
        }
        return $vendor;
    }

Usage Example

 protected function interact(InputInterface $input, OutputInterface $output)
 {
     $dialog = $this->getDialogHelper();
     $dialog->writeSection($output, 'Welcome to the Newscoop Plugin bundle generator');
     // vendor
     $vendor = null;
     try {
         // validate the plugin name
         $vendor = $input->getOption('vendor') ? Validators::validateVendor($input->getOption('vendor')) : null;
     } catch (\Exception $error) {
         $output->writeln($dialog->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
     }
     if (null === $vendor) {
         $output->writeln(array('', 'Your application code must be written in <comment>bundles</comment>. This command helps', 'you generate the bundle name and namespace easily.', '', 'Each bundle is hosted under a namespace (like <comment>Google/YoutubePluginBundle</comment>).', 'The vendor should be a "vendor" name like your company name', '(Google in the above example)', 'It should be camel cased with no spaces, and should NOT contain the words "Plugin"', 'or "Bundle" (these will be appended automatically)', ''));
         $acceptedVendor = false;
         while (!$acceptedVendor) {
             $vendor = $dialog->askAndValidate($output, $dialog->getQuestion('Vendor', $input->getOption('vendor')), function ($vendor) use($dialog, $output) {
                 return Validators::validateVendor($vendor);
             }, false, $input->getOption('vendor'));
             // mark as accepted, unless they want to try again below
             $acceptedVendor = true;
         }
         $input->setOption('vendor', $vendor);
     }
     // pluginName
     $pluginName = null;
     try {
         // validate the plugin name
         $pluginName = $input->getOption('plugin-name') ? Validators::validatePluginName($input->getOption('plugin-name')) : null;
     } catch (\Exception $error) {
         $output->writeln($dialog->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
     }
     if (null === $pluginName) {
         $output->writeln(array('', 'Your application code must be written in <comment>bundles</comment>. This command helps', 'you generate the bundle name and namespace easily.', '', 'Each bundle is hosted under a namespace (like <comment>Newscoop/YoutubePluginBundle</comment>).', 'The plugin name should be a project name, product name, or your client name.', 'It should be camel cased with no spaces, and should NOT contain the words "Plugin"', 'or "Bundle" (these will be appended automatically)', ''));
         $acceptedPluginName = false;
         while (!$acceptedPluginName) {
             $pluginName = $dialog->askAndValidate($output, $dialog->getQuestion('Plugin name', $input->getOption('plugin-name')), function ($pluginName) use($dialog, $output) {
                 return Validators::validatePluginName($pluginName);
             }, false, $input->getOption('plugin-name'));
             // mark as accepted, unless they want to try again below
             $acceptedPluginName = true;
         }
         $input->setOption('plugin-name', $pluginName);
     }
     // namespace
     $namespace = ucwords($vendor) . "/" . $pluginName . 'PluginBundle';
     $input->setOption('namespace', $namespace);
     // bundle name
     $bundle = ucwords($vendor) . $pluginName . 'PluginBundle';
     $input->setOption('bundle-name', $bundle);
     // target dir
     $dir = null;
     try {
         $dir = $input->getOption('dir') ? Validators::validateTargetDir($input->getOption('dir')) : null;
     } catch (\Exception $error) {
         $output->writeln($dialog->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
     }
     if (null === $dir) {
         $dir = dirname($this->getContainer()->getParameter('kernel.root_dir')) . '/plugins/' . ucwords($vendor);
         $output->writeln(array('', 'The bundle can be generated anywhere. The suggested default directory uses', 'the plugin directory for this Newscoop instance.', ''));
         $dir = $dialog->askAndValidate($output, $dialog->getQuestion('Target directory', $dir), function ($dir) use($bundle, $namespace) {
             return Validators::validateTargetDir($dir);
         }, false, $dir);
         $input->setOption('dir', $dir);
     }
     // format
     $format = null;
     try {
         $format = $input->getOption('format') ? Validators::validateFormat($input->getOption('format')) : null;
     } catch (\Exception $error) {
         $output->writeln($dialog->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
     }
     if (null === $format) {
         $output->writeln(array('', 'Determine the format to use for the generated configuration.', ''));
         $format = $dialog->askAndValidate($output, $dialog->getQuestion('Configuration format (yml, xml, php, or annotation)', $input->getOption('format')), array('Newscoop\\PluginGeneratorBundle\\Command\\Validators', 'validateFormat'), false, $input->getOption('format'));
         $input->setOption('format', $format);
     }
     // optional files to generate
     $output->writeln(array('', 'To help you get started faster, the command can generate some', 'code snippets for you.', ''));
     $structure = $input->getOption('structure');
     if (!$structure && $dialog->askConfirmation($output, $dialog->getQuestion('Do you want to generate the whole directory structure', 'no', '?'), false)) {
         $structure = true;
     }
     $input->setOption('structure', $structure);
     $admin = $input->getOption('admin');
     if (!$admin && $dialog->askConfirmation($output, $dialog->getQuestion('Do you want to generate an Admin structure', 'no', '?'), false)) {
         $admin = true;
     }
     $input->setOption('admin', $admin);
     $zip = $input->getOption('zip');
     if (!$zip && $dialog->askConfirmation($output, $dialog->getQuestion('Do you want to generate a private plugin zip', 'no', '?'), false)) {
         $zip = true;
     }
     $input->setOption('zip', $zip);
     // summary
     $output->writeln(array('', $this->getHelper('formatter')->formatBlock('Summary before generation', 'bg=blue;fg=white', true), '', sprintf("You are going to generate a \"<info>%s\\%s</info>\" bundle\nin \"<info>%s</info>\" using the \"<info>%s</info>\" format.", $namespace, $bundle, $dir, $format), ''));
 }