fix tabletree

This commit is contained in:
System Administrator 2023-01-30 18:28:45 +03:00
parent e4527ad94e
commit b3f6cfcbd7
2 changed files with 14 additions and 12 deletions

37
src/TableTree.php Normal file
View file

@ -0,0 +1,37 @@
<?php
/**
* Преобразование дерева из модели Plain в массив массивов (Adjacency List)
*/
/**
* Обходит таблицу как дерево
* $fn ($name, $index, $rows, $cc)
* $name Ключ уровня
* $index Значение ключа уровня
* $rows Все столбцы текущго уровня
* $cc Столбцы более низкого уровня
*
* @param Array $level Уровни вложенности
* @param array $table Таблица
* @param Function $fn Функция которая применяется к каждой ветке дерева
*/
namespace ctiso;
use ctiso\Functions;
class TableTree {
static function walk($level, $table, $fn) {
if (empty ($level)) return $table;
$name = array_shift ($level);
$keys = Functions::key_unique_values($name, $table);
$data = array ();
foreach ($keys as $index) {
list($rows, $table) = Functions::partition (Functions::lcurry(['\ctiso\Functions', '__index'], $index, $name), $table);
// $rows = array_filter ($table, lcurry('__index', intval($index), $name));
//$rows = array_filter ($table, create_function ('$x', 'return __index ('.intval($index).', \''.$name.'\', $x);'));
$data[$index] = call_user_func ($fn, $name, $index, $rows, self::walk ($level, $rows, $fn));
}
return $data;
}
}