]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenSpecialTerm.php
89dfa02619447f78106eb16eb2139e8557c8ab4b
[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      * Derive new searches by adding this token to an existing search.
36      *
37      * @param object  $oSearch      Partial search description derived so far.
38      * @param object  $oPosition    Description of the token position within
39                                     the query.
40      *
41      * @return SearchDescription[] List of derived search descriptions.
42      */
43     public function extendSearch($oSearch, $oPosition)
44     {
45         if ($oSearch->hasOperator() || !$oPosition->isPhrase('')) {
46             return array();
47         }
48
49         $iSearchCost = 2;
50
51         $iOp = $this->iOperator;
52         if ($iOp == \Nominatim\Operator::NONE) {
53             if ($oSearch->hasName() || $oSearch->getContext()->isBoundedSearch()) {
54                 $iOp = \Nominatim\Operator::NAME;
55             } else {
56                 $iOp = \Nominatim\Operator::NEAR;
57             }
58             $iSearchCost += 2;
59         } elseif (!$oPosition->isFirstToken() && !$oPosition->isLastToken()) {
60             $iSearchCost += 2;
61         }
62         if ($oSearch->hasHousenumber()) {
63             $iSearchCost ++;
64         }
65
66         $oNewSearch = $oSearch->clone($iSearchCost);
67         $oNewSearch->setPoiSearch($iOp, $this->sClass, $this->sType);
68
69         return array($oNewSearch);
70     }
71
72
73     public function debugInfo()
74     {
75         return array(
76                 'ID' => $this->iId,
77                 'Type' => 'special term',
78                 'Info' => array(
79                            'class' => $this->sClass,
80                            'type' => $this->sType,
81                            'operator' => \Nominatim\Operator::toString($this->iOperator)
82                           )
83                );
84     }
85
86     public function debugCode()
87     {
88         return 'S';
89     }
90 }