Passed
Pull Request — dev (#2)
by Romain
03:39
created

StringService::formatEmailAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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