Exemple #1
0
 /**
  * Deletes a non-empty directory and its sub-directories
  *
  * PHP's native rmdir() function requires the directory to be empty. I'll 
  * recursively delete a directory's files and sub-directories. BE CAREFUL!
  * Use the $container argument to be safe.
  *
  * @since  0.1.0
  *
  * @param  string  $directory  the path of the directory to remove
  * @param  string  $container  an ancestor directory of $directory
  *
  * @return  bool  true if success
  *
  * @throws  \BadMethodCallException    if $directory or $container is null
  * @throws  \InvalidArgumentException  if $directory is not a string
  * @throws  \InvalidArgumentException  if $container is not a string
  * @throws  \InvalidArgumentException  if $directory is not a valid directory path
  * @throws  \InvalidArgumentException  if $directory is not writeable
  * @throws  \InvalidArgumentException  if $directory is not contained in $container
  *
  * @see  http://stackoverflow.com/a/11614201  donald123's answer to "Remove all
  *    files, folders, and their subfolders with php" on StackOverflow
  * @see  http://us1.php.net/rmdir  rmdir() man page
  *
  */
 public static function remove($directory, $container)
 {
     $isSuccess = false;
     // if $directory and $container are given
     if ($directory !== null && $container !== null) {
         // if $directory is a string
         if (is_string($directory)) {
             // if $container is a string
             if (is_string($container)) {
                 // if the $directory argument is a dir
                 if (is_dir($directory)) {
                     // if $directory is writable
                     if (is_writable($directory)) {
                         // if the $directory is in the $container
                         if (\Jstewmc\PhpHelpers\Str::startsWith($directory, $container)) {
                             // open the directory
                             $dir = opendir($directory);
                             // read the first entity
                             $entity = readdir($dir);
                             // loop through the dir's entities
                             while ($entity !== false) {
                                 // if the entity is not the special chars "." and ".."
                                 if ($entity != '.' && $entity != '..') {
                                     // if the entity is a sub-directory
                                     if (is_dir($directory . DIRECTORY_SEPARATOR . $entity)) {
                                         // clear and delete the sub-directory
                                         $isSuccess = self::remove($directory . DIRECTORY_SEPARATOR . $entity, $container);
                                     } else {
                                         // otheriwse, the entity is a file; delete it
                                         $isSuccess = unlink($directory . DIRECTORY_SEPARATOR . $entity);
                                     }
                                     // if an error occurs, stop
                                     if (!$isSuccess) {
                                         break;
                                     }
                                 } else {
                                     // there was nothing to remove
                                     // set $isSuccess true in case the directory is empty
                                     // if it's not empty, $isSuccess will be overwritten anyway
                                     //
                                     $isSuccess = true;
                                 }
                                 // advance to the next entity
                                 $entity = readdir($dir);
                             }
                             // close and remove the directory
                             closedir($dir);
                             $isSuccess = rmdir($directory . DIRECTORY_SEPARATOR . $entity);
                         } else {
                             throw new \InvalidArgumentException(__METHOD__ . "() expects parameter two, container, to contain the directory");
                         }
                     } else {
                         throw new \InvalidArgumentException(__METHOD__ . "() expects parameter one, directory, to be a writable directory");
                     }
                 } else {
                     throw new \InvalidArgumentException(__METHOD__ . "() expects parameter one, directory, to be a valid directory");
                 }
             } else {
                 throw new \InvalidArgumentException(__METHOD__ . "() expects the second parameter, container, to be a string");
             }
         } else {
             throw new \InvalidArgumentException(__METHOD__ . "() expects the first parameter, directory, to be a string");
         }
     } else {
         throw new \BadMethodCallException(__METHOD__ . "() expects two string parameters, directory and container");
     }
     return $isSuccess;
 }
Exemple #2
0
 /**
  * truncate() should return the string truncated exactly at the limit if the break
  *     is null
  */
 public function testTruncate_returnStringTruncatedExact_ifBreakIsEmptyString()
 {
     return $this->assertEquals(Str::truncate('foo bar', 5, null), 'foo b...');
 }
Exemple #3
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;
 }