InflectCommand.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Tools\Command;
  4. use Cake\Command\Command;
  5. use Cake\Console\Arguments;
  6. use Cake\Console\ConsoleIo;
  7. use Cake\Console\ConsoleOptionParser;
  8. use Shim\Utility\Inflector;
  9. /**
  10. * Inflect the heck out of your words.
  11. *
  12. * @author Jose Diaz-Gonzalez
  13. * @author Mark Scherer
  14. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  15. */
  16. class InflectCommand extends Command {
  17. /**
  18. * Valid inflection rules
  19. *
  20. * @var array<string>
  21. */
  22. protected array $validActions = [
  23. 'pluralize', 'singularize', 'camelize',
  24. 'underscore', 'humanize', 'tableize',
  25. 'classify', 'variable', 'dasherize',
  26. ];
  27. /**
  28. * Valid inflection rules
  29. *
  30. * @var array<string>
  31. */
  32. protected array $validCommands = [
  33. 'pluralize', 'singularize', 'camelize',
  34. 'underscore', 'humanize', 'tableize',
  35. 'classify', 'variable', 'dasherize', 'all', 'quit',
  36. ];
  37. /**
  38. * Hook action for defining this command's option parser.
  39. *
  40. * @see https://book.cakephp.org/4/en/console-commands/commands.html#defining-arguments-and-options
  41. * @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
  42. * @return \Cake\Console\ConsoleOptionParser The built parser.
  43. */
  44. public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser {
  45. $parser = parent::buildOptionParser($parser);
  46. $parser->addArgument('word');
  47. $parser->addArgument('action');
  48. return $parser;
  49. }
  50. /**
  51. * Implement this action with your command's logic.
  52. *
  53. * @param \Cake\Console\Arguments $args The command arguments.
  54. * @param \Cake\Console\ConsoleIo $io The console io
  55. * @return int|null|void The exit code or null for success
  56. */
  57. public function execute(Arguments $args, ConsoleIo $io) {
  58. if ($args->getArguments()) {
  59. $arguments = $this->_parseArguments($args->getArguments(), $io);
  60. } else {
  61. $arguments = $this->_interactive($io);
  62. }
  63. if (!$arguments['action'] || !$arguments['word']) {
  64. return static::CODE_SUCCESS;
  65. }
  66. $this->_inflect($arguments['action'], $arguments['word'], $io);
  67. }
  68. /**
  69. * Prompts the user for word
  70. *
  71. * @param \Cake\Console\ConsoleIo $io
  72. *
  73. * @return array
  74. */
  75. protected function _interactive(ConsoleIo $io) {
  76. $word = $this->_getWord($io);
  77. $action = $this->_getAction($io);
  78. return ['action' => $action, 'word' => $word];
  79. }
  80. /**
  81. * Requests a valid inflection action
  82. *
  83. * @param \Cake\Console\ConsoleIo $io
  84. *
  85. * @return string
  86. */
  87. protected function _getAction(ConsoleIo $io) {
  88. $validCharacters = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'a', 'q'];
  89. $validCommands = array_merge($validCharacters, $this->validCommands);
  90. $command = null;
  91. while (empty($command)) {
  92. $io->out('Please type the number or name of the inflection you would like to use');
  93. $io->hr();
  94. $io->out('[1] Pluralize');
  95. $io->out('[2] Singularize');
  96. $io->out('[3] Camelize');
  97. $io->out('[4] Underscore');
  98. $io->out('[5] Humanize');
  99. $io->out('[6] Tableize');
  100. $io->out('[7] Classify');
  101. $io->out('[8] Variable');
  102. $io->out('[9] Dasherize');
  103. $io->out('[a] All');
  104. $io->out('[q] Quit');
  105. $answer = $io->ask('What action would you like to perform?', 'q');
  106. if (in_array(strtolower($answer), $validCommands, true)) {
  107. $command = strtolower($answer);
  108. } else {
  109. $io->out('Try again.');
  110. }
  111. }
  112. switch ($command) {
  113. case '1':
  114. case 'pluralize':
  115. return 'pluralize';
  116. case '2':
  117. case 'singularize':
  118. return 'singularize';
  119. case '3':
  120. case 'camelize':
  121. return 'camelize';
  122. case '4':
  123. case 'underscore':
  124. return 'underscore';
  125. case '5':
  126. case 'humanize':
  127. return 'humanize';
  128. case '6':
  129. case 'tableize':
  130. return 'tableize';
  131. case '7':
  132. case 'classify':
  133. return 'classify';
  134. case '8':
  135. case 'variable':
  136. return 'variable';
  137. case '9':
  138. case 'dasherize':
  139. return 'dasherize';
  140. case 'a':
  141. case 'all':
  142. return 'all';
  143. case 'q':
  144. case 'quit':
  145. default:
  146. $this->abort(static::CODE_SUCCESS);
  147. }
  148. }
  149. /**
  150. * Requests word to inflect
  151. *
  152. * @param \Cake\Console\ConsoleIo $io
  153. *
  154. * @return string|null
  155. */
  156. protected function _getWord(ConsoleIo $io) {
  157. $word = null;
  158. while (empty($word)) {
  159. $temp = $io->ask('What word would you like to inflect?');
  160. if (!empty($temp)) {
  161. $word = $temp;
  162. } else {
  163. $io->out('Try again.');
  164. }
  165. }
  166. return $word;
  167. }
  168. /**
  169. * Parse the arguments into the function and the word to be inflected
  170. *
  171. * @param array $arguments
  172. * @param \Cake\Console\ConsoleIo $io
  173. *
  174. * @return array
  175. */
  176. protected function _parseArguments($arguments, ConsoleIo $io) {
  177. $word = array_shift($arguments);
  178. if (!$arguments) {
  179. $action = $this->_getAction($io);
  180. } else {
  181. $action = array_shift($arguments);
  182. }
  183. return ['action' => $action, 'word' => $word];
  184. }
  185. /**
  186. * Inflects a set of word based upon the inflection set in the arguments
  187. *
  188. * @param string $function
  189. * @param string $word
  190. * @param \Cake\Console\ConsoleIo $io
  191. *
  192. * @return void
  193. */
  194. protected function _inflect($function, $word, ConsoleIo $io) {
  195. $io->out($word);
  196. if ($function === 'all') {
  197. foreach ($this->validActions as $action) {
  198. $functionName = $this->_getMessage($action);
  199. $io->out("{$functionName}: " . Inflector::$action($word));
  200. }
  201. } else {
  202. $functionName = $this->_getMessage($function);
  203. if (!$functionName) {
  204. $io->error('Action does not exist');
  205. }
  206. $io->out("{$functionName}: " . Inflector::$function($word));
  207. }
  208. }
  209. /**
  210. * Returns the appropriate message for a given function
  211. *
  212. * @param string $function
  213. * @return string|null
  214. */
  215. protected function _getMessage($function) {
  216. $messages = [
  217. 'camelize' => 'CamelCase form ',
  218. 'camelBacked' => 'camelBacked form ',
  219. 'classify' => 'Cake Model Class form ',
  220. 'humanize' => 'Human Readable Group form ',
  221. 'singularize' => 'Singular form ',
  222. 'dasherize' => 'Dasherized-form ',
  223. 'pluralize' => 'Pluralized form ',
  224. 'tableize' => 'table_names form ',
  225. 'underscore' => 'under_scored_form ',
  226. 'variable' => 'variableForm ',
  227. ];
  228. return $messages[$function] ?? null;
  229. }
  230. }