/** * Sort the list of CIDR blocks, optionally with a custom callback function. * * @param array $cidrs A list of CIDR blocks (strings or objects) * @param Closure $callback Optional callback to perform the sorting. * See PHP usort documentation for more details. */ public static function cidr_sort($cidrs, $callback = null) { // all indexes must be a CIDR object $cidrs = array_map(function ($o) { return $o instanceof CIDR ? $o : new CIDR($o); }, $cidrs); if ($callback === null) { $callback = function ($a, $b) { if (0 != ($o = BC::cmp($a->getStart(true), $b->getStart(true)))) { return $o; // < or > } if ($a->getPrefix() == $b->getPrefix()) { return 0; } return $a->getPrefix() < $b->getPrefix() ? -1 : 1; }; } elseif (!$callback instanceof \Closure or !is_callable($callback)) { throw new \InvalidArgumentException("Invalid callback in CIDR::cidr_sort, expected Closure, got " . gettype($callback)); } usort($cidrs, $callback); return $cidrs; }