示例#1
0
 /**
  * endsWith() should return false if the haystack does not end with needle
  */
 public function testEndsWith_returnsFalse_ifHaystackDoesNotEndWithNeedle()
 {
     return $this->assertFalse(Str::endsWith('foobar', 'baz'));
 }
示例#2
0
 /**
  * Wildcard search for a value in an array 
  *
  * I'll search $haystack for $needle. Unlike PHP's native in_array() method,
  * I'll accept begins-with (e.g., "foo*"), ends-with (e.g., "*foo"), and 
  * contains (e.g., "*foo*") wildcard notation.
  *
  * For example:
  *
  *     Arr::inArray('foo', ['foo', 'bar']);  // returns true
  *     Arr::inArray('qux', ['foo', 'bar']);  // returns false
  *     Arr::inArray('fo*', ['foo', 'bar']);  // returns true
  *     Arr::inArray('*oo', ['foo', 'bar']);  // returns true
  *     Arr::inArray('*o*', ['foo', 'bar']);  // returns true
  * 
  * @since  0.1.0
  *
  * @param  string    $needle    the needle to find
  * @param  string[]  $haystack  the haystack to search
  * @param  string    $wildcard  the wildcard character (optional; if omitted, 
  *    defaults to '*')
  *
  * @return  bool  true if the needle exists in haystack
  *
  * @throws  \BadMethodCallException    if $needle, $haystack, or $wildcard is null
  * @throws  \InvalidArgumentException  if $needle is not a string
  * @throws  \InvalidArgumentException  if $haystack is not an array
  * @throws  \InvalidArgumentException  if $wildcard is not a string
  */
 public static function inArray($needle, $haystack, $wildcard = '*')
 {
     $inArray = false;
     // if $needle, $haystack, and $wildcard are given
     if ($needle !== null && $haystack !== null && $wildcard !== null) {
         // if $needle is a string
         if (is_string($needle)) {
             // if $haystack is an array
             if (is_array($haystack)) {
                 // if $wildcard is a string
                 if (is_string($wildcard)) {
                     // if $needle contains the wildcard character
                     if (strpos($needle, $wildcard) !== false) {
                         // determine if the neeedle starts or ends with the wildcard
                         $startsWith = \Jstewmc\PhpHelpers\Str::startsWith($needle, $wildcard);
                         $endsWith = \Jstewmc\PhpHelpers\Str::endsWith($needle, $wildcard);
                         // set the *actual* needle
                         $needle = str_ireplace($wildcard, '', $needle);
                         // loop through the haystack
                         foreach ($haystack as $value) {
                             if ($startsWith && $endsWith) {
                                 $inArray = strpos($value, $needle) !== false;
                             } elseif ($startsWith) {
                                 $inArray = \Jstewmc\PhpHelpers\Str::endsWith($value, $needle);
                             } else {
                                 $inArray = \Jstewmc\PhpHelpers\Str::startsWith($value, $needle);
                             }
                             // if the needle is in the array, stop looking
                             if ($inArray) {
                                 break;
                             }
                         }
                     } else {
                         $inArray = in_array($needle, $haystack);
                     }
                 } else {
                     throw new \InvalidArgumentException(__METHOD__ . "() expects parameter three, the wildcard character, to be a string");
                 }
             } else {
                 throw new \InvalidArgumentException(__METHOD__ . "() expects parameter two, the haystack, to be an array");
             }
         } else {
             throw new \InvalidArgumentException(__METHOD__ . "() expects parameter one, the needle, to be a string");
         }
     } else {
         throw new \BadMethodCallException(__METHOD__ . "() expects two or three parameters: needle, haystack, and wildcard");
     }
     return $inArray;
 }