src/Form/Hydrator/TreeHydrator.php
private function findOrCreateChild($tree, $id)
    {
        /* @var NodeInterface $node */
        foreach ($tree->getChildren() as $node) {
            if ($id && $node->getId() == $id) {
                return $node;
            }
        }

        $nodeClass = get_class($tree);
        $node = new $nodeClass();
        $tree->addChild($node);

        return $node;
    }
private function findOrCreateChild($tree, $id)
    {
        /* @var NodeInterface $node */
        foreach ($tree->getChildren() as $node) {
            if ($id && $node->getId() == $id) {
                return $node;
            }
        }

        $nodeClass = get_class($tree);
        $node = new $nodeClass();
        $tree->addChild($node);

        return $node;
    }
Finds an item in a tree structure or create a new item.

@param NodeInterface $tree
@param $id

@return NodeInterface