<?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 {
}
- 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);
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]);
}
$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;
}