1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (C) 2018 |
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
|
|
|
* @param string $replacement |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public static function mark($content, $replacement = '<samp class="bg-info">$1</samp>') |
37
|
|
|
{ |
38
|
|
|
return self::get()->doMark($content, $replacement); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Modifies the contents using the grave accent wrapper, by replacing it with the |
43
|
|
|
* HTML tag `samp`. |
44
|
|
|
* |
45
|
|
|
* Example: |
46
|
|
|
* |
47
|
|
|
* > Look at `foo` lorem ipsum... |
48
|
|
|
* |
49
|
|
|
* will become: |
50
|
|
|
* |
51
|
|
|
* > Look at <samp class="bg-info">foo</samp> lorem ipsum... |
52
|
|
|
* |
53
|
|
|
* @param string $content |
54
|
|
|
* @param string $replacement |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function doMark($content, $replacement) |
58
|
|
|
{ |
59
|
|
|
return preg_replace( |
60
|
|
|
'/`([^`]+)`/', |
61
|
|
|
$replacement, |
62
|
|
|
$content |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Takes an email address that can have the following format: |
68
|
|
|
* |
69
|
|
|
* - `John Smith <[email protected]>` |
70
|
|
|
* - `[email protected]` |
71
|
|
|
* |
72
|
|
|
* It will return an array: |
73
|
|
|
* |
74
|
|
|
* ``` |
75
|
|
|
* // `John Smith <[email protected]>` becomes : |
76
|
|
|
* [ |
77
|
|
|
* 'email' => '[email protected]', |
78
|
|
|
* 'name' => 'John Smith' |
79
|
|
|
* ] |
80
|
|
|
* |
81
|
|
|
* // `[email protected]` becomes: |
82
|
|
|
* [ |
83
|
|
|
* 'email' => '[email protected]', |
84
|
|
|
* 'name' => null |
85
|
|
|
* ] |
86
|
|
|
* ``` |
87
|
|
|
* |
88
|
|
|
* @param string $email |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function formatEmailAddress($email) |
92
|
|
|
{ |
93
|
|
|
if (preg_match('#([^<]+) <([^>]+)>#', $email, $matches)) { |
94
|
|
|
return [ |
95
|
|
|
'name' => $matches[1], |
96
|
|
|
'email' => $matches[2], |
97
|
|
|
]; |
98
|
|
|
} else { |
99
|
|
|
return [ |
100
|
|
|
'name' => null, |
101
|
|
|
'email' => $email, |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $string |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function upperCamelCase($string) |
111
|
|
|
{ |
112
|
|
|
return GeneralUtility::underscoredToUpperCamelCase(GeneralUtility::camelCaseToLowerCaseUnderscored($string)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* This will wrap each line of `$text` inside `<p></p>` tags. |
117
|
|
|
* |
118
|
|
|
* @param $text |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function wrapLines($text) |
122
|
|
|
{ |
123
|
|
|
$pad = function ($line) { |
124
|
|
|
return "<p>$line</p>"; |
125
|
|
|
}; |
126
|
|
|
|
127
|
|
|
$lines = explode("\n", $text); |
128
|
|
|
$lines = array_filter($lines); |
129
|
|
|
$lines = array_map($pad, $lines); |
130
|
|
|
|
131
|
|
|
return implode('', $lines); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|