3 * SPDX-License-Identifier: GPL-2.0-only
5 * This file is part of Nominatim. (https://nominatim.org)
7 * Copyright (C) 2022 by the Nominatim developer community.
8 * For a full list of authors see the git log.
18 public function __construct($aParams = null)
20 $this->aParams = ($aParams === null) ? $_GET : $aParams;
23 public function getBool($sName, $bDefault = false)
25 if (!isset($this->aParams[$sName])
26 || !is_string($this->aParams[$sName])
27 || strlen($this->aParams[$sName]) == 0
32 return (bool) $this->aParams[$sName];
35 public function getInt($sName, $bDefault = false)
37 if (!isset($this->aParams[$sName]) || is_array($this->aParams[$sName])) {
41 if (!preg_match('/^[+-]?[0-9]+$/', $this->aParams[$sName])) {
42 userError("Integer number expected for parameter '$sName'");
45 return (int) $this->aParams[$sName];
48 public function getFloat($sName, $bDefault = false)
50 if (!isset($this->aParams[$sName]) || is_array($this->aParams[$sName])) {
54 if (!preg_match('/^[+-]?[0-9]*\.?[0-9]+$/', $this->aParams[$sName])) {
55 userError("Floating-point number expected for parameter '$sName'");
58 return (float) $this->aParams[$sName];
61 public function getString($sName, $bDefault = false)
63 if (!isset($this->aParams[$sName])
64 || !is_string($this->aParams[$sName])
65 || strlen($this->aParams[$sName]) == 0
70 return $this->aParams[$sName];
73 public function getSet($sName, $aValues, $sDefault = false)
75 if (!isset($this->aParams[$sName])
76 || !is_string($this->aParams[$sName])
77 || strlen($this->aParams[$sName]) == 0
82 if (!in_array($this->aParams[$sName], $aValues, true)) {
83 userError("Parameter '$sName' must be one of: ".join(', ', $aValues));
86 return $this->aParams[$sName];
89 public function getStringList($sName, $aDefault = false)
91 $sValue = $this->getString($sName);
94 // removes all NULL, FALSE and Empty Strings but leaves 0 (zero) values
95 return array_values(array_filter(explode(',', $sValue), 'strlen'));
101 public function getPreferredLanguages($sFallback = null)
103 if ($sFallback === null && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
104 $sFallback = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
107 $aLanguages = array();
108 $sLangString = $this->getString('accept-language', $sFallback);
111 && 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)
113 foreach ($aLanguagesParse as $iLang => $aLanguage) {
114 $aLanguages[$aLanguage[1]] = isset($aLanguage[5])?(float)$aLanguage[5]:1 - ($iLang/100);
115 if (!isset($aLanguages[$aLanguage[2]])) {
116 $aLanguages[$aLanguage[2]] = $aLanguages[$aLanguage[1]]/10;
121 if (empty($aLanguages) && CONST_Default_Language) {
122 $aLanguages[CONST_Default_Language] = 1;
125 foreach ($aLanguages as $sLanguage => $fLanguagePref) {
126 $this->addNameTag($aLangPrefOrder, 'name:'.$sLanguage);
128 $this->addNameTag($aLangPrefOrder, 'name');
129 $this->addNameTag($aLangPrefOrder, 'brand');
130 foreach ($aLanguages as $sLanguage => $fLanguagePref) {
131 $this->addNameTag($aLangPrefOrder, 'official_name:'.$sLanguage);
132 $this->addNameTag($aLangPrefOrder, 'short_name:'.$sLanguage);
134 $this->addNameTag($aLangPrefOrder, 'official_name');
135 $this->addNameTag($aLangPrefOrder, 'short_name');
136 $this->addNameTag($aLangPrefOrder, 'ref');
137 $this->addNameTag($aLangPrefOrder, 'type');
138 return $aLangPrefOrder;
141 private function addNameTag(&$aLangPrefOrder, $sTag)
143 $aLangPrefOrder[$sTag] = $sTag;
144 $aLangPrefOrder['_place_'.$sTag] = '_place_'.$sTag;
147 public function hasSetAny($aParamNames)
149 foreach ($aParamNames as $sName) {
150 if ($this->getBool($sName)) {