Passed
Push — main ( 17ed8d...71af5c )
by Thierry
03:29 queued 01:55
created

DialogPlugin::_getReadyScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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 count;
23
use function json_encode;
24
use function trim;
25
26
class DialogPlugin extends AbstractPlugin
27
{
28
    /**
29
     * @const The plugin name
30
     */
31
    const NAME = 'dialog_code';
32
33
    /**
34
     * @var array
35
     */
36
    protected $aLibraries = null;
37
38
    /**
39
     * The constructor
40
     *
41
     * @param DialogManager $xDialogManager
42
     */
43
    public function __construct(private DialogManager $xDialogManager)
44
    {}
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public function getName(): string
50
    {
51
        return self::NAME;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function getHash(): string
58
    {
59
        // The version number is used as hash
60
        return '4.0.0';
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    private function getLibraries(): array
67
    {
68
        return $this->aLibraries ?: $this->aLibraries = $this->xDialogManager->getLibraries();
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function getJs(): string
75
    {
76
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
77
            return $sCode . $xLibrary->getJs() . "\n\n";
78
        }, '');
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function getCss(): string
85
    {
86
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
87
            return $sCode . trim($xLibrary->getCss()) . "\n\n";
88
        }, '');
89
    }
90
91
    /**
92
     * @inheritDoc
93
     * @throws SetupException
94
     */
95
    public function getScript(): string
96
    {
97
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
98
            return $sCode . trim($xLibrary->getScript()) . "\n\n";
99
        }, '');
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    private function getOptionsJs(): string
106
    {
107
        $aJsOptions = [];
108
        foreach($this->getLibraries() as $xLibrary)
109
        {
110
            $aLibOptions = $xLibrary->helper()->getJsOptions();
111
            if(count($aLibOptions) > 0)
112
            {
113
                $aJsOptions[$xLibrary->getName()] = $aLibOptions;
114
            }
115
        }
116
        return count($aJsOptions) === 0 ? '' :
117
            "jaxon.dialog.lib.options(" . json_encode($aJsOptions) . ");\n\n";
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    private function _getReadyScript(): string
124
    {
125
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
126
            return $sCode . trim($xLibrary->getReadyScript()) . "\n\n";
127
        }, '');
128
    }
129
130
    /**
131
     * @inheritDoc
132
     */
133
    public function getReadyScript(): string
134
    {
135
        return $this->getOptionsJs() . $this->_getReadyScript();
136
    }
137
}
138