|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Bright Nucleus Shortcode Component. |
|
4
|
|
|
* |
|
5
|
|
|
* @package BrightNucleus\Shortcode |
|
6
|
|
|
* @author Alain Schlesser <[email protected]> |
|
7
|
|
|
* @license GPL-2.0+ |
|
8
|
|
|
* @link http://www.brightnucleus.com/ |
|
9
|
|
|
* @copyright 2015-2016 Alain Schlesser, Bright Nucleus |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace BrightNucleus\Shortcode\Exception; |
|
13
|
|
|
|
|
14
|
|
|
use BrightNucleus\Exception\RuntimeException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class FailedToInstantiateObject. |
|
18
|
|
|
* |
|
19
|
|
|
* @since 0.3.0 |
|
20
|
|
|
* |
|
21
|
|
|
* @package BrightNucleus\Shortcode\Exception |
|
22
|
|
|
* @author Alain Schlesser <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class FailedToInstantiateObject extends RuntimeException implements ShortcodeException { |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Create a new instance from a passed-in class name or factory callable. |
|
28
|
|
|
* |
|
29
|
|
|
* @since 0.3.0 |
|
30
|
|
|
* |
|
31
|
|
|
* @param mixed $factory Class name or factory callable. |
|
32
|
|
|
* @param string $interface Interface name that the object should have |
|
33
|
|
|
* implemented. |
|
34
|
|
|
* @return static Instance of an exception. |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function fromFactory( $factory, $interface ) { |
|
|
|
|
|
|
37
|
|
|
if ( is_callable( $factory ) ) { |
|
38
|
|
|
$message = sprintf( |
|
39
|
|
|
'Could not instantiate object of type "%1$s" from factory of type: "%2$s".', |
|
40
|
|
|
$interface, |
|
41
|
|
|
gettype( $factory ) |
|
42
|
|
|
); |
|
43
|
|
|
} elseif ( is_string( $factory ) ) { |
|
44
|
|
|
$message = sprintf( |
|
45
|
|
|
'Could not instantiate object of type "%1$s" from class name: "%2$s".', |
|
46
|
|
|
$interface, |
|
47
|
|
|
$factory |
|
48
|
|
|
); |
|
49
|
|
|
} else { |
|
50
|
|
|
$message = sprintf( |
|
51
|
|
|
'Could not instantiate object of type "%1$s" from invalid argument of type: "%2$s".', |
|
52
|
|
|
$interface, |
|
53
|
|
|
$factory |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
return new static( $message ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Create a new instance from a passed-in object that does not implement the |
|
61
|
|
|
* correct interface. |
|
62
|
|
|
* |
|
63
|
|
|
* @since 0.3.0 |
|
64
|
|
|
* |
|
65
|
|
|
* @param mixed $factory Class name or factory callable. |
|
66
|
|
|
* @param string $interface Interface name that the object should have |
|
67
|
|
|
* implemented. |
|
68
|
|
|
* @return static Instance of an exception. |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function fromInvalidObject( $factory, $interface ) { |
|
|
|
|
|
|
71
|
|
|
$message = sprintf( |
|
72
|
|
|
'Could not instantiate object : "%1$s".', |
|
73
|
|
|
$factory |
|
74
|
|
|
); |
|
75
|
|
|
return new static( $message ); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|