12
12
13
13
namespace dvlpr1996 \PhpEnumHelperTrait ;
14
14
15
- use Exception ;
16
15
use SimpleXMLElement ;
16
+ use dvlpr1996 \PhpEnumHelperTrait \Exceptions \XmlException ;
17
+ use dvlpr1996 \PhpEnumHelperTrait \Exceptions \JsonException ;
17
18
18
19
/**
19
20
* Provides utility methods for handling enums in PHP.
@@ -121,7 +122,8 @@ public static function randomCase(): object
121
122
return (object )[];
122
123
}
123
124
124
- return $ cases [array_rand ($ cases )];
125
+ $ randomIndex = array_rand ($ cases );
126
+ return $ cases [$ randomIndex ];
125
127
}
126
128
127
129
/**
@@ -231,7 +233,7 @@ public static function getNameFromValue(int|string $value): ?string
231
233
* @param integer $flags json_encode() $flag argument
232
234
* @param integer $depth json_encode() $depth argument
233
235
* @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
235
237
*/
236
238
public static function toJson (int $ flags = 0 , int $ depth = 512 ): ?string
237
239
{
@@ -244,7 +246,7 @@ public static function toJson(int $flags = 0, int $depth = 512): ?string
244
246
$ jsonData = json_encode ($ data , $ flags , $ depth );
245
247
246
248
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 ());
248
250
}
249
251
250
252
return $ jsonData ;
@@ -254,7 +256,7 @@ public static function toJson(int $flags = 0, int $depth = 512): ?string
254
256
* Convert Enum Data To Xml
255
257
*
256
258
* @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
258
260
*/
259
261
public static function toXml (): ?string
260
262
{
@@ -281,8 +283,8 @@ public static function toXml(): ?string
281
283
}
282
284
283
285
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 ());
286
288
}
287
289
}
288
290
0 commit comments