PluginProbe
Packeta / 1.6.1
Packeta v1.6.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / utils / src / Utils / Helpers.php

Helpers.php in Packeta 1.6.1, at deps/nette/utils/src/Utils/Helpers.php

64 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 Packetery\Nette\Utils;
9
10 class Helpers
11 {
12 /**
13 * Executes a callback and returns the captured output as a string.
14 */
15 public static function capture(callable $func) : string
16 {
17 \ob_start(function () {
18 });
19 try {
20 $func();
21 return \ob_get_clean();
22 } catch (\Throwable $e) {
23 \ob_end_clean();
24 throw $e;
25 }
26 }
27 /**
28 * Returns the last occurred PHP error or an empty string if no error occurred. Unlike error_get_last(),
29 * it is nit affected by the PHP directive html_errors and always returns text, not HTML.
30 */
31 public static function getLastError() : string
32 {
33 $message = \error_get_last()['message'] ?? '';
34 $message = \ini_get('html_errors') ? Html::htmlToText($message) : $message;
35 $message = \preg_replace('#^\\w+\\(.*?\\): #', '', $message);
36 return $message;
37 }
38 /**
39 * Converts false to null, does not change other values.
40 * @param mixed $value
41 * @return mixed
42 */
43 public static function falseToNull($value)
44 {
45 return $value === \false ? null : $value;
46 }
47 /**
48 * Looks for a string from possibilities that is most similar to value, but not the same (for 8-bit encoding).
49 * @param string[] $possibilities
50 */
51 public static function getSuggestion(array $possibilities, string $value) : ?string
52 {
53 $best = null;
54 $min = (\strlen($value) / 4 + 1) * 10 + 0.1;
55 foreach (\array_unique($possibilities) as $item) {
56 if ($item !== $value && ($len = \levenshtein($item, $value, 10, 11, 10)) < $min) {
57 $min = $len;
58 $best = $item;
59 }
60 }
61 return $best;
62 }
63 }
64