/** * @param $filename string * * @return Grid * @throws MflParserException */ public function parse($filename) { if (is_file($filename)) { return Grid::fromGridFile($this->extractGridFile($filename)); } throw new MflParserException(0, 0, sprintf("No such file '%s'", $filename)); }
/** * @param GridFile $gridFile * @throws MflParserException * @return Grid */ public static function fromGridFile(GridFile $gridFile) { $grid = new Grid(); $grid->setForce($gridFile->getForce()); $index = 1; for ($y = 0; $y < $gridFile->getHeight(); $y++) { for ($x = 0; $x < $gridFile->getWidth(); $x++) { $value = $gridFile->getCell($x, $y); if ($gridFile->isPicture($x, $y)) { $grid->addCell(new PictureCell($x, $y)); } else { if ($value === strtolower($value)) { $clues = []; $arrows = Arrow::getArrows($value); if (sizeof($arrows) == 0) { throw new MflParserException($x, $y, "Unable to find definitions for {$value}"); } foreach ($arrows as $arrow) { $clue = new Clue($gridFile->getDefinitions()[$index], self::getWord($arrow, $gridFile, $x, $y), $gridFile->getLevel($index - 1), $arrow); $clues[] = $clue; $grid->addClue($clue); $index++; } $grid->addCell(new ClueCell($x, $y, $clues)); } else { $grid->addCell(new LetterCell($x, $y, $value, isset($gridFile->getDashes()[$x * sizeof($gridFile->getRows()[0]) + $y + 1]) ? $gridFile->getDashes()[$x * sizeof($gridFile->getRows()[0]) + $y + 1] : [])); } } } } return $grid; }