1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Latte;
9:
10:
11: 12: 13: 14:
15: class Helpers
16: {
17:
18: public static $emptyElements = [
19: 'img' => 1, 'hr' => 1, 'br' => 1, 'input' => 1, 'meta' => 1, 'area' => 1, 'embed' => 1, 'keygen' => 1, 'source' => 1, 'base' => 1,
20: 'col' => 1, 'link' => 1, 'param' => 1, 'basefont' => 1, 'frame' => 1, 'isindex' => 1, 'wbr' => 1, 'command' => 1, 'track' => 1,
21: ];
22:
23:
24: 25: 26: 27:
28: public static function checkCallback($callable)
29: {
30: if (!is_callable($callable, false, $text)) {
31: throw new \InvalidArgumentException("Callback '$text' is not callable.");
32: }
33: return $callable;
34: }
35:
36:
37: 38: 39: 40:
41: public static function getSuggestion(array $items, $value)
42: {
43: $best = null;
44: $min = (strlen($value) / 4 + 1) * 10 + .1;
45: foreach (array_unique($items, SORT_REGULAR) as $item) {
46: $item = is_object($item) ? $item->getName() : $item;
47: if (($len = levenshtein($item, $value, 10, 11, 10)) > 0 && $len < $min) {
48: $min = $len;
49: $best = $item;
50: }
51: }
52: return $best;
53: }
54:
55:
56: 57: 58:
59: public static function removeFilter(&$modifier, $filter)
60: {
61: $modifier = preg_replace('#\|(' . $filter . ')\s?(?=\||\z)#i', '', $modifier, -1, $found);
62: return (bool) $found;
63: }
64:
65:
66: 67: 68: 69:
70: public static function startsWith($haystack, $needle)
71: {
72: return strncmp($haystack, $needle, strlen($needle)) === 0;
73: }
74: }
75: