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]) || strlen($this->aParams[$sName]) == 0) {
29 return (bool) $this->aParams[$sName];
32 public function getInt($sName, $bDefault = false)
34 if (!isset($this->aParams[$sName])) {
38 if (!preg_match('/^[+-]?[0-9]+$/', $this->aParams[$sName])) {
39 userError("Integer number expected for parameter '$sName'");
42 return (int) $this->aParams[$sName];
45 public function getFloat($sName, $bDefault = false)
47 if (!isset($this->aParams[$sName])) {
51 if (!preg_match('/^[+-]?[0-9]*\.?[0-9]+$/', $this->aParams[$sName])) {
52 userError("Floating-point number expected for parameter '$sName'");
55 return (float) $this->aParams[$sName];
58 public function getString($sName, $bDefault = false)
60 if (!isset($this->aParams[$sName]) || strlen($this->aParams[$sName]) == 0) {
64 return $this->aParams[$sName];
67 public function getSet($sName, $aValues, $sDefault = false)
69 if (!isset($this->aParams[$sName]) || strlen($this->aParams[$sName]) == 0) {
73 if (!in_array($this->aParams[$sName], $aValues, true)) {
74 userError("Parameter '$sName' must be one of: ".join(', ', $aValues));
77 return $this->aParams[$sName];
80 public function getStringList($sName, $aDefault = false)
82 $sValue = $this->getString($sName);
85 // removes all NULL, FALSE and Empty Strings but leaves 0 (zero) values
86 return array_values(array_filter(explode(',', $sValue), 'strlen'));
92 public function getPreferredLanguages($sFallback = null)
94 if ($sFallback === null && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
95 $sFallback = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
98 $aLanguages = array();
99 $sLangString = $this->getString('accept-language', $sFallback);
102 && 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)
104 foreach ($aLanguagesParse as $iLang => $aLanguage) {
105 $aLanguages[$aLanguage[1]] = isset($aLanguage[5])?(float)$aLanguage[5]:1 - ($iLang/100);
106 if (!isset($aLanguages[$aLanguage[2]])) {
107 $aLanguages[$aLanguage[2]] = $aLanguages[$aLanguage[1]]/10;
112 if (empty($aLanguages) && CONST_Default_Language) {
113 $aLanguages[CONST_Default_Language] = 1;
116 foreach ($aLanguages as $sLanguage => $fLanguagePref) {
117 $this->addNameTag($aLangPrefOrder, 'name:'.$sLanguage);
119 $this->addNameTag($aLangPrefOrder, 'name');
120 $this->addNameTag($aLangPrefOrder, 'brand');
121 foreach ($aLanguages as $sLanguage => $fLanguagePref) {
122 $this->addNameTag($aLangPrefOrder, 'official_name:'.$sLanguage);
123 $this->addNameTag($aLangPrefOrder, 'short_name:'.$sLanguage);
125 $this->addNameTag($aLangPrefOrder, 'official_name');
126 $this->addNameTag($aLangPrefOrder, 'short_name');
127 $this->addNameTag($aLangPrefOrder, 'ref');
128 $this->addNameTag($aLangPrefOrder, 'type');
129 return $aLangPrefOrder;
132 private function addNameTag(&$aLangPrefOrder, $sTag)
134 $aLangPrefOrder[$sTag] = $sTag;
135 $aLangPrefOrder['_place_'.$sTag] = '_place_'.$sTag;
138 public function hasSetAny($aParamNames)
140 foreach ($aParamNames as $sName) {
141 if ($this->getBool($sName)) {