Advertisement
Guest User

MUtils.php

a guest
Mar 28th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. <?php
  2.  
  3. namespace legionpe\utils;
  4.  
  5. class MUtils{
  6.     public static function word_startsWithVowel($word){
  7.         return in_array(strtolower(substr($word, 0, 1)), str_split("aeiou"));
  8.     }
  9.     public static function word_addSingularArticle(&$word){
  10.         $word = (self::word_startsWithVowel($word) ? "an":"a") . " $word";
  11.     }
  12.     public static function word_toPlural(&$word){
  13.         if(in_array(substr($word, -1), str_split("osxz"))){
  14.             $word .= "es";
  15.         }
  16.         elseif(in_array(substr($word, -2), ["sh", "ch"])){
  17.             $word .= "es";
  18.         }
  19.         elseif(substr($word, -1) === "y"){
  20.             $word = substr($word, 0, -1) . "ies";
  21.         }
  22.         elseif(substr($word, -1) === "f"){
  23.             $word = substr($word, 0, -1) . "ves";
  24.         }
  25.         else{
  26.             $word .= "s";
  27.         }
  28.     }
  29.     public static function word_quantitize(&$word, $count){
  30.         if($count > 1){ // not >= 2
  31.             self::word_toPlural($word);
  32.             $word = "$count $word";
  33.         }
  34.         elseif(substr($word, -1) !== "s"){
  35.             self::word_addSingularArticle($word);
  36.         }
  37.     }
  38.     public static function num_getOrdinal($num){
  39.         $rounded = $num % 100;
  40.         if(3 < $rounded and $rounded < 21){
  41.             return "th";
  42.         }
  43.         $unit = $rounded % 10;
  44.         if($unit === 1){
  45.             return "st";
  46.         }
  47.         if($unit === 2){
  48.             return "nd";
  49.         }
  50.         return $unit === 3 ? "rd":"th";
  51.     }
  52.     public static function num_forceSign($num){
  53.         if($num > 0){
  54.             return "+$num";
  55.         }
  56.         if($num < 0){
  57.             return "$num";
  58.         }
  59.         return "0";
  60.     }
  61.     public static function time_secsToString($secs){
  62.         $hours = 0;
  63.         $minutes = 0;
  64.         while($secs >= 3600){
  65.             $hours++;
  66.             $secs -= 3600;
  67.         }
  68.         while($secs >= 60){
  69.             $minutes++;
  70.             $secs -= 60;
  71.         }
  72.         $time = "";
  73.         if($hours > 1){
  74.             $time .= "$hours hours, ";
  75.         }
  76.         elseif($hours === 1){
  77.             $time .= "one hour, ";
  78.         }
  79.         if($minutes > 1){
  80.             $time .= "$minutes minutes, ";
  81.         }
  82.         elseif($minutes === 1){
  83.             $time .= "1 minute, ";
  84.         }
  85.         if($secs > 1){
  86.             $time .= "$secs seconds, ";
  87.         }
  88.         elseif($secs !== 0){
  89.             $time .= "$secs second, ";
  90.         }
  91.         return substr($time, 0, -2);
  92.     }
  93.     public static function dir_copy($from, $to){
  94.         $to = rtrim($to, "\\/") . "/";
  95.         foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($from)) as $file){
  96.             if(is_file($file)){
  97.                 $includePath = ltrim(substr($file, strlen($from)), "\\/");
  98.                 $target = $to . $includePath;
  99.                 $dir = dirname($target);
  100.                 if(!is_dir($dir)){
  101.                     mkdir(dirname($target), 0777, true);
  102.                 }
  103.                 copy($file, $target);
  104.             }
  105.         }
  106.     }
  107.     public static function dir_delete($dir){
  108.         $dir = rtrim($dir, "/\\") . "/";
  109.         foreach(scandir($dir) as $file){
  110.             if($file === "." or $file === ".."){
  111.                 continue;
  112.             }
  113.             $path = $dir . $file;
  114.             if(is_dir($path)){
  115.                 self::dir_delete($path);
  116.             }
  117.             else{
  118.                 unlink($file);
  119.             }
  120.         }
  121.         rmdir($dir);
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement