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