Completed
Push — develop ( fed724...263ba5 )
by Schlaefer
03:00 queued 11s
created

JsData::addMessage()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 10
nop 2
dl 0
loc 27
rs 9.1768
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Saito\JsData;
14
15
class JsData
16
{
17
    protected $_appJs = [
18
        'msg' => []
19
    ];
20
21
    /**
22
     * get js
23
     *
24
     * @return array
25
     */
26
    public function getJs()
27
    {
28
        return $this->_appJs;
29
    }
30
31
    /**
32
     * Setter
33
     *
34
     * @param string $key string
35
     * @param mixed $value value
36
     * @return void
37
     */
38
    public function set($key, $value)
39
    {
40
        $this->_appJs[$key] = $value;
41
    }
42
43
    /**
44
     * Add message
45
     *
46
     * @param string $message message
47
     * @param array|null $options options
48
     * @return void
49
     */
50
    public function addMessage(string $message, ?array $options = []): void
51
    {
52
        $defaults = [
53
            'type' => 'notice',
54
            'channel' => 'notification'
55
        ];
56
        $options = array_merge($defaults, $options);
57
58
        if (!is_array($message)) {
59
            $message = [$message];
60
        }
61
62
        foreach ($message as $m) {
63
            $nm = [
64
                'message' => $m,
65
                'type' => $options['type'],
66
                'channel' => $options['channel']
67
            ];
68
            if (isset($options['title'])) {
69
                $nm['title'] = $options['title'];
70
            }
71
            if (isset($options['element'])) {
72
                $nm['element'] = $options['element'];
73
            }
74
            $this->_appJs['msg'][] = $nm;
75
        }
76
    }
77
78
    /**
79
     * get messages
80
     *
81
     * @return array
82
     */
83
    public function getMessages()
84
    {
85
        return ['msg' => $this->_appJs['msg']];
86
    }
87
}
88