1 <?php
2
3 /*
4 * This file is part of the Icybee package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Icybee\Modules\Nodes;
13
14 /**
15 * Converts a string into a slug.
16 *
17 * A slug is the part of a URL which identifies a page using human-readable keywords. It is
18 * common practice to make the slug all lowercase, accented characters are usually replaced
19 * by letters from the English alphabet, punctuation marks are generally removed, and long
20 * page titles may also be truncated to keep the final URL to a reasonable length.
21 *
22 * @param string $str The string to convert into a slug.
23 * @param string $language The language of the string.
24 *
25 * @return string
26 *
27 * @see http://en.wikipedia.org/wiki/Clean_URL
28 */
29 function slugize($str, $language=null)
30 {
31 return Helpers::slugize($str, $language);
32 }
33
34 class Helpers
35 {
36 static private $jumptable = [
37
38 'slugize' => [ __CLASS__, 'slugize' ]
39
40 ];
41
42 /**
43 * Calls the callback of a patchable function.
44 *
45 * @param string $name Name of the function.
46 * @param array $arguments Arguments.
47 *
48 * @return mixed
49 */
50 static public function __callstatic($name, array $arguments)
51 {
52 return call_user_func_array(self::$jumptable[$name], $arguments);
53 }
54
55 /**
56 * Patches a patchable function.
57 *
58 * @param string $name Name of the function.
59 * @param collable $callback Callback.
60 *
61 * @throws \RuntimeException is attempt to patch an undefined function.
62 */
63 static public function patch($name, $callback)
64 {
65 if (empty(self::$jumptable[$name]))
66 {
67 throw new \RuntimeException("Undefined patchable: $name.");
68 }
69
70 self::$jumptable[$name] = $callback;
71 }
72
73 /*
74 * Default implementations
75 */
76
77 static private function slugize($str, $language=null)
78 {
79 return \ICanBoogie\normalize($str);
80 }
81 }