Skip to content

Commit e69a643

Browse files
committed
Improve code
1 parent fadcc7e commit e69a643

File tree

3 files changed

+86
-73
lines changed

3 files changed

+86
-73
lines changed

src/Constants.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,4 @@ class Constants
4343
public const DEFAULT_BR_TEXT = "\r\n";
4444

4545
public const DEFAULT_SPAN_TEXT = " ";
46-
47-
public const MAX_FILE_SIZE = 600000;
4846
}

src/HtmlDomParser.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
namespace PhpSimple;
44

5+
use Exception;
6+
57
class HtmlDomParser
68
{
7-
public function fileGetHtml(
9+
/**
10+
* @throws Exception
11+
*/
12+
public static function fileGetHtml(
813
string $url,
914
bool $useIncludePath = false,
1015
$context = null,
@@ -16,22 +21,28 @@ public function fileGetHtml(
1621
string $defaultBRText = Constants::DEFAULT_BR_TEXT,
1722
string $defaultSpanText = Constants::DEFAULT_SPAN_TEXT
1823
): ?SimpleHtmlDom {
24+
if (!defined('MAX_FILE_SIZE')) {
25+
define('MAX_FILE_SIZE', 600000);
26+
}
1927
// We DO force the tags to be terminated.
2028
$dom = new SimpleHtmlDom(null, $lowercase, $forceTagsClosed, $targetCharset, $stripRN, $defaultBRText, $defaultSpanText);
2129
// For sourceforge users: uncomment the next line and comment the retreive_url_contents line 2 lines down if it is not already done.
2230
$contents = file_get_contents($url, $useIncludePath, $context, $offset);
2331
// Paperg - use our own mechanism for getting the contents as we want to control the timeout.
24-
if (empty($contents) || strlen($contents) > Constants::MAX_FILE_SIZE) {
32+
if (empty($contents) || strlen($contents) > MAX_FILE_SIZE) {
2533
$dom->clear();
26-
return null;
34+
throw new Exception('Content size exceed ' . MAX_FILE_SIZE);
2735
}
2836
// The second parameter can force the selectors to all be lowercase.
2937
$dom->load($contents, $lowercase, $stripRN);
3038

3139
return $dom;
3240
}
3341

34-
public function strGetHtml(
42+
/**
43+
* @throws Exception
44+
*/
45+
public static function strGetHtml(
3546
string $str,
3647
bool $lowercase = true,
3748
bool $forceTagsClosed = true,
@@ -40,10 +51,13 @@ public function strGetHtml(
4051
string $defaultBRText = Constants::DEFAULT_BR_TEXT,
4152
string $defaultSpanText = Constants::DEFAULT_SPAN_TEXT
4253
): ?SimpleHtmlDom {
54+
if (!defined('MAX_FILE_SIZE')) {
55+
define('MAX_FILE_SIZE', 600000);
56+
}
4357
$dom = new SimpleHtmlDom(null, $lowercase, $forceTagsClosed, $targetCharset, $stripRN, $defaultBRText, $defaultSpanText);
44-
if (empty($str) || strlen($str) > Constants::MAX_FILE_SIZE) {
58+
if (empty($str) || strlen($str) > MAX_FILE_SIZE) {
4559
$dom->clear();
46-
return null;
60+
throw new Exception('Content size exceed ' . MAX_FILE_SIZE);
4761
}
4862
$dom->load($str, $lowercase, $stripRN);
4963

0 commit comments

Comments
 (0)