ArrayHash.php
2 years ago
ArrayList.php
2 years ago
Arrays.php
2 years ago
Callback.php
2 years ago
DateTime.php
2 years ago
FileSystem.php
2 years ago
Floats.php
2 years ago
Helpers.php
2 years ago
Html.php
2 years ago
Image.php
2 years ago
Json.php
2 years ago
ObjectHelpers.php
2 years ago
ObjectMixin.php
2 years ago
Paginator.php
2 years ago
Random.php
2 years ago
Reflection.php
2 years ago
Strings.php
2 years ago
Type.php
2 years ago
Validators.php
2 years ago
exceptions.php
2 years ago
Helpers.php
79 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the Nette Framework (https://nette.org) |
| 5 | * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | */ |
| 7 | declare (strict_types=1); |
| 8 | namespace FapiMember\Library\Nette\Utils; |
| 9 | |
| 10 | use FapiMember\Library\Nette; |
| 11 | class Helpers |
| 12 | { |
| 13 | /** |
| 14 | * Executes a callback and returns the captured output as a string. |
| 15 | */ |
| 16 | public static function capture(callable $func): string |
| 17 | { |
| 18 | ob_start(function () { |
| 19 | }); |
| 20 | try { |
| 21 | $func(); |
| 22 | return ob_get_clean(); |
| 23 | } catch (\Throwable $e) { |
| 24 | ob_end_clean(); |
| 25 | throw $e; |
| 26 | } |
| 27 | } |
| 28 | /** |
| 29 | * Returns the last occurred PHP error or an empty string if no error occurred. Unlike error_get_last(), |
| 30 | * it is nit affected by the PHP directive html_errors and always returns text, not HTML. |
| 31 | */ |
| 32 | public static function getLastError(): string |
| 33 | { |
| 34 | $message = error_get_last()['message'] ?? ''; |
| 35 | $message = ini_get('html_errors') ? Html::htmlToText($message) : $message; |
| 36 | $message = preg_replace('#^\w+\(.*?\): #', '', $message); |
| 37 | return $message; |
| 38 | } |
| 39 | /** |
| 40 | * Converts false to null, does not change other values. |
| 41 | * @param mixed $value |
| 42 | * @return mixed |
| 43 | */ |
| 44 | public static function falseToNull($value) |
| 45 | { |
| 46 | return ($value === \false) ? null : $value; |
| 47 | } |
| 48 | /** |
| 49 | * Returns value clamped to the inclusive range of min and max. |
| 50 | * @param int|float $value |
| 51 | * @param int|float $min |
| 52 | * @param int|float $max |
| 53 | * @return int|float |
| 54 | */ |
| 55 | public static function clamp($value, $min, $max) |
| 56 | { |
| 57 | if ($min > $max) { |
| 58 | throw new Nette\InvalidArgumentException("Minimum ({$min}) is not less than maximum ({$max})."); |
| 59 | } |
| 60 | return min(max($value, $min), $max); |
| 61 | } |
| 62 | /** |
| 63 | * Looks for a string from possibilities that is most similar to value, but not the same (for 8-bit encoding). |
| 64 | * @param string[] $possibilities |
| 65 | */ |
| 66 | public static function getSuggestion(array $possibilities, string $value): ?string |
| 67 | { |
| 68 | $best = null; |
| 69 | $min = (strlen($value) / 4 + 1) * 10 + 0.1; |
| 70 | foreach (array_unique($possibilities) as $item) { |
| 71 | if ($item !== $value && ($len = levenshtein($item, $value, 10, 11, 10)) < $min) { |
| 72 | $min = $len; |
| 73 | $best = $item; |
| 74 | } |
| 75 | } |
| 76 | return $best; |
| 77 | } |
| 78 | } |
| 79 |