Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
226 | daniel-mar | 1 | <?php |
2 | |||
3 | # if (!interface_exists('VolcanoSearchProvider')) throw new Exception('Required interface "VolcanoSearchProvider" not found.'); |
||
4 | # if (!class_exists('VolcanoDB')) throw new Exception('Required class "VolcanoDB" not found.'); |
||
5 | |||
6 | require_once __DIR__ . '/../../core/VolcanoSearchProvider.class.php'; |
||
7 | |||
8 | class VolcanoSearchUUID implements VolcanoSearchProvider { |
||
9 | public static function checkId($id) { |
||
10 | return self::strEqual($id, 'guid', true) || self::strEqual($id, 'uuid', true); // is always case insensitive |
||
11 | } |
||
12 | |||
13 | protected static function tryNormalize($term) { |
||
14 | // TODO: also validate syntax |
||
15 | $term = str_replace('{', '', $term); |
||
16 | $term = str_replace('}', '', $term); |
||
17 | $term = strtolower($term); |
||
18 | return $term; |
||
19 | } |
||
20 | |||
21 | public static function calcDistance($candidate, $searchterm) { |
||
22 | $candidate = self::tryNormalize($candidate); |
||
23 | $searchterm = self::tryNormalize($searchterm); |
||
24 | |||
25 | if ($candidate == $searchterm) { |
||
26 | return 0; |
||
27 | } else { |
||
28 | return false; |
||
29 | } |
||
30 | } |
||
31 | |||
32 | protected static function strEqual($str1, $str2, $caseInsensitive = false) { |
||
33 | if ($caseInsensitive) { |
||
34 | return strtolower($str1) == strtolower($str2); |
||
35 | } else { |
||
36 | return $str1 == $str2; |
||
37 | } |
||
38 | } |
||
39 | } |
||
40 | |||
41 | require_once __DIR__ . '/../../core/1_VolcanoDB.class.php'; |
||
42 | VolcanoDB::registerSearchProvider(new VolcanoSearchUUID()); |
||
43 | |||
44 | # --- TEST |
||
45 | |||
46 | /* |
||
47 | $x = new VolcanoSearchUUID(); |
||
48 | |||
49 | assert($x->calcDistance('fd3c657c-0728-4769-b608-db5ece442c97', '8338af1c-61ea-41c1-aded-c836846ae22d') === false); |
||
50 | assert($x->calcDistance('fd3c657c-0728-4769-b608-db5ece442c97', 'fd3c657c-0728-4769-b608-db5ece442c97') === 0); |
||
51 | assert($x->calcDistance('fd3c657c-0728-4769-b608-db5ece442c97', '{FD3c657c-0728-4769-b608-db5ece442C97}') === 0); |
||
52 | assert($x->calcDistance('{fd3C657C-0728-4769-b608-db5ece442c97}', 'fd3c657c-0728-4769-b608-db5ece442c97') === 0); |
||
53 | */ |
||
54 |