]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenSpecialTerm.php
factor out check if a token fits current search
[nominatim.git] / lib-php / TokenSpecialTerm.php
1 <?php
2
3 namespace Nominatim\Token;
4
5 require_once(CONST_LibDir.'/SpecialSearchOperator.php');
6
7 /**
8  * A word token describing a place type.
9  */
10 class SpecialTerm
11 {
12     /// Database word id, if applicable.
13     public $iId;
14     /// Class (or OSM tag key) of the place to look for.
15     public $sClass;
16     /// Type (or OSM tag value) of the place to look for.
17     public $sType;
18     /// Relationship of the operator to the object (see Operator class).
19     public $iOperator;
20
21     public function __construct($iID, $sClass, $sType, $iOperator)
22     {
23         $this->iId = $iID;
24         $this->sClass = $sClass;
25         $this->sType = $sType;
26         $this->iOperator = $iOperator;
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->hasOperator() && $oPosition->isPhrase('');
48     }
49
50     /**
51      * Derive new searches by adding this token to an existing search.
52      *
53      * @param object  $oSearch      Partial search description derived so far.
54      * @param object  $oPosition    Description of the token position within
55                                     the query.
56      *
57      * @return SearchDescription[] List of derived search descriptions.
58      */
59     public function extendSearch($oSearch, $oPosition)
60     {
61         $iSearchCost = 2;
62
63         $iOp = $this->iOperator;
64         if ($iOp == \Nominatim\Operator::NONE) {
65             if ($oSearch->hasName() || $oSearch->getContext()->isBoundedSearch()) {
66                 $iOp = \Nominatim\Operator::NAME;
67             } else {
68                 $iOp = \Nominatim\Operator::NEAR;
69             }
70             $iSearchCost += 2;
71         } elseif (!$oPosition->isFirstToken() && !$oPosition->isLastToken()) {
72             $iSearchCost += 2;
73         }
74         if ($oSearch->hasHousenumber()) {
75             $iSearchCost ++;
76         }
77
78         $oNewSearch = $oSearch->clone($iSearchCost);
79         $oNewSearch->setPoiSearch($iOp, $this->sClass, $this->sType);
80
81         return array($oNewSearch);
82     }
83
84
85     public function debugInfo()
86     {
87         return array(
88                 'ID' => $this->iId,
89                 'Type' => 'special term',
90                 'Info' => array(
91                            'class' => $this->sClass,
92                            'type' => $this->sType,
93                            'operator' => \Nominatim\Operator::toString($this->iOperator)
94                           )
95                );
96     }
97
98     public function debugCode()
99     {
100         return 'S';
101     }
102 }