<?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;
PD9waHAKCmNsYXNzIERvY3VtZW50CnsKICAgIAogICAgcHJpdmF0ZSAkX2NoaWxkcmVuID0gW107CgogICAgcHVibGljIGZ1bmN0aW9uIHBhcmVudExldmVsICgpCiAgICB7CiAgICAgICAgJGxldmVsID0gMDsKICAgICAgICAKICAgICAgICBpZiAoJHRoaXMtPmhhc0NoaWxkcmVuKCkpCiAgICAgICAgewogICAgICAgICAgICAkY2hpbGRyZW5MZXZlbHMgPSBbXTsKICAgICAgICAgICAgCiAgICAgICAgICAgIGZvcmVhY2goJHRoaXMtPmNoaWxkcmVuKCkgYXMgJGNoaWxkKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAkY2hpbGRyZW5MZXZlbHNbXSA9ICRjaGlsZC0+cGFyZW50TGV2ZWwoKTsKICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgJGxldmVsID0gbWF4KCRjaGlsZHJlbkxldmVscykgKyAxOwogICAgICAgIH0KICAgICAgICAKICAgICAgICByZXR1cm4gJGxldmVsOwogICAgfQogICAgCiAgICBwdWJsaWMgZnVuY3Rpb24gY2hpbGRyZW4gKCkKICAgIHsKICAgICAgICByZXR1cm4gJHRoaXMtPl9jaGlsZHJlbjsKICAgIH0KICAgIAog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