aFieldNameToPosition[$sName] = count($this->aFieldNameToPosition); if (empty($aDependencies)) { $this->AddChild($oNode); return; } // Search the last added dependency $oParentNode = null; $iParentPosition = -1; foreach ($aDependencies as $sNodeName) { if ($sNodeName === $sName) { throw new DependencyException("Form field '$sName' cannot reference itself"); } if (!isset($this->aFieldNameToPosition[$sNodeName])) { throw new DependencyException("Form field '$sNodeName' is not existing"); } if ($this->aFieldNameToPosition[$sNodeName] > $iParentPosition) { $oParentNode = $this->SearchNode($sNodeName); $iParentPosition = $this->aFieldNameToPosition[$sNodeName]; } } if (is_null($oParentNode)) { throw new DependencyException("Could not find dependency for field '$sName'"); } // Add to the latest dependency $oParentNode->AddChild($oNode); } private function SearchNode(string $sName) : ?DependencyNode { foreach($this as $oChildNode) { $oNode = $oChildNode->SearchNode($sName); if ($oNode instanceof DependencyNode) { return $oNode; } } return null; } public function __toString(): string { $sResult = "\n"; foreach ($this as $oNode) { $sResult .= $oNode->Display(); } return $sResult; } }