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