-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThrow_Helpers.php
45 lines (40 loc) · 1.11 KB
/
Throw_Helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace Your_Namespace\Core\Traits;
trait Throw_Helpers {
/**
* @return \Exception
*/
public static function get_error_class () {
return \Exception::class;
}
/**
* If the condition is falsy, throws a Exception.
*
* @param bool $condition
* @param callable|mixed $message
* @param \Throwable|null $exception_class
* @return void
* @throws \Throwable
*/
public static function throw_if ( $condition, $message, $exception_class = null ) {
if ( $condition ) {
if ( ! is_string( $message ) && \is_callable( $message ) ) {
$message = $message();
}
$exception_class = $exception_class ? $exception_class : self::get_error_class();
throw new $exception_class( $message );
}
}
/**
* If the $var is a WP_Error instance, throws a Exception.
*
* @param mixed $condition
* @param string $code
* @param \Throwable|null $exception_class
* @return void
* @throws \Throwable
*/
public static function throw_wp_error ( $var, $code = null, $exception_class = null ) {
if ( \is_wp_error( $var ) ) self::throw_if( true, $var->get_error_message( $code ), $exception_class );
}
}