]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/ParameterParser.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / lib-php / 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             && 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         ) {
96             foreach ($aLanguagesParse as $iLang => $aLanguage) {
97                 $aLanguages[$aLanguage[1]] = isset($aLanguage[5])?(float)$aLanguage[5]:1 - ($iLang/100);
98                 if (!isset($aLanguages[$aLanguage[2]])) {
99                     $aLanguages[$aLanguage[2]] = $aLanguages[$aLanguage[1]]/10;
100                 }
101             }
102             arsort($aLanguages);
103         }
104         if (empty($aLanguages) && CONST_Default_Language) {
105             $aLanguages[CONST_Default_Language] = 1;
106         }
107
108         foreach ($aLanguages as $sLanguage => $fLanguagePref) {
109             $aLangPrefOrder['name:'.$sLanguage] = 'name:'.$sLanguage;
110         }
111         $aLangPrefOrder['name'] = 'name';
112         $aLangPrefOrder['brand'] = 'brand';
113         foreach ($aLanguages as $sLanguage => $fLanguagePref) {
114             $aLangPrefOrder['official_name:'.$sLanguage] = 'official_name:'.$sLanguage;
115             $aLangPrefOrder['short_name:'.$sLanguage] = 'short_name:'.$sLanguage;
116         }
117         $aLangPrefOrder['official_name'] = 'official_name';
118         $aLangPrefOrder['short_name'] = 'short_name';
119         $aLangPrefOrder['ref'] = 'ref';
120         $aLangPrefOrder['type'] = 'type';
121         return $aLangPrefOrder;
122     }
123
124     public function hasSetAny($aParamNames)
125     {
126         foreach ($aParamNames as $sName) {
127             if ($this->getBool($sName)) {
128                 return true;
129             }
130         }
131
132         return false;
133     }
134 }