Exemple #1
0
 function testImplodeRange()
 {
     $pieces = array('Zero', 'One', 'Two', 'Three', 'Four', 'Five');
     $result = ArrayUtility::implodeRange(' ', $pieces, 0, 5);
     $this->assertIdentical($result, 'Zero One Two Three Four Five');
     $result = ArrayUtility::implodeRange(' ', $pieces, 0, 4);
     $this->assertIdentical($result, 'Zero One Two Three Four');
     $result = ArrayUtility::implodeRange(' ', $pieces, 1, 4);
     $this->assertIdentical($result, 'One Two Three Four');
     $result = ArrayUtility::implodeRange(' ', $pieces, 1, 3);
     $this->assertIdentical($result, 'One Two Three');
     $result = ArrayUtility::implodeRange(' ', $pieces, 2, 3);
     $this->assertIdentical($result, 'Two Three');
     $result = ArrayUtility::implodeRange(' ', $pieces, 2, 2);
     $this->assertIdentical($result, 'Two');
     $result = ArrayUtility::implodeRange(' ', $pieces, 0, 6);
     $this->assertIdentical($result, 'Zero One Two Three Four Five');
     $result = ArrayUtility::implodeRange(' ', $pieces, -500, 500);
     $this->assertIdentical($result, 'Zero One Two Three Four Five');
     $result = ArrayUtility::implodeRange(', ', $pieces, -500, 500);
     $this->assertIdentical($result, 'Zero, One, Two, Three, Four, Five');
 }
 protected function _getCityStateZipArray($cityStateZipLine)
 {
     /* Safe default values. */
     $city = '';
     $state = '';
     $zip = '';
     /* Count the number of "words" / tokens in the line. */
     $tokenCount = StringUtility::countTokens(";, \t", $cityStateZipLine);
     if ($tokenCount < 2) {
         return array('city' => '', 'state' => '', 'zip' => '');
     }
     /* Split the string into an array of tokens. */
     $tokens = StringUtility::tokenize(";, \t", $cityStateZipLine);
     if ($tokenCount == 3) {
         $city = $tokens[0];
         $state = $tokens[1];
         $zip = $tokens[2];
     } else {
         /* If we have a known two- or three-word state, recognize it. */
         $twoWordState = ArrayUtility::implodeRange(' ', $tokens, $tokenCount - 3, $tokenCount - 2);
         $threeWordState = ArrayUtility::implodeRange(' ', $tokens, $tokenCount - 4, $tokenCount - 2);
         /* Known two- and three- word states / provinces. */
         $twoWordStates = array('New Hampshire', 'New York', 'New Jersey', 'New Mexico', 'North Dakota', 'North Carolina', 'South Dakota', 'South Carolina', 'West Virginia', 'Rhode Island', 'American Samoa', 'Puerto Rico', 'British Columbia', 'New Brunswick', 'Nova Scotia', 'Northwest Territories', 'South Dekota', 'North Dekota', 'Rode Island', 'New Hamshire');
         $threeWordStates = array('District Of Columbia', 'Prince Edward Island');
         /* Do we have a two-word state? */
         if (in_array(ucwords($twoWordState), $twoWordStates)) {
             /* Yes; assume that the last token is the zip code, the two
              * proceeding tokens before are part of the state, and all
              * other preceding tokens are part of the city.
              */
             $city = ArrayUtility::implodeRange(' ', $tokens, 0, $tokenCount - 4);
             $state = $twoWordState;
             $zip = $tokens[$tokenCount - 1];
         } else {
             if ($tokenCount > 4 && in_array(ucwords($threeWordState), $threeWordStates)) {
                 /* Yes; assume that the last token is the zip code, the
                  * three proceeding tokens before are part of the state,
                  * and all other preceding tokens are part of the city.
                  */
                 $city = ArrayUtility::implodeRange(' ', $tokens, 0, $tokenCount - 5);
                 $state = $threeWordState;
                 $zip = $tokens[$tokenCount - 1];
             } else {
                 /* Assume that the last token is the zip code, the token before
                  * the last token is the state, and all other preceding tokens
                  * are part of the city.
                  */
                 $city = ArrayUtility::implodeRange(' ', $tokens, 0, $tokenCount - 3);
                 $state = $tokens[$tokenCount - 2];
                 $zip = $tokens[$tokenCount - 1];
             }
         }
     }
     /* Regular expression to match US state abbreviations. */
     $USStateABBR = 'A[AEKLPRSZ]|C[AOT]|D[CE]|F[LM]|G[AU]|HI|I[ADLN]' . '|K[SY]|LA|M[ADEHINOPST]|N[CDEH]|N[JMVY]|O[HKR]|P[ARW]|RI' . '|S[CD]|T[NX]|UT|V[AIT]|W[AIVY]';
     /* If the state is a United States Postal Service state abbreviation,
      * we can apply additional formatting.
      */
     if (!empty($state) && strlen($state) == 3 && preg_match('/^(' . $USStateABBR . ')\\.$/i', $state)) {
         $state = strtoupper(substr($state, 0, 2));
     }
     /* Common spelling corrections. */
     $search = array('South Dekota', 'North Dekota', 'Rode Island', 'New Hamshire');
     $replace = array('South Dakota', 'North Dakota', 'Rhode Island', 'New Hampshire');
     $state = str_replace($search, $replace, $state);
     // FIXME: Convert US state names to abbreviations.
     // FIXME: Proper title case.
     return array('city' => ucwords($city), 'state' => ucwords($state), 'zip' => strtoupper($zip));
 }