librenms-librenms/LibreNMS/Util/Categorizer.php
Tony Murray ce21011aff Rewrite development helper to lnms dev:check (#11650)
* Refactor pre-commit to class

* docs build

* dusk check

* ci mode for checks

* full checks

* other mysql

* make other lint checks actually work
fix pylint finding

* ci is a long opt

* fix undefined index

* dusk fully working

* ask for forgiveness, not permission

* fix whitespace

* skip dusk sometimes

* Handle 3com and other os with digits

* flags instead of if else spaghetti

* convert to command

* cleanup

* missed check

* fixes

* case

* self-check :D

* argument now

* fix bugs from refactors

* another fix

* adjust file change parsing

* refactor execut a bit

* fallback to global quiet when unknown type.

* allow quiet override for specific commands

* output cleanup

* check flow

* start of tests

* file categorizer tests and fixes

* fixes and cleanup

* skipable not implemented...

* more tests, fix bugs

* more tests and cleanup

* wrong command

* fix canCheck and set env properly

* full env fix

* don't allow dusk on user's run as it will erase their db.

* fix os option

* fix whitespace

* don't need to start server

* ci doesn't like that
2020-05-22 20:27:48 -05:00

72 lines
2.0 KiB
PHP

<?php
/**
* Categorizer.php
*
* Categorize a list of items according to a dynamic list
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2020 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Util;
class Categorizer
{
protected $items;
protected $categorized = [];
protected $categories = [];
protected $skippable;
public function __construct($items = [])
{
$this->skippable = function ($item) {
return false;
};
$this->items = $items;
}
public function addCategory(string $category, callable $function)
{
$this->categories[$category] = $function;
$this->categorized[$category] = [];
}
public function setSkippable(callable $function)
{
$this->skippable = $function;
}
public function categorize()
{
foreach ($this->items as $item) {
foreach ($this->categories as $category => $test) {
if (call_user_func($this->skippable, $item)) {
continue;
}
$result = call_user_func($test, $item);
if ($result !== false) {
$this->categorized[$category][] = $result;
}
}
}
return $this->categorized;
}
}