Skip to content

Commit 27c3602

Browse files
committed
Use exceptions instead of PEAR::raiseError
git-svn-id: http://svn.php.net/repository/pear/packages/Text_LanguageDetect/trunk@322303 c90b9560-bf6c-de11-be94-00142212c4b1
1 parent 49d59fd commit 27c3602

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

Text/LanguageDetect.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Implements a version of a technique originally proposed by Cavnar & Trenkle
1010
* (1994): "N-Gram-Based Text Categorization"
1111
*
12-
* PHP versions 4 and 5
12+
* PHP version 5
1313
*
1414
* @category Text
1515
* @package Text_LanguageDetect
@@ -22,6 +22,7 @@
2222
*/
2323

2424
require_once 'PEAR.php';
25+
require_once 'Text/LanguageDetect/Exception.php';
2526
require_once 'Text/LanguageDetect/Parser.php';
2627
require_once 'Text/LanguageDetect/ISO639.php';
2728

@@ -252,9 +253,9 @@ function _readdb($fname)
252253

253254
// input check
254255
if (!file_exists($fname)) {
255-
return PEAR::raiseError('Language database does not exist.');
256+
throw new Text_LanguageDetect_Exception('Language database does not exist.');
256257
} elseif (!is_readable($fname)) {
257-
return PEAR::raiseError('Language database is not readable.');
258+
throw new Text_LanguageDetect_Exception('Language database is not readable.');
258259
}
259260

260261
if (function_exists('file_get_contents')) {
@@ -288,16 +289,12 @@ function _setup_ok(&$err)
288289

289290
} elseif (!is_array($this->_lang_db)) {
290291
if (ini_get('magic_quotes_runtime')) {
291-
$err = PEAR::raiseError('Error loading database. Try turning magic_quotes_runtime off.');
292+
throw new Text_LanguageDetect_Exception('Error loading database. Try turning magic_quotes_runtime off.');
292293
} else {
293-
$err = PEAR::raiseError('Language database is not an array.');
294+
throw new Text_LanguageDetect_Exception('Language database is not an array.');
294295
}
295-
return false;
296-
297296
} elseif (empty($this->_lang_db)) {
298-
$err = PEAR::raiseError('Language database has no elements.');
299-
return false;
300-
297+
throw new Text_LanguageDetect_Exception('Language database has no elements.');
301298
} else {
302299
return true;
303300
}
@@ -424,7 +421,7 @@ function languageExists($lang)
424421

425422
// other (error)
426423
} else {
427-
return PEAR::raiseError('Unknown type passed to languageExists()');
424+
throw new Text_LanguageDetect_Exception('Unknown type passed to languageExists()');
428425
}
429426
}
430427
}
@@ -774,7 +771,7 @@ function detect($sample, $limit = 0)
774771
if (is_array($blocks)) {
775772
$present_blocks = array_keys($blocks);
776773
} else {
777-
return PEAR::raiseError('Error during block detection');
774+
throw new Text_LanguageDetect_Exception('Error during block detection');
778775
}
779776

780777
$possible_langs = array();
@@ -964,11 +961,11 @@ function detectUnicodeBlocks($str, $skip_symbols)
964961
{
965962
// input check
966963
if (!is_bool($skip_symbols)) {
967-
return PEAR::raiseError('Second parameter must be boolean');
964+
throw new Text_LanguageDetect_Exception('Second parameter must be boolean');
968965
}
969966

970967
if (!is_string($str)) {
971-
return PEAR::raiseError('First parameter was not a string');
968+
throw new Text_LanguageDetect_Exception('First parameter was not a string');
972969
}
973970

974971
$sample_obj = new Text_LanguageDetect_Parser($str);
@@ -1001,18 +998,18 @@ function unicodeBlockName($unicode) {
1001998

1002999
// input check
10031000
if ($this->utf8strlen($unicode) > 1) {
1004-
return PEAR::raiseError('Pass this function only a single char');
1001+
throw new Text_LanguageDetect_Exception('Pass this function only a single char');
10051002
}
10061003

10071004
$unicode = $this->_utf8char2unicode($unicode);
10081005

10091006
if ($unicode == -1) {
1010-
return PEAR::raiseError('Malformatted char');
1007+
throw new Text_LanguageDetect_Exception('Malformatted char');
10111008
}
10121009

10131010
// input check
10141011
} elseif (!is_int($unicode)) {
1015-
return PEAR::raiseError('Input must be of type string or int.');
1012+
throw new Text_LanguageDetect_Exception('Input must be of type string or int.');
10161013
}
10171014

10181015
$blocks =& $this->_read_unicode_block_db();
@@ -1253,7 +1250,7 @@ function clusterLanguages()
12531250

12541251
foreach ($langs as $lang) {
12551252
if (!isset($this->_lang_db[$lang])) {
1256-
return PEAR::raiseError("missing $lang!\n");
1253+
throw new Text_LanguageDetect_Exception("missing $lang!\n");
12571254
}
12581255
}
12591256

@@ -1281,7 +1278,7 @@ function clusterLanguages()
12811278

12821279
if (!$highest_key1) {
12831280
// should not ever happen
1284-
return PEAR::raiseError("no highest key? (step: $i)");
1281+
throw new Text_LanguageDetect_Exception("no highest key? (step: $i)");
12851282
}
12861283

12871284
if ($highest_score == 0) {

Text/LanguageDetect/Exception.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
class Text_LanguageDetect_Exception extends Exception {}

0 commit comments

Comments
 (0)