]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenCountry.php
reintroduce cutoffs when searching for very frequent words
[nominatim.git] / lib-php / TokenCountry.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11 namespace Nominatim\Token;
12
13 /**
14  * A country token.
15  */
16 class Country
17 {
18     /// Database word id, if available.
19     private $iId;
20     /// Two-letter country code (lower-cased).
21     private $sCountryCode;
22
23     public function __construct($iId, $sCountryCode)
24     {
25         $this->iId = $iId;
26         $this->sCountryCode = $sCountryCode;
27     }
28
29     public function getId()
30     {
31         return $this->iId;
32     }
33
34     /**
35      * Check if the token can be added to the given search.
36      * Derive new searches by adding this token to an existing search.
37      *
38      * @param object  $oSearch      Partial search description derived so far.
39      * @param object  $oPosition    Description of the token position within
40                                     the query.
41      *
42      * @return True if the token is compatible with the search configuration
43      *         given the position.
44      */
45     public function isExtendable($oSearch, $oPosition)
46     {
47         return !$oSearch->hasCountry()
48                && $oPosition->maybePhrase('country')
49                && $oSearch->getContext()->isCountryApplicable($this->sCountryCode);
50     }
51
52     /**
53      * Derive new searches by adding this token to an existing search.
54      *
55      * @param object  $oSearch      Partial search description derived so far.
56      * @param object  $oPosition    Description of the token position within
57                                     the query.
58      *
59      * @return SearchDescription[] List of derived search descriptions.
60      */
61     public function extendSearch($oSearch, $oPosition)
62     {
63         $oNewSearch = $oSearch->clone($oPosition->isLastToken() ? 1 : 6);
64         $oNewSearch->setCountry($this->sCountryCode);
65
66         return array($oNewSearch);
67     }
68
69     public function debugInfo()
70     {
71         return array(
72                 'ID' => $this->iId,
73                 'Type' => 'country',
74                 'Info' => $this->sCountryCode
75                );
76     }
77
78     public function debugCode()
79     {
80         return 'C';
81     }
82 }