Skip to content

Commit bc0cc73

Browse files
committed
implement custom JsonException
1 parent bb82e3d commit bc0cc73

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/EnumHelperTrait.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
namespace dvlpr1996\PhpEnumHelperTrait;
1414

15-
use Exception;
1615
use SimpleXMLElement;
16+
use dvlpr1996\PhpEnumHelperTrait\Exceptions\XmlException;
17+
use dvlpr1996\PhpEnumHelperTrait\Exceptions\JsonException;
1718

1819
/**
1920
* Provides utility methods for handling enums in PHP.
@@ -121,7 +122,8 @@ public static function randomCase(): object
121122
return (object)[];
122123
}
123124

124-
return $cases[array_rand($cases)];
125+
$randomIndex = array_rand($cases);
126+
return $cases[$randomIndex];
125127
}
126128

127129
/**
@@ -231,7 +233,7 @@ public static function getNameFromValue(int|string $value): ?string
231233
* @param integer $flags json_encode() $flag argument
232234
* @param integer $depth json_encode() $depth argument
233235
* @return string|null JSON Representation Of The Enum otherwise return null
234-
* @throws Exception if unable to encode data to JSON
236+
* @throws JsonException if unable to encode data to JSON
235237
*/
236238
public static function toJson(int $flags = 0, int $depth = 512): ?string
237239
{
@@ -244,7 +246,7 @@ public static function toJson(int $flags = 0, int $depth = 512): ?string
244246
$jsonData = json_encode($data, $flags, $depth);
245247

246248
if (!is_string($jsonData) || (json_last_error() !== JSON_ERROR_NONE)) {
247-
throw new Exception('Error encoding data to JSON: ' . json_last_error_msg());
249+
throw new JsonException('Error encoding data to JSON: ' . json_last_error_msg());
248250
}
249251

250252
return $jsonData;
@@ -254,7 +256,7 @@ public static function toJson(int $flags = 0, int $depth = 512): ?string
254256
* Convert Enum Data To Xml
255257
*
256258
* @return string|null Xml Representation Of The Enum Otherwise Return Null
257-
* @throws Exception If The Xml Data Could Not Be Parsed
259+
* @throws XmlException If The Xml Data Could Not Be Parsed
258260
*/
259261
public static function toXml(): ?string
260262
{
@@ -281,8 +283,8 @@ public static function toXml(): ?string
281283
}
282284

283285
return $xml->asXML();
284-
} catch (Exception $e) {
285-
throw new Exception('Error creating SimpleXMLElement: ' . $e->getMessage());
286+
} catch (\Exception $e) {
287+
throw new XmlException('Error creating SimpleXMLElement: ' . $e->getMessage());
286288
}
287289
}
288290

src/Exceptions/JsonException.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace dvlpr1996\PhpEnumHelperTrait\Exceptions;
4+
5+
class JsonException extends \Exception
6+
{
7+
public function __construct(string $message, int $statusCode = 500, $previous = null)
8+
{
9+
parent::__construct($message . ' File Path Not Exists', $statusCode, $previous);
10+
}
11+
12+
public function __toString(): string
13+
{
14+
return __CLASS__ . ' : ' . $this->message;
15+
}
16+
}

0 commit comments

Comments
 (0)