1212
1313namespace dvlpr1996 \PhpEnumHelperTrait ;
1414
15- use Exception ;
1615use 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
0 commit comments