Passed
Push — main ( 02d0e2...a48ca0 )
by Thierry
12:44 queued 11:20
created

DialogPlugin::getScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * DialogPlugin.php - modal, alert and confirm dialogs for Jaxon.
5
 *
6
 * Show modal, alert and confirm dialogs with various javascript libraries.
7
 * This class generates js ans css code for dialog libraries.
8
 *
9
 * @package jaxon-core
10
 * @author Thierry Feuzeu <[email protected]>
11
 * @copyright 2016 Thierry Feuzeu <[email protected]>
12
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
13
 * @link https://github.com/jaxon-php/jaxon-core
14
 */
15
16
namespace Jaxon\Dialogs;
17
18
use Jaxon\Exception\SetupException;
19
use Jaxon\Plugin\AbstractPlugin;
20
21
use function array_reduce;
22
use function trim;
23
24
class DialogPlugin extends AbstractPlugin
25
{
26
    /**
27
     * @const The plugin name
28
     */
29
    const NAME = 'dialog_code';
30
31
    /**
32
     * @var array
33
     */
34
    protected $aLibraries = null;
35
36
    /**
37
     * The constructor
38
     *
39
     * @param DialogManager $xDialogManager
40
     */
41
    public function __construct(private DialogManager $xDialogManager)
42
    {}
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function getName(): string
48
    {
49
        return self::NAME;
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function getHash(): string
56
    {
57
        // The version number is used as hash
58
        return '4.0.0';
59
    }
60
61
    public function getUri(): string
62
    {
63
        return '';
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    private function getLibraries(): array
70
    {
71
        return $this->aLibraries ?: $this->aLibraries = $this->xDialogManager->getLibraries();
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    public function getJs(): string
78
    {
79
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
80
            return $sCode . $xLibrary->getJs() . "\n\n";
81
        }, '');
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function getCss(): string
88
    {
89
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
90
            return $sCode . trim($xLibrary->getCss()) . "\n\n";
91
        }, '');
92
    }
93
94
    /**
95
     * @inheritDoc
96
     * @throws SetupException
97
     */
98
    public function getScript(): string
99
    {
100
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
101
            return $sCode . trim($xLibrary->getScript()) . "\n\n";
102
        }, '');
103
    }
104
105
    /**
106
     * @inheritDoc
107
     */
108
    public function getReadyScript(): string
109
    {
110
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
111
            return $sCode . trim($xLibrary->getReadyScript()) . "\n\n";
112
        }, '');
113
    }
114
}
115