]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenSpecialTerm.php
Merge pull request #2793 from lonvia/increase-minimum-results
[nominatim.git] / lib-php / TokenSpecialTerm.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 require_once(CONST_LibDir.'/SpecialSearchOperator.php');
14
15 /**
16  * A word token describing a place type.
17  */
18 class SpecialTerm
19 {
20     /// Database word id, if applicable.
21     private $iId;
22     /// Class (or OSM tag key) of the place to look for.
23     private $sClass;
24     /// Type (or OSM tag value) of the place to look for.
25     private $sType;
26     /// Relationship of the operator to the object (see Operator class).
27     private $iOperator;
28
29     public function __construct($iID, $sClass, $sType, $iOperator)
30     {
31         $this->iId = $iID;
32         $this->sClass = $sClass;
33         $this->sType = $sType;
34         $this->iOperator = $iOperator;
35     }
36
37     public function getId()
38     {
39         return $this->iId;
40     }
41
42     /**
43      * Check if the token can be added to the given search.
44      * Derive new searches by adding this token to an existing search.
45      *
46      * @param object  $oSearch      Partial search description derived so far.
47      * @param object  $oPosition    Description of the token position within
48                                     the query.
49      *
50      * @return True if the token is compatible with the search configuration
51      *         given the position.
52      */
53     public function isExtendable($oSearch, $oPosition)
54     {
55         return !$oSearch->hasOperator()
56                && $oPosition->isPhrase('')
57                && ($this->iOperator != \Nominatim\Operator::NONE
58                   || (!$oSearch->hasAddress() && !$oSearch->hasHousenumber() && !$oSearch->hasCountry()));
59     }
60
61     /**
62      * Derive new searches by adding this token to an existing search.
63      *
64      * @param object  $oSearch      Partial search description derived so far.
65      * @param object  $oPosition    Description of the token position within
66                                     the query.
67      *
68      * @return SearchDescription[] List of derived search descriptions.
69      */
70     public function extendSearch($oSearch, $oPosition)
71     {
72         $iSearchCost = 2;
73
74         $iOp = $this->iOperator;
75         if ($iOp == \Nominatim\Operator::NONE) {
76             if ($oSearch->hasName() || $oSearch->getContext()->isBoundedSearch()) {
77                 $iOp = \Nominatim\Operator::NAME;
78             } else {
79                 $iOp = \Nominatim\Operator::NEAR;
80                 $iSearchCost += 2;
81             }
82         } elseif (!$oPosition->isFirstToken() && !$oPosition->isLastToken()) {
83             $iSearchCost += 2;
84         }
85         if ($oSearch->hasHousenumber()) {
86             $iSearchCost ++;
87         }
88
89         $oNewSearch = $oSearch->clone($iSearchCost);
90         $oNewSearch->setPoiSearch($iOp, $this->sClass, $this->sType);
91
92         return array($oNewSearch);
93     }
94
95
96     public function debugInfo()
97     {
98         return array(
99                 'ID' => $this->iId,
100                 'Type' => 'special term',
101                 'Info' => array(
102                            'class' => $this->sClass,
103                            'type' => $this->sType,
104                            'operator' => \Nominatim\Operator::toString($this->iOperator)
105                           )
106                );
107     }
108
109     public function debugCode()
110     {
111         return 'S';
112     }
113 }