Skip to content

Commit cd771c4

Browse files
committed
implement custom exceptions handler class
1 parent ce286a5 commit cd771c4

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

src/PhpStringHelpers.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
<?php
22

3+
declare(strict_types=1);
4+
5+
use PhpStringHelpers\utility\StrUtility as strHelpers;

src/exceptions/.gitkeep

Whitespace-only changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace src\exceptions;
4+
5+
use Exception;
6+
7+
class FileDoesNotExistsException extends Exception
8+
{
9+
public function __construct(string $message, int $statusCode = 500)
10+
{
11+
parent::__construct($message, $statusCode);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace src\exceptions;
4+
5+
use Exception;
6+
7+
class UrlIsNotValidException extends Exception
8+
{
9+
public function __construct(string $message, int $statusCode = 500)
10+
{
11+
parent::__construct($message, $statusCode);
12+
}
13+
}

src/utility/StrUtility.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace PhpStringHelpers\utility;
66

7+
use src\exceptions\UrlIsNotValidException;
8+
use src\exceptions\FileDoesNotExistsException;
9+
710
class StrUtility
811
{
912
const REGULAR_WORDS_PATTERN = '/[^a-z0-9]/im';
@@ -85,13 +88,11 @@ public static function translate(string $key, string $replace = '', string $file
8588
$filePath = self::path('lang.' . $fileName);
8689

8790
if (!is_file($filePath) || !file_exists($filePath))
88-
// throw new FileDoesNotExistsException("file does not exist");
89-
return 'no found';
91+
throw new FileDoesNotExistsException("File Does Not Exist");
9092

9193
$data = require_once $filePath;
9294

9395
if (!is_array($data))
94-
// throw file data is not array ...
9596
$data = [];
9697

9798
if (!key_exists($key, $data))
@@ -115,8 +116,7 @@ public static function path(string $path, string $pathExtension = 'php'): string
115116
$filePath = $path . '.' . strtolower($pathExtension);
116117

117118
if (!is_file($filePath) || !file_exists($filePath))
118-
// throw new FileDoesNotExistsException("file does not exist");
119-
return 'no found';
119+
throw new FileDoesNotExistsException("File Does Not Exist");
120120

121121
return $filePath;
122122
}
@@ -242,8 +242,8 @@ public static function generateAnchor(string|int $content, string $href): string
242242
$href = filter_var(trim($href), FILTER_SANITIZE_URL);
243243

244244
if (!filter_var($href, FILTER_VALIDATE_URL))
245-
// throw exception
246-
return "url is not valid";
245+
throw new UrlIsNotValidException('Url Is Not Valid');
246+
247247
return "<a href=$href>" . self::clearString($content) . '</a>';
248248
}
249249

0 commit comments

Comments
 (0)