/** 
  * Numbers a street address accordingly
  *
  * A street address contains a numeric street address in addition to, potentially,
  * a numeric street name (e.g., "123 101st st"). The street name can be a number 
  * ("101"), an ordinal (e.g., "101st"), or a cardinal (e.g., "one hundred and 
  * first"). Ultimately, all of those should resolve to the same norm.
  *
  * For example:
  * 
  *     self::number("123 101 st");                    // returns "123 101 st"
  *     self::number("123 101st st");                  // returns "123 101 st"
  *     self::number("123 one hundred first st");      // returns "123 101 st"
  *     self::number("123 one hundred and first st");  // returns "123 101 st"
  *
  * @since  0.1.0
  * @param  string  $street  the street address to number
  * @return  string
  */
 protected static function number($street)
 {
     // validate street
     if ($street === null || !is_string($street)) {
         throw new \InvalidArgumentException(__METHOD__ . "() expects parameter one to be a string street address");
     }
     // break $street into words
     $words = explode(' ', $street);
     // get the numeric value of each word (or zero if the word is not a number)
     // for example, ["123", "one", "hundred", "and", "first", "st"] will become
     //     [123, 1, 100, 0, 1, 0]
     //
     $numbers = array_map(function ($v) {
         return \Jstewmc\PhpHelpers\Num::val($v);
     }, $words);
     // if the street address has numeric words
     // for example, in the example above, there are three "number words" ("one",
     //     "hundred", and "first")
     //
     $hasNumerics = count(array_diff(array_filter($numbers), $words));
     if ($hasNumerics > 0) {
         // we're going to replace the numeric words with their value
         // for example, ["123", "one", "hundred", "and", "first", "st"] becomes
         //     ["123", "101", "st"]
         //
         $newWords = array();
         // loop through the words and determine exactly which words need replacing
         // a word needs replacing if the word's numeric value is not zero and it isn't
         //     already a number in the address, it needs to be replaced
         //
         $numerics = array();
         foreach ($words as $k => $word) {
             $numerics[] = $numbers[$k] !== 0 && $numbers[$k] != $word;
         }
         // loop through the words (again)
         $number = array();
         foreach ($words as $k => $word) {
             // if the word is a "number word", add it to the number
             // otherwise, if the word is "and", the number isn't empty, and the next word
             //    is a number word, add it to the number
             // finally, the word is not a number word and the number is finished
             //
             if ($numerics[$k]) {
                 $number[] = $word;
             } elseif ($word === 'and' && !empty($number) && array_key_exists($k + 1, $numerics) && $numerics[$k + 1]) {
                 $number[] = $word;
             } else {
                 // if a number exists it's complete
                 // get the number's value, append it to new words, and reset the number
                 //
                 if (!empty($number)) {
                     $number = implode(' ', $number);
                     $newWords[] = \Jstewmc\PhpHelpers\Num::val($number);
                     $number = array();
                 }
                 $newWords[] = $word;
             }
         }
         // glue the street address back together again
         $street = implode(' ', $newWords);
     }
     return $street;
 }
Beispiel #2
0
 /**
  * val() should return int if var is a long-length integer name
  */
 public function testVal_returnsInt_ifVarIsLongName()
 {
     return $this->assertEquals(Num::val('one million four hundred thirty-seven thousand five hundred twenty-two'), 1437522);
 }
Beispiel #3
0
 /**
  * Returns true if $key does not exist in $array or $array[$key] is empty
  *
  * PHP's isset() method is will return false if the key does not exist or if the 
  * key exists and its value is null. However, it will return true if the key
  * exists and its value is not null (including other "empty" values like '', false
  * and array()). 
  *
  * PHP's empty() method (or some frameworks) will throw a warning if you attempt
  * to test a non-existant key in an array.
  *
  * I, on the other hand, will return false if the key does not exist in the array
  * or if the key's value is empty.
  *
  * For example:
  *
  *     $a = ['foo' => null, 'bar' => array(), 'qux' => 'hello'];
  * 
  *     // when key doesn't exist (!)
  *     isset($a['quux']);           // returns false
  *     ! empty($a['quux']);         // throws key-does-not-exist warning
  *     ! Arr::isEmpty('quux', $a);  // returns false
  *
  *     // when key does exist, but value is null
  *     isset($a['foo']);           // returns false
  *     ! empty($a['foo']);         // returns false
  *     ! Arr::isEmpty('foo', $a);  // returns false
  *
  *     // when key does exist, but value is "empty" (!)
  *     isset($a['bar']);           // returns true
  *     ! empty($a['bar']);         // returns false
  *     ! Arr::isEmpty('bar', $a);  // returns false
  *
  *     // when key does exist, but value is not "empty"
  *     isset($a['qux']);           // returns true
  *     ! empty($a['qux']);         // returns true
  *     ! Arr::isEmpty('qux', $a);  // returns true
  * 
  * @since  0.1.0
  *
  * @param  string  $key          the key's name
  * @param  array   $array        the array to test
  * @param  bool    $isZeroEmpty  a flag indicating whether or not zero is
  *    considered empty (optional; if omitted, defaults to true - i.e., the
  *    default behavior of PHP's empty() function )
  *
  * @return  bool  true if the key exists and its value is not empty
  *
  * @throws  \BadMethodCallException    if $key or $array are null
  * @throws  \InvalidArgumentException  if $key is not a string
  * @throws  \InvalidArgumentException  if $array is not an array
  * @throws  \InvalidArgumentException  if $isZeroEmpty is not a bool value
  */
 public static function isEmpty($key, $array, $isZeroEmpty = true)
 {
     $isEmpty = true;
     // if $key and array are given
     if ($key !== null && $array !== null) {
         // if $key is a string
         if (is_string($key)) {
             // if $array is an array
             if (is_array($array)) {
                 // if $zero is a bool value
                 if (is_bool($isZeroEmpty)) {
                     // if $array is not empty
                     if (!empty($array)) {
                         // if the key exists
                         if (array_key_exists($key, $array)) {
                             $isEmpty = empty($array[$key]);
                             // if the value is "empty" but zero is not considered empty
                             if ($isEmpty && !$isZeroEmpty) {
                                 // if the value is zero it is not empty
                                 $isEmpty = !\Jstewmc\PhpHelpers\Num::isZero($array[$key]);
                             }
                         }
                     }
                 } else {
                     throw new \InvalidArgumentException(__METHOD__ . "() expects parameter three, allow zeros, to be a bool");
                 }
             } else {
                 throw new \InvalidArgumentException(__METHOD__ . "() expects parameter two, array, to be an array");
             }
         } else {
             throw new \InvalidArgumentException(__METHOD__ . "() expects parameter one, key, to be a string key name");
         }
     } else {
         throw new \BadMethodCallException(__METHOD__ . "() expects two parameters, a string key name and an array");
     }
     return $isEmpty;
 }