Passed
Pull Request — dev (#16)
by Romain
11:46 queued 07:53
created

StringService::upperCamelCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23
class StringService implements SingletonInterface
24
{
25
    use SelfInstantiateTrait;
26
27
    /**
28
     * Static alias method for:
29
     *
30
     * @see \CuyZ\Notiz\Service\StringService::doMark
31
     *
32
     * @param string $content
33
     * @return string
34
     */
35
    public static function mark($content)
36
    {
37
        return self::get()->doMark($content);
38
    }
39
40
    /**
41
     * Modifies the contents using the grave accent wrapper, by replacing it with the
42
     * HTML tag `samp`.
43
     *
44
     * Example:
45
     *
46
     * > Look at `foo` lorem ipsum...
47
     *
48
     * will become:
49
     *
50
     * > Look at <samp class="bg-info">foo</samp> lorem ipsum...
51
     *
52
     * @param string $content
53
     * @return string
54
     */
55
    public function doMark($content)
56
    {
57
        return preg_replace(
58
            '/`([^`]+)`/',
59
            '<samp class="bg-info">$1</samp>',
60
            $content
61
        );
62
    }
63
64
    /**
65
     * Takes an email address that can have the following format:
66
     *
67
     * - `John Smith <[email protected]>`
68
     * - `[email protected]`
69
     *
70
     * It will return an array:
71
     *
72
     * ```
73
     * // `John Smith <[email protected]>` becomes :
74
     * [
75
     *     'email' => '[email protected]',
76
     *     'name' => 'John Smith'
77
     * ]
78
     *
79
     * // `[email protected]` becomes:
80
     * [
81
     *     'email' => '[email protected]',
82
     *     'name' => null
83
     * ]
84
     * ```
85
     *
86
     * @param string $email
87
     * @return array
88
     */
89
    public function formatEmailAddress($email)
90
    {
91
        if (preg_match('#([^<]+) <([^>]+)>#', $email, $matches)) {
92
            return [
93
                'name' => $matches[1],
94
                'email' => $matches[2],
95
            ];
96
        } else {
97
            return [
98
                'name' => null,
99
                'email' => $email,
100
            ];
101
        }
102
    }
103
104
    /**
105
     * @param string $string
106
     * @return string
107
     */
108
    public function upperCamelCase($string)
109
    {
110
        return GeneralUtility::underscoredToUpperCamelCase(GeneralUtility::camelCaseToLowerCaseUnderscored($string));
111
    }
112
}
113