예제 #1
0
 /**
  * isZero() should return true if number is zero
  */
 public function testIsZero_returnsTrue_ifNumberIsZero()
 {
     return $this->assertTrue(Num::isZero(0));
 }
예제 #2
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;
 }