$hierarchy * @var string $prefix * @var Tree $tree */ preg_match_all('/^(\d+) (\w+) ?(.*)/m', $gedcom, $matches); [, $levels, $tags, $values] = $matches; $levels = array_map(static fn(string $x): int => (int) $x, $levels); $keys = array_keys($levels); $elements = []; $ids = []; $indent = []; $collapse = []; foreach ($keys as $key) { $hierarchy[$levels[$key]] = $tags[$key]; $full_tag = implode(':', array_slice($hierarchy, 0, 1 + $levels[$key])); $elements[$key] = Registry::elementFactory()->make($full_tag); $ids[$key] = Uuid::uuid4()->toString() . '-' . $full_tag; // Does this element have any children? $has_subtags = ($levels[$key + 1] ?? 0) > $levels[$key]; // Do these children have values? $has_subtags_with_values = false; for ($n = $key + 1; $n < count($keys) && $levels[$n] > $levels[$key]; ++$n) { if ($values[$n] !== '') { $has_subtags_with_values = true; break; } } $indent[$key] = $elements[$key]->collapseChildren() && $has_subtags; $collapse[$key] = $indent[$key] && !$has_subtags_with_values; } ?>