/** * Attempt to update the best path * * @param PathNode $node */ protected function updateBestPath(PathNode $node) { $pathLength = $node->getLength(); if ($this->bestPathLength < $pathLength) { $this->bestPathLength = $pathLength; $this->bestPath = $node; } }
/** * Update the best path reference * * @param PathNode $pathTail * @throws Exception */ public function setPath(PathNode $pathTail) { // Check that the node identifier match if ($pathTail->getStringIdentifier() != $this->getStringIdentifier()) { throw new Exception('Node identifiers do not match'); } // Check that we have the node we are coming from $comingFrom = $pathTail->getParentNode(); if (is_null($comingFrom)) { return; } $comingFromStringIdentifier = $comingFrom->getStringIdentifier(); // Check if the path is longer than anything we have seen for that node before $pathLength = $pathTail->getLength(); if (array_key_exists($comingFromStringIdentifier, $this->bestPathLengths)) { if ($this->bestPathLengths[$comingFromStringIdentifier] < $pathLength) { // Set the new best $this->bestPathLengths[$comingFromStringIdentifier] = $pathLength; $this->bestPaths[$comingFromStringIdentifier] = $pathTail; } } else { // Set the new best $this->bestPathLengths[$comingFromStringIdentifier] = $pathLength; $this->bestPaths[$comingFromStringIdentifier] = $pathTail; } // Keep a statistic of the best path overall if ($pathLength > $this->bestPathLength) { $this->bestPathLength = $pathLength; } }