Issues (89)

src/View/Helper/FlashMessenger.php (2 issues)

1
<?php
2
3
namespace ConferenceTools\Tickets\View\Helper;
4
5
use Zend\View\Helper\FlashMessenger as ZendFlashMessenger;
6
use Zend\Mvc\Controller\Plugin\FlashMessenger as PluginFlashMessenger;
7
8
class FlashMessenger extends ZendFlashMessenger
0 ignored issues
show
Deprecated Code introduced by
The class Zend\View\Helper\FlashMessenger has been deprecated: This helper will be removed in version 3.0 of this component. At that time, it will be available in zendframework/zend-mvc-plugin-flashmessenger. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

8
class FlashMessenger extends /** @scrutinizer ignore-deprecated */ ZendFlashMessenger
Loading history...
9
{
10
    protected $classMessages = array(
11
        PluginFlashMessenger::NAMESPACE_INFO => 'alert-info',
12
        PluginFlashMessenger::NAMESPACE_ERROR => 'alert-danger',
13
        PluginFlashMessenger::NAMESPACE_SUCCESS => 'alert-success',
14
        PluginFlashMessenger::NAMESPACE_DEFAULT => 'alert-info',
15
        PluginFlashMessenger::NAMESPACE_WARNING => 'alert-warning',
16
    );
17
18
    public function renderAll()
19
    {
20
        $flashMessenger = $this->getPluginFlashMessenger();
21
        $markup = '';
22
        foreach ($this->classMessages as $namespace => $class) {
23
            $messages = $flashMessenger->getMessagesFromNamespace($namespace);
24
            $markup .= $this->renderMessages($namespace, $messages);
25
        }
26
27
        return $markup;
28
    }
29
30
    protected function renderMessages(
31
        $namespace = PluginFlashMessenger::NAMESPACE_DEFAULT,
32
        array $messages = array(),
33
        array $classes = array(),
34
        $autoEscape = null
35
    ) {
36
        // Prepare classes for opening tag
37
        if (empty($classes)) {
38
            if (isset($this->classMessages[$namespace])) {
39
                $classes = $this->classMessages[$namespace];
40
            } else {
41
                $classes = $this->classMessages[PluginFlashMessenger::NAMESPACE_DEFAULT];
42
            }
43
            $classes = array($classes);
44
        }
45
46
        if (null === $autoEscape) {
47
            $autoEscape = $this->getAutoEscape();
48
        }
49
        $messagesToPrint = $this->flattenMessages($messages, $autoEscape);
50
51
        // Generate markup
52
        $markup = '';
53
        foreach ($messagesToPrint as $message) {
54
            $markup .= $this->getView()->alert($message, ['class' => current($classes)], true);
0 ignored issues
show
The method alert() does not exist on Zend\View\Renderer\RendererInterface. It seems like you code against a sub-type of Zend\View\Renderer\RendererInterface such as Zend\View\Renderer\PhpRenderer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            $markup .= $this->getView()->/** @scrutinizer ignore-call */ alert($message, ['class' => current($classes)], true);
Loading history...
55
        }
56
57
        return $markup;
58
    }
59
60
    /**
61
     * @param array $messages
62
     * @param $autoEscape
63
     * @return array
64
     */
65
    private function flattenMessages(array $messages, $autoEscape)
66
    {
67
        // Flatten message array
68
        $escapeHtml = $this->getEscapeHtmlHelper();
69
        $messagesToPrint = array();
70
        $translator = $this->getTranslator();
71
        $translatorTextDomain = $this->getTranslatorTextDomain();
72
        array_walk_recursive(
73
            $messages,
74
            function($item) use (& $messagesToPrint, $escapeHtml, $autoEscape, $translator, $translatorTextDomain) {
75
                if ($translator !== null) {
76
                    $item = $translator->translate(
77
                        $item,
78
                        $translatorTextDomain
79
                    );
80
                }
81
82
                if ($autoEscape) {
83
                    $messagesToPrint[] = $escapeHtml($item);
84
                    return;
85
                }
86
87
                $messagesToPrint[] = $item;
88
            }
89
        );
90
        return $messagesToPrint;
91
    }
92
}