1<?php 2 3/* 4 * This file is part of Twig. 5 * 6 * (c) 2009 Fabien Potencier 7 * (c) 2009 Armin Ronacher 8 * 9 * For the full copyright and license information, please view the LICENSE 10 * file that was distributed with this source code. 11 */ 12 13/** 14 * Exception thrown when a syntax error occurs during lexing or parsing of a template. 15 * 16 * @author Fabien Potencier <fabien@symfony.com> 17 */ 18class Twig_Error_Syntax extends Twig_Error 19{ 20 /** 21 * Tweaks the error message to include suggestions. 22 * 23 * @param string $name The original name of the item that does not exist 24 * @param array $items An array of possible items 25 */ 26 public function addSuggestions($name, array $items) 27 { 28 if (!$alternatives = self::computeAlternatives($name, $items)) { 29 return; 30 } 31 32 $this->appendMessage(sprintf(' Did you mean "%s"?', implode('", "', $alternatives))); 33 } 34 35 /** 36 * @internal 37 * 38 * To be merged with the addSuggestions() method in 2.0. 39 */ 40 public static function computeAlternatives($name, $items) 41 { 42 $alternatives = array(); 43 foreach ($items as $item) { 44 $lev = levenshtein($name, $item); 45 if ($lev <= strlen($name) / 3 || false !== strpos($item, $name)) { 46 $alternatives[$item] = $lev; 47 } 48 } 49 asort($alternatives); 50 51 return array_keys($alternatives); 52 } 53} 54