Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
14 / 14 |
CRAP | |
100.00% |
73 / 73 |
| Formatting | |
100.00% |
1 / 1 |
|
100.00% |
14 / 14 |
33 | |
100.00% |
73 / 73 |
| setTwig | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| twigRenderString | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| twigRenderFile | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| normalizePath | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| moduleFromPath | |
100.00% |
1 / 1 |
4 | |
100.00% |
13 / 13 |
|||
| startsWith | |
100.00% |
1 / 1 |
2 | |
100.00% |
7 / 7 |
|||
| endsWith | |
100.00% |
1 / 1 |
3 | |
100.00% |
9 / 9 |
|||
| removePrefix | |
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
|||
| removeSuffix | |
100.00% |
1 / 1 |
3 | |
100.00% |
1 / 1 |
|||
| camelCaseToSnakeCase | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| snakeCaseToCamelCase | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| indent | |
100.00% |
1 / 1 |
3 | |
100.00% |
12 / 12 |
|||
| toString | |
100.00% |
1 / 1 |
3 | |
100.00% |
5 / 5 |
|||
| toArray | |
100.00% |
1 / 1 |
7 | |
100.00% |
18 / 18 |
|||
| 1 | <?php |
| 2 | |
| 3 | // {{{ License |
| 4 | // This file is part of GNU social - https://www.gnu.org/software/social |
| 5 | // |
| 6 | // GNU social is free software: you can redistribute it and/or modify |
| 7 | // it under the terms of the GNU Affero General Public License as published by |
| 8 | // the Free Software Foundation, either version 3 of the License, or |
| 9 | // (at your option) any later version. |
| 10 | // |
| 11 | // GNU social is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU Affero General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU Affero General Public License |
| 17 | // along with GNU social. If not, see <http://www.gnu.org/licenses/>. |
| 18 | // }}} |
| 19 | |
| 20 | /** |
| 21 | * String formatting utilities |
| 22 | * |
| 23 | * @package GNUsocial |
| 24 | * @category Util |
| 25 | * |
| 26 | * @author Hugo Sales <hugo@hsal.es> |
| 27 | * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org |
| 28 | * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later |
| 29 | */ |
| 30 | |
| 31 | namespace App\Util; |
| 32 | |
| 33 | use App\Core\Log; |
| 34 | use App\Util\Exception\ServerException; |
| 35 | use Functional as F; |
| 36 | use InvalidArgumentException; |
| 37 | |
| 38 | abstract class Formatting |
| 39 | { |
| 40 | private static ?\Twig\Environment $twig; |
| 41 | public static function setTwig(\Twig\Environment $twig) |
| 42 | { |
| 43 | self::$twig = $twig; |
| 44 | } |
| 45 | |
| 46 | public static function twigRenderString(string $template, array $context): string |
| 47 | { |
| 48 | return self::$twig->createTemplate($template, null)->render($context); |
| 49 | } |
| 50 | |
| 51 | public static function twigRenderFile(string $template_path, array $context): string |
| 52 | { |
| 53 | return self::$twig->render($template_path, $context); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Normalize path by converting \ to / |
| 58 | * |
| 59 | * @param string $path |
| 60 | * |
| 61 | * @return string |
| 62 | */ |
| 63 | public static function normalizePath(string $path): string |
| 64 | { |
| 65 | return preg_replace(',(/|\\\\)+,', '/', $path); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get plugin name from it's path, or null if not a plugin |
| 70 | * |
| 71 | * @param string $path |
| 72 | * |
| 73 | * @return null|string |
| 74 | */ |
| 75 | public static function moduleFromPath(string $path): ?string |
| 76 | { |
| 77 | foreach (['/plugins/', '/components/'] as $mod_p) { |
| 78 | $module = strpos($path, $mod_p); |
| 79 | if ($module === false) { |
| 80 | continue; |
| 81 | } |
| 82 | $cut = $module + strlen($mod_p); |
| 83 | $cut2 = strpos($path, '/', $cut); |
| 84 | if ($cut2) { |
| 85 | $final = substr($path, $cut, $cut2 - $cut); |
| 86 | } else { |
| 87 | // We might be running directly from the plugins dir? |
| 88 | // If so, there's no place to store locale info. |
| 89 | $m = 'The GNU social install dir seems to contain a piece named \'plugin\' or \'component\''; |
| 90 | Log::critical($m); |
| 91 | throw new ServerException($m); |
| 92 | } |
| 93 | return $final; |
| 94 | } |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Check whether $haystack starts with $needle |
| 100 | * |
| 101 | * @param array|string $haystack if array, check that all strings start with $needle |
| 102 | * @param string $needle |
| 103 | * |
| 104 | * @return bool |
| 105 | */ |
| 106 | public static function startsWith($haystack, string $needle): bool |
| 107 | { |
| 108 | if (is_string($haystack)) { |
| 109 | $length = strlen($needle); |
| 110 | return substr($haystack, 0, $length) === $needle; |
| 111 | } |
| 112 | return F\every($haystack, |
| 113 | function ($haystack) use ($needle) { |
| 114 | return self::startsWith($haystack, $needle); |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Check whether $haystack ends with $needle |
| 120 | * |
| 121 | * @param array|string $haystack if array, check that all strings end with $needle |
| 122 | * @param string $needle |
| 123 | * |
| 124 | * @return bool |
| 125 | */ |
| 126 | public static function endsWith($haystack, string $needle) |
| 127 | { |
| 128 | if (is_string($haystack)) { |
| 129 | $length = strlen($needle); |
| 130 | if ($length == 0) { |
| 131 | return true; |
| 132 | } |
| 133 | return substr($haystack, -$length) === $needle; |
| 134 | } |
| 135 | return F\every($haystack, |
| 136 | function ($haystack) use ($needle) { |
| 137 | return self::endsWith($haystack, $needle); |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * If $haystack starts with $needle, remove it from the beginning |
| 143 | */ |
| 144 | public static function removePrefix(string $haystack, string $needle) |
| 145 | { |
| 146 | return self::startsWith($haystack, $needle) ? substr($haystack, strlen($needle)) : $haystack; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * If $haystack ends with $needle, remove it from the end |
| 151 | */ |
| 152 | public static function removeSuffix(string $haystack, string $needle) |
| 153 | { |
| 154 | return self::endsWith($haystack, $needle) && !empty($needle) ? substr($haystack, 0, -strlen($needle)) : $haystack; |
| 155 | } |
| 156 | |
| 157 | public static function camelCaseToSnakeCase(string $str): string |
| 158 | { |
| 159 | return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $str)); |
| 160 | } |
| 161 | |
| 162 | public static function snakeCaseToCamelCase(string $str): string |
| 163 | { |
| 164 | return implode('', F\map(preg_split('/[\b_]/', $str), F\ary('ucfirst', 1))); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Indent $in, a string or array, $level levels |
| 169 | * |
| 170 | * @param array|string $in |
| 171 | * @param int $level How many levels of indentation |
| 172 | * @param int $count How many spaces per indentation |
| 173 | * |
| 174 | * @return string |
| 175 | */ |
| 176 | public static function indent($in, int $level = 1, int $count = 2): string |
| 177 | { |
| 178 | if (is_string($in)) { |
| 179 | return self::indent(explode("\n", $in), $level, $count); |
| 180 | } elseif (is_array($in)) { |
| 181 | $indent = str_repeat(' ', $count * $level); |
| 182 | return implode("\n", F\map(F\select($in, |
| 183 | F\ary(function ($s) { |
| 184 | return $s != ''; |
| 185 | }, 1)), |
| 186 | function ($val) use ($indent) { |
| 187 | return F\concat($indent . $val); |
| 188 | })); |
| 189 | } |
| 190 | throw new InvalidArgumentException('Formatting::indent\'s first parameter must be either an array or a string. Input was: ' . $in); |
| 191 | } |
| 192 | |
| 193 | const SPLIT_BY_SPACE = ' '; |
| 194 | const JOIN_BY_SPACE = ' '; |
| 195 | const SPLIT_BY_COMMA = ', '; |
| 196 | const JOIN_BY_COMMA = ', '; |
| 197 | const SPLIT_BY_BOTH = '/[, ]/'; |
| 198 | |
| 199 | /** |
| 200 | * Convert scalars, objects implementing __toString or arrays to strings |
| 201 | * |
| 202 | * @param mixed $value |
| 203 | */ |
| 204 | public static function toString($value, string $join_type = self::JOIN_BY_COMMA): string |
| 205 | { |
| 206 | if (!in_array($join_type, [static::JOIN_BY_SPACE, static::JOIN_BY_COMMA])) { |
| 207 | throw new \Exception('Formatting::toString received invalid join option'); |
| 208 | } else { |
| 209 | if (!is_array($value)) { |
| 210 | return (string) $value; |
| 211 | } else { |
| 212 | return implode($join_type, $value); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Convert a user supplied string to array and return whether the conversion was successfull |
| 219 | * |
| 220 | * @param mixed $output |
| 221 | */ |
| 222 | public static function toArray(string $input, &$output, string $split_type = self::SPLIT_BY_COMMA): bool |
| 223 | { |
| 224 | if (!in_array($split_type, [static::SPLIT_BY_SPACE, static::SPLIT_BY_COMMA, static::SPLIT_BY_BOTH])) { |
| 225 | throw new \Exception('Formatting::toArray received invalid split option'); |
| 226 | } |
| 227 | if ($input == '') { |
| 228 | $output = []; |
| 229 | return true; |
| 230 | } |
| 231 | $matches = []; |
| 232 | if (preg_match('/^ *\[?([^,]+(, ?[^,]+)*)\]? *$/', $input, $matches)) { |
| 233 | switch ($split_type) { |
| 234 | case self::SPLIT_BY_BOTH: |
| 235 | $arr = preg_split($split_type, $matches[1], 0, PREG_SPLIT_NO_EMPTY); |
| 236 | break; |
| 237 | case self::SPLIT_BY_COMMA: |
| 238 | $arr = preg_split('/, ?/', $matches[1]); |
| 239 | break; |
| 240 | default: |
| 241 | $arr = explode($split_type[0], $matches[1]); |
| 242 | } |
| 243 | $output = str_replace([' \'', '\'', ' "', '"'], '', $arr); |
| 244 | $output = F\map($output, F\ary('trim', 1)); |
| 245 | return true; |
| 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | } |