Skip to content

Commit ffb3e6f

Browse files
committed
Input arguments and options
1 parent 081445f commit ffb3e6f

File tree

6 files changed

+153
-86
lines changed

6 files changed

+153
-86
lines changed

config/services.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,3 @@ services:
2222

2323
# add more service definitions when explicit configuration is needed
2424
# please note that last definitions always *replace* previous ones
25-
26-
App\Command\FooQuxCommand:
27-
tags:
28-
- { name: 'console.command', command: 'foo:qux' }
29-

src/Command/FooBarCommand.php

-29
This file was deleted.

src/Command/FooBazCommand.php

-27
This file was deleted.

src/Command/FooQuxCommand.php

-25
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Command;
6+
7+
use App\Value\Lipsum;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Input\InputOption;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
14+
final class VariableNameGeneratorCommand extends Command
15+
{
16+
protected static $defaultName = 'generate:variable';
17+
18+
private array $validCases = ['camel', 'snake', 'kebab'];
19+
20+
protected function configure()
21+
{
22+
$this->setDescription('Generate variable names');
23+
$this->addArgument('word-count', InputArgument::REQUIRED, 'Word count');
24+
$this->addArgument('variable-count', InputArgument::OPTIONAL, 'Variable count');
25+
$this->addOption('case', 'c', InputOption::VALUE_OPTIONAL, 'Change case', 'camel');
26+
}
27+
28+
protected function execute(InputInterface $input, OutputInterface $output): int
29+
{
30+
$wordCount = $input->getArgument('word-count');
31+
if (!is_numeric($wordCount)) {
32+
$output->writeln('Word count must be numeric!');
33+
34+
return Command::INVALID;
35+
}
36+
37+
$variableCount = $input->getArgument('variable-count');
38+
if (!is_numeric($variableCount)) {
39+
$variableCount = 1;
40+
}
41+
42+
$case = $input->getOption('case');
43+
if (!in_array($case, $this->validCases)) {
44+
$case = 'camel';
45+
}
46+
47+
$lipsum = new Lipsum();
48+
49+
$output->writeln('Here are some variables:');
50+
$output->writeln('');
51+
52+
for ($i = 0; $i < $variableCount; $i++) {
53+
$words = $lipsum->getWords($wordCount);
54+
$output->writeln($this->formatCase($words, $case));
55+
}
56+
57+
return Command::SUCCESS;
58+
}
59+
60+
private function formatCase(array $words, string $case): string
61+
{
62+
$formattedVariable = '';
63+
if ($case === 'snake') {
64+
foreach ($words as $key => $word) {
65+
if ($key !== 0) {
66+
$formattedVariable = $formattedVariable . '_' . $word;
67+
} else {
68+
$formattedVariable = $word;
69+
}
70+
}
71+
} elseif ($case === 'kebab') {
72+
foreach ($words as $key => $word) {
73+
if ($key !== 0) {
74+
$formattedVariable = $formattedVariable . '-' . $word;
75+
} else {
76+
$formattedVariable = $word;
77+
}
78+
}
79+
} else {
80+
foreach ($words as $key => $word) {
81+
if ($key !== 0) {
82+
$word = ucfirst($word);
83+
}
84+
$formattedVariable = $formattedVariable . $word;
85+
}
86+
}
87+
88+
return $formattedVariable;
89+
}
90+
}

src/Value/Lipsum.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Value;
6+
7+
final class Lipsum
8+
{
9+
private $words = [
10+
'lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit',
11+
'a', 'ac', 'accumsan', 'ad', 'aenean', 'aliquam', 'aliquet', 'ante',
12+
'aptent', 'arcu', 'at', 'auctor', 'augue', 'bibendum', 'blandit',
13+
'class', 'commodo', 'condimentum', 'congue', 'consequat', 'conubia',
14+
'convallis', 'cras', 'cubilia', 'curabitur', 'curae', 'cursus',
15+
'dapibus', 'diam', 'dictum', 'dictumst', 'dignissim', 'dis', 'donec',
16+
'dui', 'duis', 'efficitur', 'egestas', 'eget', 'eleifend', 'elementum',
17+
'enim', 'erat', 'eros', 'est', 'et', 'etiam', 'eu', 'euismod', 'ex',
18+
'facilisi', 'facilisis', 'fames', 'faucibus', 'felis', 'fermentum',
19+
'feugiat', 'finibus', 'fringilla', 'fusce', 'gravida', 'habitant',
20+
'habitasse', 'hac', 'hendrerit', 'himenaeos', 'iaculis', 'id',
21+
'imperdiet', 'in', 'inceptos', 'integer', 'interdum', 'justo',
22+
'lacinia', 'lacus', 'laoreet', 'lectus', 'leo', 'libero', 'ligula',
23+
'litora', 'lobortis', 'luctus', 'maecenas', 'magna', 'magnis',
24+
'malesuada', 'massa', 'mattis', 'mauris', 'maximus', 'metus', 'mi',
25+
'molestie', 'mollis', 'montes', 'morbi', 'mus', 'nam', 'nascetur',
26+
'natoque', 'nec', 'neque', 'netus', 'nibh', 'nisi', 'nisl', 'non',
27+
'nostra', 'nulla', 'nullam', 'nunc', 'odio', 'orci', 'ornare',
28+
'parturient', 'pellentesque', 'penatibus', 'per', 'pharetra',
29+
'phasellus', 'placerat', 'platea', 'porta', 'porttitor', 'posuere',
30+
'potenti', 'praesent', 'pretium', 'primis', 'proin', 'pulvinar',
31+
'purus', 'quam', 'quis', 'quisque', 'rhoncus', 'ridiculus', 'risus',
32+
'rutrum', 'sagittis', 'sapien', 'scelerisque', 'sed', 'sem', 'semper',
33+
'senectus', 'sociosqu', 'sodales', 'sollicitudin', 'suscipit',
34+
'suspendisse', 'taciti', 'tellus', 'tempor', 'tempus', 'tincidunt',
35+
'torquent', 'tortor', 'tristique', 'turpis', 'ullamcorper', 'ultrices',
36+
'ultricies', 'urna', 'ut', 'varius', 'vehicula', 'vel', 'velit',
37+
'venenatis', 'vestibulum', 'vitae', 'vivamus', 'viverra', 'volutpat',
38+
'vulputate',
39+
];
40+
41+
public function getWords($count = 1): array
42+
{
43+
$count = (int) $count;
44+
$words = array();
45+
$word_count = 0;
46+
47+
while ($word_count < $count) {
48+
$shuffle = true;
49+
50+
while ($shuffle) {
51+
shuffle($this->words);
52+
53+
if (!$word_count || $words[$word_count - 1] != $this->words[0]) {
54+
$words = array_merge($words, $this->words);
55+
$word_count = count($words);
56+
$shuffle = false;
57+
}
58+
}
59+
}
60+
61+
return array_slice($words, 0, $count);
62+
}
63+
}

0 commit comments

Comments
 (0)