]> _ Git - cubist_util.git/commitdiff
wip #5893 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 27 Apr 2023 18:24:33 +0000 (20:24 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 27 Apr 2023 18:24:33 +0000 (20:24 +0200)
src/ArrayUtil.php

index 6b8ebb2c37e91e49fe00a16afe329bc84097e9cc..51db0ed7c0f3f09725b1bb51dabe28d0162e8609 100644 (file)
@@ -1,19 +1,22 @@
 <?php
+
 namespace Cubist\Util;
-class ArrayUtil {
+class ArrayUtil
+{
 
-       public static function removeValue(&$input, $value, $strict = false) {
-               $key = array_search($value, $input, $strict);
-               if ($key === false) {
-                       return;
-               }
-               unset($input[$key]);
-       }
+    public static function removeValue(&$input, $value, $strict = false)
+    {
+        $key = array_search($value, $input, $strict);
+        if ($key === false) {
+            return;
+        }
+        unset($input[$key]);
+    }
 
     public static function flatten($a)
     {
         $res = array();
-        foreach($a as $k => $v) {
+        foreach ($a as $k => $v) {
             if (is_array($v)) {
                 $res = array_merge($res, self::flatten($v));
             } else {
@@ -24,149 +27,159 @@ class ArrayUtil {
     }
 
 
-       public static function removeValues(&$input, $values, $strict = false) {
-               if (!is_array($values)) {
-                       $values = array($values);
-               }
-               foreach ($values as $value) {
-                       self::removeValue($input, $value, $strict);
-               }
-       }
-
-       public static function implodeMulti($glue, $arr) {
-               if (is_string($arr)) {
-                       return $arr;
-               }
-
-               if (!is_array($arr) && !is_object($arr)) {
-                       return $arr;
-               }
-
-               $flat = array();
-               foreach ($arr as $a) {
-                       if (is_array($a)) {
-                               $flat[] = self::implodeMulti($glue, $a);
-                       } else {
-                               $flat[] = $a;
-                       }
-               }
-               return implode($glue, $flat);
-       }
-
-       public static function shuffle(&$arr) {
-               uasort($arr, function ($a, $b) {
-                       return mt_rand(0, 1) > 0 ? 1 : -1;
-               });
-       }
-
-       public static function pickRandom($arr, &$key, &$value) {
-               $rand = $arr;
-               self::shuffle($rand);
-               foreach ($rand as $k => $v) {
-                       $key = $k;
-                       $value = $v;
-                       return;
-               }
-       }
-
-       public static function average($array) {
-               $nb = count($array);
-               if (!$nb) {
-                       return false;
-               }
-
-               $nb = 0;
-               $sum = 0;
-               foreach ($array as $v) {
-                       if (is_null($v)) {
-                               continue;
-                       }
-                       $nb++;
-                       $sum += $v;
-               }
-               if (!$nb) {
-                       return false;
-               }
-
-               $res = $sum / $nb;
-               return $res;
-       }
-
-       public static function multidimensionnalSort(&$input, $way = SORT_ASC, $sort_flags = SORT_STRING) {
-               if (!is_array($input)) {
-                       return;
-               }
-
-               if (is_callable($way)) {
-                       uksort($input, $way);
-               } else {
-                       $func = 'sort';
-                       if ($way == SORT_DESC) {
-                               $func = 'r' . $func;
-                       }
-                       $func = 'k' . $func;
-                       $func($input, $sort_flags);
-               }
-
-               foreach ($input as $k => $v) {
-                       self::multidimensionnalSort($input[$k], $way, $sort_flags);
-               }
-       }
-
-       public static function unsetKeys(&$input, $keys) {
-               foreach ($keys as $k) {
-                       if (isset($input[$k])) {
-                               unset($input[$k]);
-                       }
-               }
-       }
+    public static function removeValues(&$input, $values, $strict = false)
+    {
+        if (!is_array($values)) {
+            $values = array($values);
+        }
+        foreach ($values as $value) {
+            self::removeValue($input, $value, $strict);
+        }
+    }
+
+    public static function implodeMulti($glue, $arr)
+    {
+        if (is_string($arr)) {
+            return $arr;
+        }
+
+        if (!is_array($arr) && !is_object($arr)) {
+            return $arr;
+        }
+
+        $flat = array();
+        foreach ($arr as $a) {
+            if (is_array($a)) {
+                $flat[] = self::implodeMulti($glue, $a);
+            } else {
+                $flat[] = $a;
+            }
+        }
+        return implode($glue, $flat);
+    }
+
+    public static function shuffle(&$arr)
+    {
+        uasort($arr, function ($a, $b) {
+            return mt_rand(0, 1) > 0 ? 1 : -1;
+        });
+    }
+
+    public static function pickRandom($arr, &$key, &$value)
+    {
+        $rand = $arr;
+        self::shuffle($rand);
+        foreach ($rand as $k => $v) {
+            $key = $k;
+            $value = $v;
+            return;
+        }
+    }
+
+    public static function average($array)
+    {
+        $nb = count($array);
+        if (!$nb) {
+            return false;
+        }
+
+        $nb = 0;
+        $sum = 0;
+        foreach ($array as $v) {
+            if (is_null($v)) {
+                continue;
+            }
+            $nb++;
+            $sum += $v;
+        }
+        if (!$nb) {
+            return false;
+        }
+
+        $res = $sum / $nb;
+        return $res;
+    }
+
+    public static function multidimensionnalSort(&$input, $way = SORT_ASC, $sort_flags = SORT_STRING)
+    {
+        if (!is_array($input)) {
+            return;
+        }
+
+        if (is_callable($way)) {
+            uksort($input, $way);
+        } else {
+            $func = 'sort';
+            if ($way == SORT_DESC) {
+                $func = 'r' . $func;
+            }
+            $func = 'k' . $func;
+            $func($input, $sort_flags);
+        }
+
+        foreach ($input as $k => $v) {
+            self::multidimensionnalSort($input[$k], $way, $sort_flags);
+        }
+    }
+
+    public static function unsetKeys(&$input, $keys)
+    {
+        foreach ($keys as $k) {
+            if (isset($input[$k])) {
+                unset($input[$k]);
+            }
+        }
+    }
 
     /**
      * @param $input
      * @return array
      */
-       public static function asArray($input) {
-               if (is_array($input)) {
-                       return $input;
-               }
-               if (is_object($input)) {
-                       return ObjectUtil::toArray($input);
-               }
-               return [$input];
-       }
-
-       public static function array_key_exists_recursive($needle, $haystack) {
-               if (!is_array($haystack)) {
-                       return false;
-               }
-               if (array_key_exists($needle, $haystack)) {
-                       return true;
-               }
-               foreach ($haystack as $k => $v) {
-                       if (self::array_key_exists_recursive($needle, $v)) {
-                               return true;
-                       }
-               }
-               return false;
-       }
-
-       public static function merge() {
-               $args = func_get_args();
-               $res = array();
-               foreach ($args as $arg) {
-                       if (!is_array($arg)) {
-                               $arg = array($arg);
-                       }
-                       $res = array_merge($res, $arg);
-               }
-               return $res;
-       }
+    public static function asArray($input)
+    {
+        if (is_array($input)) {
+            return $input;
+        }
+        if (is_object($input)) {
+            return ObjectUtil::toArray($input);
+        }
+        return [$input];
+    }
+
+    public static function array_key_exists_recursive($needle, $haystack)
+    {
+        if (!is_array($haystack)) {
+            return false;
+        }
+        if (array_key_exists($needle, $haystack)) {
+            return true;
+        }
+        foreach ($haystack as $k => $v) {
+            if (self::array_key_exists_recursive($needle, $v)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public static function merge()
+    {
+        $args = func_get_args();
+        $res = array();
+        foreach ($args as $arg) {
+            if (!is_array($arg)) {
+                $arg = array($arg);
+            }
+            $res = array_merge($res, $arg);
+        }
+        return $res;
+    }
 
     /**
      * @param $str
      * @return int[]
      */
-    public static function parseRange($str)
+    public static function parseRange($str, $min = null, $max = null)
     {
         $res = [];
         $str = preg_replace('|([^0-9;\-,]?)|', '', $str);
@@ -174,7 +187,7 @@ class ArrayUtil {
             return $res;
         }
         $ranges = Text::multiExplode(';,', $str);
-        foreach($ranges as $range) {
+        foreach ($ranges as $range) {
             $e = explode('-', $range);
             if (count($e) == 1) {
                 $res[] = intval($e[0]);
@@ -188,6 +201,23 @@ class ArrayUtil {
         }
         $res = array_unique($res);
         sort($res, SORT_NUMERIC);
+        if ($min !== null) {
+            foreach ($res as $k => $v) {
+                if ($v < $min) {
+                    unset($res[$k]);
+                } else {
+                    // Array is sorted so next values can be lower that min
+                    break;
+                }
+            }
+        }
+        if ($max !== null) {
+            foreach ($res as $k => $v) {
+                if ($v > $max) {
+                    unset($res[$k]);
+                }
+            }
+        }
         return $res;
     }