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