Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. php
  2. // should return: 'Do Androids Dream of Electric Sheep?'
  3. ->titleCase('do androids dream of electric SHEEP?', array('do', 'an', 'of'));
  4.  
  5. php
  6. //should return: 'Rendezvous with Rama'
  7. ->titleCase('RENDEZVOUS WITH RAMA', array('With'));
  8.  
  9. php
  10. // should return: 'Stranger In A Strange Land'
  11. ->titleCase('stranger in a strange land');
  12.  
  13. class StringUtil {
  14. /**
  15. * Verify a string of braces
  16. *
  17. * Takes a string of braces as a single parameter and determines if the order of the braces is
  18. * valid.
  19. *
  20. * @access public
  21. * @param String $braces
  22. * @return Boolean
  23. * @throws InvalidArgumentException If $braces is not a string
  24. */
  25. public function validBraces($braces = '') {
  26. /**
  27. * I assume that empty string is valid because
  28. * is the default value
  29. * and you have in the test as valid
  30. */
  31. if ($braces === '') {
  32. return true;
  33. } elseif (empty($braces)) {
  34. return false;
  35. }
  36. /**
  37. * array of chars allowed,
  38. * the keys are the open chars
  39. * the values are the closure
  40. */
  41. $symbols = array("(" => ")", "{" => "}", "[" => "]");
  42.  
  43. // get array with all characters allowed
  44. $search = array_merge(array_keys($symbols), array_values($symbols));
  45.  
  46. //check valid string
  47. if (strlen(str_replace($search, '', $braces)) > 0) {
  48. throw new InvalidArgumentException("ERROR: Invalid Arguments. Just (){}[] characters allowed in the string");
  49. }
  50.  
  51. //check if the length is even
  52. if (strlen($braces) % 2 !== 0) {
  53. return false;
  54. }
  55.  
  56. // Make sure start & end chars are not incorrect
  57. if (in_array(substr($braces, 0, 1), $symbols) || array_key_exists(substr($braces, -1, 1), $symbols)) {
  58. return false;
  59. }
  60.  
  61. //Make sure the string have the same amount of open & end chars
  62. foreach ($symbols as $key => $value) {
  63. if (substr_count($braces, $key) !== substr_count($braces, $value)) {
  64. return false;
  65. }
  66. }
  67.  
  68. $bracesArray = str_split($braces);
  69. for ($i = 0; $i <= count($bracesArray) - 1; $i++) {
  70. if (in_array($bracesArray[$i], $symbols)) {
  71. if ($bracesArray[$i] === $symbols[$bracesArray[$i - 1]]) {
  72. unset($bracesArray[$i], $bracesArray[$i - 1]);
  73. break;
  74. } else {
  75. return false;
  76. }
  77. }
  78. }
  79.  
  80. if (!empty($bracesArray)) {
  81. $newBraces = implode("", $bracesArray);
  82. return $this->validBraces($newBraces);
  83. }
  84.  
  85. return true;
  86. }
  87.  
  88. /**
  89. * Convert a string into title case given an optional list of exceptions.
  90. *
  91. * @access public
  92. * @param String $title
  93. * @param Array $exceptions
  94. * @return String
  95. */
  96. public function titleCase($title, $exceptions = array()) {
  97. $newExecption = array();
  98. foreach ($exceptions as $execptionV) {
  99. $newExecption[] = strtolower($execptionV);
  100. }
  101. $stringArray = explode(' ', $title);
  102. foreach ($stringArray as $key => $value) {
  103. if (empty($newExecption)) {
  104. $new[] = ucfirst(strtolower($value));
  105. } else {
  106. if ($key == 0) {
  107. $new[] = ucfirst(strtolower($value));
  108. } else {
  109. if (!in_array(strtolower($value), $newExecption)) {
  110. $new[] = ucfirst(strtolower($value));
  111. } else {
  112. $new[] = strtolower($value);
  113. }
  114. }
  115. }
  116. }
  117. $newPhrase = implode(" ", $new);
  118. return $newPhrase;
  119. }
  120.  
  121. }
  122.  
  123. public function validBraces() {
  124. return array(
  125. array('()'),
  126. array('{}'),
  127. array('[]'),
  128. array('(){}[]'),
  129. array('([{}])'),
  130. array('{}({})[]'),
  131. array('({})[({})]'),
  132. array('(({{[[]]}}))'),
  133. array('')
  134. );
  135. }
  136.  
  137.  
  138. public function invalidBraces() {
  139. return array(
  140. array('(}'),
  141. array('[(])'),
  142. array('(})'),
  143. array(')(}{]['),
  144. array('())({}}{()][]['),
  145. array('(((({{'),
  146. array('}}]]))}])')
  147. );
  148. }
  149.  
  150. public function testDefault() {
  151. $this->assertSame('Abc Def Ghi', $this->util->titleCase('aBC deF Ghi'));
  152. }
  153.  
  154. public function testFirstWord() {
  155. $this->assertSame('Ab', $this->util->titleCase('ab', array('ab')));
  156. }
  157.  
  158. public function testExceptionWord() {
  159. $this->assertSame('A bc', $this->util->titleCase('a bc', array('bc')));
  160. }
  161.  
  162. public function testExceptionWordIgnoresCase() {
  163. $this->assertSame('A bc', $this->util->titleCase('a bc', array('BC')));
  164. }
  165.  
  166. public function testBogusExceptionWords() {
  167. $this->assertSame('First A Of In', $this->util->titleCase(
  168. 'First a of in', array('an', 'often', 'into')
  169. ));
  170. }
  171.  
  172. public function testTitles() {
  173. $this->assertSame('A Clash of Kings', $this->util->titleCase(
  174. 'a clash of KINGS', array('a', 'an', 'the', 'OF')
  175. ));
  176.  
  177. $this->assertSame('The quick Brown fox', $this->util->titleCase(
  178. 'the QUICK bRoWn fOX', array('xyz', 'fox', 'quick', 'the')
  179. ));
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement