|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Shieldon package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Terry L. <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace Shieldon\Firewall\Messenger; |
|
14
|
|
|
|
|
15
|
|
|
use Shieldon\Messenger\Messenger\MessengerInterface; |
|
16
|
|
|
|
|
17
|
|
|
/* |
|
18
|
|
|
* The factory creates messenger instances. |
|
19
|
|
|
*/ |
|
20
|
|
|
class MessengerFactory |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Create a messenger instance. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $messenger The messenger's ID string. |
|
26
|
|
|
* @param array $setting The configuration of that messanger. |
|
27
|
|
|
* |
|
28
|
|
|
* @return MessengerInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function getInstance(string $messenger, array $setting): MessengerInterface |
|
31
|
|
|
{ |
|
32
|
|
|
$className = 'Item' . self::getCamelCase($messenger); |
|
33
|
|
|
|
|
34
|
|
|
return $className::get($setting); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Check whether a messenger is available or not. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $messenger The messenger's ID string. |
|
41
|
|
|
* @param array $setting The configuration of that messanger. |
|
42
|
|
|
* |
|
43
|
|
|
* @return bool |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function check(string $messenger, array $setting): bool |
|
46
|
|
|
{ |
|
47
|
|
|
// If the settings is not set correctly. |
|
48
|
|
|
if (empty($setting['enable']) || empty($setting['confirm_test'])) { |
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// If the class doesn't exist. |
|
53
|
|
|
if (!file_exists(__DIR__ . '/' . self::getCamelCase($messenger) . '.php')) { |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Covert string with dashes into camel-case string. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $string A string with dashes. |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function getCamelCase(string $string = '') |
|
66
|
|
|
{ |
|
67
|
|
|
$str = explode('-', $string); |
|
68
|
|
|
$str = implode('', array_map(function($word) { |
|
69
|
|
|
return ucwords($word); |
|
70
|
|
|
}, $str)); |
|
71
|
|
|
|
|
72
|
|
|
return $str; |
|
73
|
|
|
} |
|
74
|
|
|
} |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: