<?php

class Document
{
    
    private $_children = [];

    public function parentLevel ()
    {
        $level = 0;
        
        if ($this->hasChildren())
        {
            $childrenLevels = [];
            
            foreach($this->children() as $child)
            {
                $childrenLevels[] = $child->parentLevel();
            }
            
            $level = max($childrenLevels) + 1;
        }
        
        return $level;
    }
    
    public function children ()
    {
        return $this->_children;
    }
    
    public function hasChildren ()
    {
        return (count($this->_children) > 0);
    }
    
    public function addChild ($child)
    {
        $this->_children[] = $child;
    }
    
}

$doc1 = new Document();
$doc2 = new Document();
$doc3 = new Document();
$doc4 = new Document();
$doc5 = new Document();
$doc6 = new Document();
$doc7 = new Document();

$doc1->addChild($doc2);
$doc1->addChild($doc3);
$doc1->addChild($doc4);

$doc2->addChild($doc5);
$doc2->addChild($doc6);

$doc5->addChild($doc7);

echo $doc1->parentLevel(), PHP_EOL;