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

TextFormatter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A formatDescription() 0 7 2
A formatDetail() 0 11 3
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
/**
28
 * Class TextFormatter
29
 *
30
 * @package SP\Core\Messages
31
 */
32
final class TextFormatter implements FormatterInterface
33
{
34
    /**
35
     * @var string
36
     */
37
    private $delimiter;
38
39
    /**
40
     * TextFormatter constructor.
41
     *
42
     * @param string $delimiter
43
     */
44
    public function __construct($delimiter = PHP_EOL)
45
    {
46
        $this->delimiter = $delimiter;
47
    }
48
49
    /**
50
     * @param array $text
51
     * @param bool  $translate
52
     *
53
     * @return string
54
     */
55
    public function formatDetail(array $text, bool $translate = false): string
56
    {
57
        return implode(
58
            $this->delimiter,
59
            array_map(function ($value) use ($translate) {
60
                return sprintf(
61
                    '%s: %s',
62
                    $translate ? __($value[0]) : $value[0]
63
                    , $translate ? __($value[1]) : $value[1]
64
                );
65
            }, $text));
66
67
    }
68
69
    /**
70
     * @param array $text
71
     * @param bool  $translate
72
     *
73
     * @return string
74
     */
75
    public function formatDescription(array $text, bool $translate = false): string
76
    {
77
        if ($translate === true) {
78
            return implode($this->delimiter, array_map('__', $text));
79
        }
80
81
        return implode($this->delimiter, $text);
82
    }
83
}