Passed
Push — 3.0 ( b611c9...572608 )
by Rubén
03:38
created

HtmlFormatter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A formatDescription() 0 7 2
A buildLink() 0 7 2
A formatDetail() 0 17 4
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Core\Messages;
26
27
use SP\Html\Html;
28
29
/**
30
 * Class HtmlFormatter
31
 *
32
 * @package SP\Core\Messages
33
 */
34
final class HtmlFormatter implements FormatterInterface
35
{
36
37
    /**
38
     * @param array $text
39
     * @param bool  $translate
40
     *
41
     * @return string
42
     */
43
    public function formatDetail(array $text, bool $translate = false): string
44
    {
45
        return implode(
46
            '',
47
            array_map(function ($value) use ($translate) {
48
                $right = $this->buildLink($value[1]);
49
                $left = $translate ? __($value[0]) : $value[0];
50
51
                if (strpos($right, '<a') === false) {
52
                    $right = $translate ? __($right) : $right;
53
                }
54
55
                return '<div class="detail">'
56
                    . '<span class="detail-left">' . $left . '</span>'
57
                    . '<span class="detail-right">' . $right . '</span>'
58
                    . '</div>';
59
            }, $text));
60
    }
61
62
    /**
63
     * Detects a link within the string and builds an HTML link
64
     *
65
     * @param string $text
66
     *
67
     * @return string
68
     */
69
    private function buildLink(string $text)
70
    {
71
        if (preg_match('#^https?://.*$#', $text, $matches)) {
72
            return sprintf('<a href="%s">%s</a>', $matches[0], Html::truncate($matches[0], 30));
73
        }
74
75
        return $text;
76
    }
77
78
    /**
79
     * @param array $text
80
     * @param bool  $translate
81
     *
82
     * @return string
83
     */
84
    public function formatDescription(array $text, bool $translate = false): string
85
    {
86
        return implode(
87
            '<br>',
88
            array_map(function ($value) use ($translate) {
89
                return '<strong>' . ($translate ? __($value) : $value) . '</strong>';
90
            }, $text));
91
    }
92
}