InflectShell.php 5.7 KB

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