Example #1
0
 /**
  * Imports the CSV file specified by $pathToFile
  * @param string $pathToFile
  * @return Graph
  */
 public function import($pathToFile)
 {
     $lines = IOUtil::readCsvFile($pathToFile, $this->hasHeader, $this->encoding, $this->delimiter, $this->enclosure, null, null, $this->offset);
     $urls = [];
     $graph = new Graph();
     foreach ($lines as $nr => $line) {
         if (!array_key_exists($this->sourceColumn, $line) || !array_key_exists($this->destinationColumn, $line)) {
             continue;
         }
         if (!parse_url($line[$this->sourceColumn]) || !parse_url($line[$this->destinationColumn])) {
             continue;
         }
         $urlFrom = trim($line[$this->sourceColumn]);
         if (!array_key_exists($urlFrom, $urls)) {
             $urls[$urlFrom] = new Node($urlFrom);
         }
         $urlTo = trim($line[$this->destinationColumn]);
         if (!array_key_exists($urlTo, $urls)) {
             $urls[$urlTo] = new Node($urlTo);
         }
         $edge = new Edge($urls[$urlFrom], $urls[$urlTo]);
         $graph->addEdge($edge);
     }
     return $graph;
 }