]> git.openstreetmap.org Git - nominatim.git/blob - lib/ParameterParser.php
Merge branch 'patch-1' of https://github.com/ganeshkrishnan1/Nominatim into ganeshkri...
[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])) {
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])) {
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             // removes all NULL, FALSE and Empty Strings but leaves 0 (zero) values
78             return array_values(array_filter(explode(',', $sValue), 'strlen'));
79         }
80
81         return $aDefault;
82     }
83
84     public function getPreferredLanguages($sFallback = null)
85     {
86         if ($sFallback === null && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
87             $sFallback = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
88         }
89
90         $aLanguages = array();
91         $sLangString = $this->getString('accept-language', $sFallback);
92
93         if ($sLangString) {
94             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)) {
95                 foreach ($aLanguagesParse as $iLang => $aLanguage) {
96                     $aLanguages[$aLanguage[1]] = isset($aLanguage[5])?(float)$aLanguage[5]:1 - ($iLang/100);
97                     if (!isset($aLanguages[$aLanguage[2]])) $aLanguages[$aLanguage[2]] = $aLanguages[$aLanguage[1]]/10;
98                 }
99                 arsort($aLanguages);
100             }
101         }
102         if (empty($aLanguages) && CONST_Default_Language) {
103             $aLanguages[CONST_Default_Language] = 1;
104         }
105
106         foreach ($aLanguages as $sLanguage => $fLanguagePref) {
107             $aLangPrefOrder['name:'.$sLanguage] = 'name:'.$sLanguage;
108         }
109         $aLangPrefOrder['name'] = 'name';
110         $aLangPrefOrder['brand'] = 'brand';
111         foreach ($aLanguages as $sLanguage => $fLanguagePref) {
112             $aLangPrefOrder['official_name:'.$sLanguage] = 'official_name:'.$sLanguage;
113             $aLangPrefOrder['short_name:'.$sLanguage] = 'short_name:'.$sLanguage;
114         }
115         $aLangPrefOrder['official_name'] = 'official_name';
116         $aLangPrefOrder['short_name'] = 'short_name';
117         $aLangPrefOrder['ref'] = 'ref';
118         $aLangPrefOrder['type'] = 'type';
119         return $aLangPrefOrder;
120     }
121
122     public function hasSetAny($aParamNames)
123     {
124         foreach ($aParamNames as $sName) {
125             if ($this->getBool($sName)) {
126                 return true;
127             }
128         }
129
130         return false;
131     }
132 }