Subversion Repositories oidplus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
226 daniel-mar 1
<?php
2
 
3
# TODO: das kann ohne suffix auskommen: http://dx.doi.org/10.1038/
4
 
5
# if (!interface_exists('VolcanoSearchProvider')) throw new Exception('Required interface "VolcanoSearchProvider" not found.');
6
# if (!class_exists('VolcanoDB')) throw new Exception('Required class "VolcanoDB" not found.');
7
 
8
require_once __DIR__ . '/../../core/VolcanoSearchProvider.class.php';
9
 
10
class VolcanoSearchDOI implements VolcanoSearchProvider {
11
        public static function checkId($id) {
12
                return self::strEqual($id, 'doi', true); // is always case insensitive
13
        }
14
 
15
        public static function calcDistance($candidate, $searchterm) {
16
                $y = explode('/', $searchterm);
17
                $x = explode('/', $candidate);
18
                $dist = count($y)-count($x);
19
 
20
                for ($i=0; $i<min(count($x),count($y)); $i++) {
21
                        if ($x[$i] != $y[$i]) return false;
22
                }
23
 
24
                return $dist;
25
        }
26
 
27
        protected static function strEqual($str1, $str2, $caseInsensitive = false) {
28
                if ($caseInsensitive) {
29
                        return strtolower($str1) == strtolower($str2);
30
                } else {
31
                        return $str1 == $str2;
32
                }
33
        }
34
}
35
 
36
require_once __DIR__ . '/../../core/1_VolcanoDB.class.php';
37
VolcanoDB::registerSearchProvider(new VolcanoSearchDOI());
38
 
39
# --- TEST
40
 
41
$x = new VolcanoSearchDOI();
42
 
43
assert($x->calcDistance('10.1000',  '10.1000/1') ==  1);
44
assert($x->calcDistance('10.1000/1',  '10.1000') ==  -1);
45
assert($x->calcDistance('10.1000',  '10.1000') ==  0);
46
assert($x->calcDistance('10.1001',  '10.1000') ===  false);
47
 
48
assert($x->calcDistance('10.1000/1/2/3',  '10.1000/1/2/3/4/5') ==  2);
49
assert($x->calcDistance('10.1000/1/2/3',  '10.1000/1/2/3') ==  0);
50
assert($x->calcDistance('10.1000/1/2/3/4/5',  '10.1000/1/2/3') ==  -2);
51
 
52
assert($x->calcDistance('10.1000/1/2/x',  '10.1000/1/2/3/4/5') ===  false);
53
assert($x->calcDistance('10.1000/1/2/x',  '10.1000/1/2/3') ===  false);
54
assert($x->calcDistance('10.1000/1/2/x/4/5',  '10.1000/1/2/3') ===  false);