Completed
Push — master ( 073016...e095ce )
by Derek Stephen
04:16
created

AlertBox   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A renderMessage() 0 15 4
A getClass() 0 8 3
A alertBox() 0 8 1
1
<?php
2
3
namespace App\View\Helper;
4
5
class AlertBox
6
{
7
    /**
8
     * @param array $message array of messages, last item in array should be alert class
9
     * @return bool|string
10
     */
11
    public function alertBox(array $message)
12
    {
13
        $class = $this->getClass($message);
14
15
        $alert = '<div class="alert '.$class.'"><button type="button" class="close" data-dismiss="alert" aria-label="Close">
16
                      <span aria-hidden="true">&times;</span>
17
                    </button>'.$this->renderMessage($message).'</div>';
18
        return $alert;
19
    }
20
21
    /**
22
     * @param array $message
23
     * @return string
24
     */
25
    private function getClass(array $message)
26
    {
27
        if(count($message) < 2) {
28
            return 'alert-info';
29
        }
30
        $class = array_pop($message);
31
        $class = (!strstr($class, 'alert-')) ? 'alert-'.$class : '';
32
        return $class;
33
    }
34
35
    /**
36
     * @param array $message
37
     * @return string
38
     */
39
    private function renderMessage(array $message)
40
    {
41
        if (isset($message[1])) {
42
            unset($message[1]);
43
        }
44
        $alert = '';
45
        $num = count($message);
46
        $x = 1;
47
        foreach($message as $msg)
48
        {
49
            $alert .= $msg;
50
            if($x < $num){$alert .= '<br />';}
51
            $x ++;
52
        }
53
        return $alert;
54
    }
55
}