Passed
Push — dev ( 9618b4...ab39bb )
by Romain
07:09 queued 03:28
created

StringService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mark() 0 3 1
A doMark() 0 6 1
A formatEmailAddress() 0 11 2
1
<?php
2
3
/*
4
 * Copyright (C) 2017
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\Service;
18
19
use CuyZ\Notiz\Service\Traits\SelfInstantiateTrait;
20
use TYPO3\CMS\Core\SingletonInterface;
21
22
class StringService implements SingletonInterface
23
{
24
    use SelfInstantiateTrait;
25
26
    /**
27
     * Static alias method for:
28
     *
29
     * @see \CuyZ\Notiz\Service\StringService::doMark
30
     *
31
     * @param string $content
32
     * @return string
33
     */
34
    public static function mark($content)
35
    {
36
        return self::get()->doMark($content);
37
    }
38
39
    /**
40
     * Modifies the contents using the grave accent wrapper, by replacing it with the
41
     * HTML tag `samp`.
42
     *
43
     * Example:
44
     *
45
     * > Look at `foo` lorem ipsum...
46
     *
47
     * will become:
48
     *
49
     * > Look at <samp class="bg-info">foo</samp> lorem ipsum...
50
     *
51
     * @param string $content
52
     * @return string
53
     */
54
    public function doMark($content)
55
    {
56
        return preg_replace(
57
            '/`([^`]+)`/',
58
            '<samp class="bg-info">$1</samp>',
59
            $content
60
        );
61
    }
62
63
    /**
64
     * Takes an email address that can have the following format:
65
     *
66
     * - `John Smith <[email protected]>`
67
     * - `[email protected]`
68
     *
69
     * It will return an array:
70
     *
71
     * ```
72
     * // `John Smith <[email protected]>` becomes :
73
     * [
74
     *     'email' => '[email protected]',
75
     *     'name' => 'John Smith'
76
     * ]
77
     *
78
     * // `[email protected]` becomes:
79
     * [
80
     *     'email' => '[email protected]',
81
     *     'name' => null
82
     * ]
83
     * ```
84
     *
85
     * @param string $email
86
     * @return array
87
     */
88
    public function formatEmailAddress($email)
89
    {
90
        if (preg_match('#([^<]+) <([^>]+)>#', $email, $matches)) {
91
            return [
92
                'name' => $matches[1],
93
                'email' => $matches[2],
94
            ];
95
        } else {
96
            return [
97
                'name' => null,
98
                'email' => $email,
99
            ];
100
        }
101
    }
102
}
103