oqlexception.class.inc.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. class OQLException extends CoreException
  3. {
  4. public function __construct($sIssue, $sInput, $iLine, $iCol, $sUnexpected, $aExpecting = null)
  5. {
  6. $this->m_MyIssue = $sIssue;
  7. $this->m_sInput = $sInput;
  8. $this->m_iLine = $iLine;
  9. $this->m_iCol = $iCol;
  10. $this->m_sUnexpected = $sUnexpected;
  11. $this->m_aExpecting = $aExpecting;
  12. if (is_null($this->m_aExpecting) || (count($this->m_aExpecting) == 0))
  13. {
  14. $sMessage = "$sIssue - found '{$this->m_sUnexpected}' at $iCol in '$sInput'";
  15. }
  16. else
  17. {
  18. $sExpectations = '{'.implode(', ', $this->m_aExpecting).'}';
  19. $sSuggest = self::FindClosestString($this->m_sUnexpected, $this->m_aExpecting);
  20. $sMessage = "$sIssue - found '{$this->m_sUnexpected}' at $iCol in '$sInput', expecting $sExpectations, I would suggest to use '$sSuggest'";
  21. }
  22. // make sure everything is assigned properly
  23. parent::__construct($sMessage, 0);
  24. }
  25. public function getHtmlDesc($sHighlightHtmlBegin = '<b>', $sHighlightHtmlEnd = '</b>')
  26. {
  27. $sRet = htmlentities($this->m_MyIssue.", found '".$this->m_sUnexpected."' in: ");
  28. $sRet .= htmlentities(substr($this->m_sInput, 0, $this->m_iCol));
  29. $sRet .= $sHighlightHtmlBegin.htmlentities(substr($this->m_sInput, $this->m_iCol, strlen($this->m_sUnexpected))).$sHighlightHtmlEnd;
  30. $sRet .= htmlentities(substr($this->m_sInput, $this->m_iCol + strlen($this->m_sUnexpected)));
  31. if (!is_null($this->m_aExpecting) && (count($this->m_aExpecting) > 0))
  32. {
  33. $sExpectations = '{'.implode(', ', $this->m_aExpecting).'}';
  34. $sRet .= ", expecting ".htmlentities($sExpectations);
  35. $sSuggest = self::FindClosestString($this->m_sUnexpected, $this->m_aExpecting);
  36. if (strlen($sSuggest) > 0)
  37. {
  38. $sRet .= ", I would suggest to use '$sHighlightHtmlBegin".htmlentities($sSuggest)."$sHighlightHtmlEnd'";
  39. }
  40. }
  41. return $sRet;
  42. }
  43. static protected function FindClosestString($sInput, $aDictionary)
  44. {
  45. // no shortest distance found, yet
  46. $fShortest = -1;
  47. $sRet = '';
  48. // loop through words to find the closest
  49. foreach ($aDictionary as $sSuggestion)
  50. {
  51. // calculate the distance between the input string and the suggested one
  52. $fDist = levenshtein($sInput, $sSuggestion);
  53. if ($fDist == 0)
  54. {
  55. // Exact match
  56. return $sSuggestion;
  57. }
  58. if ($fShortest < 0 || ($fDist < 4 && $fDist <= $fShortest))
  59. {
  60. // set the closest match, and shortest distance
  61. $sRet = $sSuggestion;
  62. $fShortest = $fDist;
  63. }
  64. }
  65. return $sRet;
  66. }
  67. }
  68. ?>