Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- php
- // should return: 'Do Androids Dream of Electric Sheep?'
- ->titleCase('do androids dream of electric SHEEP?', array('do', 'an', 'of'));
- php
- //should return: 'Rendezvous with Rama'
- ->titleCase('RENDEZVOUS WITH RAMA', array('With'));
- php
- // should return: 'Stranger In A Strange Land'
- ->titleCase('stranger in a strange land');
- class StringUtil {
- /**
- * Verify a string of braces
- *
- * Takes a string of braces as a single parameter and determines if the order of the braces is
- * valid.
- *
- * @access public
- * @param String $braces
- * @return Boolean
- * @throws InvalidArgumentException If $braces is not a string
- */
- public function validBraces($braces = '') {
- /**
- * I assume that empty string is valid because
- * is the default value
- * and you have in the test as valid
- */
- if ($braces === '') {
- return true;
- } elseif (empty($braces)) {
- return false;
- }
- /**
- * array of chars allowed,
- * the keys are the open chars
- * the values are the closure
- */
- $symbols = array("(" => ")", "{" => "}", "[" => "]");
- // get array with all characters allowed
- $search = array_merge(array_keys($symbols), array_values($symbols));
- //check valid string
- if (strlen(str_replace($search, '', $braces)) > 0) {
- throw new InvalidArgumentException("ERROR: Invalid Arguments. Just (){}[] characters allowed in the string");
- }
- //check if the length is even
- if (strlen($braces) % 2 !== 0) {
- return false;
- }
- // Make sure start & end chars are not incorrect
- if (in_array(substr($braces, 0, 1), $symbols) || array_key_exists(substr($braces, -1, 1), $symbols)) {
- return false;
- }
- //Make sure the string have the same amount of open & end chars
- foreach ($symbols as $key => $value) {
- if (substr_count($braces, $key) !== substr_count($braces, $value)) {
- return false;
- }
- }
- $bracesArray = str_split($braces);
- for ($i = 0; $i <= count($bracesArray) - 1; $i++) {
- if (in_array($bracesArray[$i], $symbols)) {
- if ($bracesArray[$i] === $symbols[$bracesArray[$i - 1]]) {
- unset($bracesArray[$i], $bracesArray[$i - 1]);
- break;
- } else {
- return false;
- }
- }
- }
- if (!empty($bracesArray)) {
- $newBraces = implode("", $bracesArray);
- return $this->validBraces($newBraces);
- }
- return true;
- }
- /**
- * Convert a string into title case given an optional list of exceptions.
- *
- * @access public
- * @param String $title
- * @param Array $exceptions
- * @return String
- */
- public function titleCase($title, $exceptions = array()) {
- $newExecption = array();
- foreach ($exceptions as $execptionV) {
- $newExecption[] = strtolower($execptionV);
- }
- $stringArray = explode(' ', $title);
- foreach ($stringArray as $key => $value) {
- if (empty($newExecption)) {
- $new[] = ucfirst(strtolower($value));
- } else {
- if ($key == 0) {
- $new[] = ucfirst(strtolower($value));
- } else {
- if (!in_array(strtolower($value), $newExecption)) {
- $new[] = ucfirst(strtolower($value));
- } else {
- $new[] = strtolower($value);
- }
- }
- }
- }
- $newPhrase = implode(" ", $new);
- return $newPhrase;
- }
- }
- public function validBraces() {
- return array(
- array('()'),
- array('{}'),
- array('[]'),
- array('(){}[]'),
- array('([{}])'),
- array('{}({})[]'),
- array('({})[({})]'),
- array('(({{[[]]}}))'),
- array('')
- );
- }
- public function invalidBraces() {
- return array(
- array('(}'),
- array('[(])'),
- array('(})'),
- array(')(}{]['),
- array('())({}}{()][]['),
- array('(((({{'),
- array('}}]]))}])')
- );
- }
- public function testDefault() {
- $this->assertSame('Abc Def Ghi', $this->util->titleCase('aBC deF Ghi'));
- }
- public function testFirstWord() {
- $this->assertSame('Ab', $this->util->titleCase('ab', array('ab')));
- }
- public function testExceptionWord() {
- $this->assertSame('A bc', $this->util->titleCase('a bc', array('bc')));
- }
- public function testExceptionWordIgnoresCase() {
- $this->assertSame('A bc', $this->util->titleCase('a bc', array('BC')));
- }
- public function testBogusExceptionWords() {
- $this->assertSame('First A Of In', $this->util->titleCase(
- 'First a of in', array('an', 'often', 'into')
- ));
- }
- public function testTitles() {
- $this->assertSame('A Clash of Kings', $this->util->titleCase(
- 'a clash of KINGS', array('a', 'an', 'the', 'OF')
- ));
- $this->assertSame('The quick Brown fox', $this->util->titleCase(
- 'the QUICK bRoWn fOX', array('xyz', 'fox', 'quick', 'the')
- ));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement