]> git.openstreetmap.org Git - nominatim.git/blob - lib/ParameterParser.php
0c0471a39b732b185573d51a22295173ede572ec
[nominatim.git] / lib / ParameterParser.php
1 <?php
2
3 class ParameterParser
4 {
5     private $aParams;
6
7
8     function __construct($aParams = null)
9     {
10         $this->aParams = ($aParams === null) ? $_GET : $aParams;
11     }
12
13     function getBool($sName, $bDefault = false)
14     {
15         if (!isset($this->aParams[$sName]) || strlen($this->aParams[$sName]) == 0) {
16             return $bDefault;
17         }
18
19         return (bool) $this->aParams[$sName];
20     }
21
22     function getInt($sName, $bDefault = false)
23     {
24         if (!isset($this->aParams[$sName]) || strlen($this->aParams[$sName]) == 0) {
25             return $bDefault;
26         }
27
28         if (!preg_match('/^[+-]?[0-9]+$/', $this->aParams[$sName])) {
29             userError("Integer number expected for parameter '$sName'");
30         }
31
32         return (int) $this->aParams[$sName];
33     }
34
35     function getFloat($sName, $bDefault = false)
36     {
37         if (!isset($this->aParams[$sName]) || strlen($this->aParams[$sName]) == 0) {
38             return $bDefault;
39         }
40
41         if (!preg_match('/^[+-]?[0-9]*\.?[0-9]+$/', $this->aParams[$sName])) {
42             userError("Floating-point number expected for parameter '$sName'");
43         }
44
45         return (float) $this->aParams[$sName];
46     }
47
48     function getString($sName, $bDefault = false)
49     {
50         if (!isset($this->aParams[$sName]) || strlen($this->aParams[$sName]) == 0) {
51             return $bDefault;
52         }
53
54         return $this->aParams[$sName];
55     }
56
57     function getSet($sName, $aValues, $sDefault = false)
58     {
59         if (!isset($this->aParams[$sName]) || strlen($this->aParams[$sName]) == 0) {
60             return $sDefault;
61         }
62
63         if (!in_array($this->aParams[$sName], $aValues)) {
64             userError("Parameter '$sName' must be one of: ".join(', ', $aValues));
65         }
66
67         return $this->aParams[$sName];
68     }
69
70     function getStringList($sName, $aDefault = false)
71     {
72         $sValue = $this->getString($sName);
73
74         if ($sValue) {
75             return explode(',', $sValue);
76         }
77
78         return $aDefault;
79     }
80
81     function getPreferredLanguages($sFallback = null)
82     {
83         if ($sFallback === null && isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
84             $sFallback = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
85         }
86
87         $aLanguages = array();
88         $sLangString = $this->getString('accept-language', $sFallback);
89
90         if ($sLangString) {
91             if (preg_match_all('/(([a-z]{1,8})(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $sLangString, $aLanguagesParse, PREG_SET_ORDER)) {
92                 foreach ($aLanguagesParse as $iLang => $aLanguage) {
93                     $aLanguages[$aLanguage[1]] = isset($aLanguage[5])?(float)$aLanguage[5]:1 - ($iLang/100);
94                     if (!isset($aLanguages[$aLanguage[2]])) $aLanguages[$aLanguage[2]] = $aLanguages[$aLanguage[1]]/10;
95                 }
96                 arsort($aLanguages);
97             }
98         }
99         if (!sizeof($aLanguages) && CONST_Default_Language) {
100             $aLanguages[CONST_Default_Language] = 1;
101         }
102
103         foreach ($aLanguages as $sLanguage => $fLanguagePref) {
104             $aLangPrefOrder['short_name:'.$sLanguage] = 'short_name:'.$sLanguage;
105             $aLangPrefOrder['name:'.$sLanguage] = 'name:'.$sLanguage;
106         }
107         $aLangPrefOrder['short_name'] = 'short_name';
108         $aLangPrefOrder['name'] = 'name';
109         $aLangPrefOrder['brand'] = 'brand';
110         foreach ($aLanguages as $sLanguage => $fLanguagePref) {
111             $aLangPrefOrder['official_name:'.$sLanguage] = 'official_name:'.$sLanguage;
112         }
113         $aLangPrefOrder['official_name'] = 'official_name';
114         $aLangPrefOrder['ref'] = 'ref';
115         $aLangPrefOrder['type'] = 'type';
116         return $aLangPrefOrder;
117     }
118 }