1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <contact@terryl.in>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * 
10
 * php version 7.1.0
11
 * 
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <contact@terryl.in>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20

21
declare(strict_types=1);
22

23
namespace Shieldon\Firewall\Firewall\Messenger;
24

25
use Shieldon\Messenger\Messenger\MessengerInterface;
26
use function array_map;
27
use function explode;
28
use function implode;
29
use function ucwords;
30

31
/*
32
 * The factory creates messenger instances.
33
 */
34
class MessengerFactory
35
{
36
    /**
37
     * Create a messenger instance.
38
     *
39
     * @param string $messenger The messenger's ID string.
40
     * @param array  $setting   The configuration of that messanger.
41
     *
42
     * @return MessengerInterface
43
     */
44 3
    public static function getInstance(string $messenger, array $setting): MessengerInterface
45
    {
46 3
        $className = '\Shieldon\Firewall\Firewall\Messenger\Item' . self::getCamelCase($messenger);
47

48 3
        return $className::get($setting);
49
    }
50

51
    /**
52
     * Check whether a messenger is available or not.
53
     *
54
     * @param array  $setting   The configuration of that messanger.
55
     *
56
     * @return bool
57
     */
58 3
    public static function check(array $setting): bool
59
    {
60
        // If the settings is not set correctly.
61 3
        if (empty($setting['enable']) || empty($setting['confirm_test'])) {
62 3
            return false;
63
        }
64

65 3
        return true;
66
    }
67

68
    /**
69
     * Covert string with dashes into camel-case string.
70
     *
71
     * @param string $string A string with dashes.
72
     *
73
     * @return string
74
     */
75 3
    public static function getCamelCase(string $string = ''): string
76
    {
77 3
        $str = explode('_', $string);
78 3
        $str = implode(
79 3
            '',
80 3
            array_map(
81
                function ($word) {
82 3
                    return ucwords($word); 
83 3
                },
84 3
                $str
85
            )
86
        );
87 3
        return $str;
88
    }
89
}

Read our documentation on viewing source code .

Loading