CuyZ /
NotiZ
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | /* |
||
| 5 | * Copyright (C) |
||
| 6 | * Nathan Boiron <[email protected]> |
||
| 7 | * Romain Canon <[email protected]> |
||
| 8 | * |
||
| 9 | * This file is part of the TYPO3 NotiZ project. |
||
| 10 | * It is free software; you can redistribute it and/or modify it |
||
| 11 | * under the terms of the GNU General Public License, either |
||
| 12 | * version 3 of the License, or any later version. |
||
| 13 | * |
||
| 14 | * For the full copyright and license information, see: |
||
| 15 | * http://www.gnu.org/licenses/gpl-3.0.html |
||
| 16 | */ |
||
| 17 | |||
| 18 | namespace CuyZ\Notiz\Service; |
||
| 19 | |||
| 20 | use CuyZ\Notiz\Service\Traits\SelfInstantiateTrait; |
||
| 21 | use TYPO3\CMS\Core\SingletonInterface; |
||
| 22 | use TYPO3\CMS\Core\Utility\GeneralUtility; |
||
| 23 | |||
| 24 | class StringService implements SingletonInterface |
||
| 25 | { |
||
| 26 | use SelfInstantiateTrait; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Static alias method for: |
||
| 30 | * |
||
| 31 | * @see \CuyZ\Notiz\Service\StringService::doMark |
||
| 32 | * |
||
| 33 | * @param mixed $content |
||
| 34 | * @param string $replacement |
||
| 35 | * @return string |
||
| 36 | */ |
||
| 37 | public static function mark($content, string $replacement = '<samp class="bg-info">$1</samp>'): string |
||
| 38 | { |
||
| 39 | return self::get()->doMark($content, $replacement); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Modifies the contents using the grave accent wrapper, by replacing it with the |
||
| 44 | * HTML tag `samp`. |
||
| 45 | * |
||
| 46 | * Example: |
||
| 47 | * |
||
| 48 | * > Look at `foo` lorem ipsum... |
||
| 49 | * |
||
| 50 | * will become: |
||
| 51 | * |
||
| 52 | * > Look at <samp class="bg-info">foo</samp> lorem ipsum... |
||
| 53 | * |
||
| 54 | * @param mixed $content |
||
| 55 | * @param string $replacement |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function doMark($content, string $replacement): string |
||
| 59 | { |
||
| 60 | return preg_replace( |
||
| 61 | '/`([^`]+)`/', |
||
| 62 | $replacement, |
||
| 63 | (string)$content |
||
| 64 | ); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Takes an email address that can have the following format: |
||
| 69 | * |
||
| 70 | * - `John Smith <[email protected]>` |
||
| 71 | * - `[email protected]` |
||
| 72 | * |
||
| 73 | * It will return an array: |
||
| 74 | * |
||
| 75 | * ``` |
||
| 76 | * // `John Smith <[email protected]>` becomes : |
||
| 77 | * [ |
||
| 78 | * 'email' => '[email protected]', |
||
| 79 | * 'name' => 'John Smith' |
||
| 80 | * ] |
||
| 81 | * |
||
| 82 | * // `[email protected]` becomes: |
||
| 83 | * [ |
||
| 84 | * 'email' => '[email protected]', |
||
| 85 | * 'name' => null |
||
| 86 | * ] |
||
| 87 | * ``` |
||
| 88 | * |
||
| 89 | * @param string $email |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | public function formatEmailAddress(string $email): array |
||
| 93 | { |
||
| 94 | if (preg_match('#([^<]+) <([^>]+)>#', $email, $matches)) { |
||
| 95 | return [ |
||
| 96 | 'name' => $matches[1], |
||
| 97 | 'email' => $matches[2], |
||
| 98 | ]; |
||
| 99 | } else { |
||
| 100 | return [ |
||
| 101 | 'name' => null, |
||
| 102 | 'email' => $email, |
||
| 103 | ]; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $string |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function upperCamelCase(string $string): string |
||
| 112 | { |
||
| 113 | return GeneralUtility::underscoredToUpperCamelCase(GeneralUtility::camelCaseToLowerCaseUnderscored($string)); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * This will wrap each line of `$text` inside `<p></p>` tags. |
||
| 118 | * |
||
| 119 | * @param $text |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | |||
| 123 | public function wrapLines($text): string |
||
| 124 | { |
||
| 125 | $pad = function ($line) { |
||
| 126 | return "<p>$line</p>"; |
||
| 127 | }; |
||
| 128 | |||
| 129 | $text = preg_replace('/ ?#LF# ?/', "\n", $text); |
||
| 130 | $lines = explode("\n", $text); |
||
| 131 | $lines = array_map('trim', $lines); |
||
| 132 | $lines = array_filter($lines); |
||
| 133 | $lines = array_map($pad, $lines); |
||
| 134 | |||
| 135 | return implode('', $lines); |
||
| 136 | } |
||
| 137 | } |
||
| 138 |