]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenSpecialTerm.php
split cli tests by subcommand and extend coverage
[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     private $iId;
14     /// Class (or OSM tag key) of the place to look for.
15     private $sClass;
16     /// Type (or OSM tag value) of the place to look for.
17     private $sType;
18     /// Relationship of the operator to the object (see Operator class).
19     private $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()
48                && $oPosition->isPhrase('')
49                && ($this->iOperator != \Nominatim\Operator::NONE
50                   || (!$oSearch->hasAddress() && !$oSearch->hasHousenumber() && !$oSearch->hasCountry()));
51     }
52
53     /**
54      * Derive new searches by adding this token to an existing search.
55      *
56      * @param object  $oSearch      Partial search description derived so far.
57      * @param object  $oPosition    Description of the token position within
58                                     the query.
59      *
60      * @return SearchDescription[] List of derived search descriptions.
61      */
62     public function extendSearch($oSearch, $oPosition)
63     {
64         $iSearchCost = 2;
65
66         $iOp = $this->iOperator;
67         if ($iOp == \Nominatim\Operator::NONE) {
68             if ($oSearch->hasName() || $oSearch->getContext()->isBoundedSearch()) {
69                 $iOp = \Nominatim\Operator::NAME;
70             } else {
71                 $iOp = \Nominatim\Operator::NEAR;
72                 $iSearchCost += 2;
73             }
74         } elseif (!$oPosition->isFirstToken() && !$oPosition->isLastToken()) {
75             $iSearchCost += 2;
76         }
77         if ($oSearch->hasHousenumber()) {
78             $iSearchCost ++;
79         }
80
81         $oNewSearch = $oSearch->clone($iSearchCost);
82         $oNewSearch->setPoiSearch($iOp, $this->sClass, $this->sType);
83
84         return array($oNewSearch);
85     }
86
87
88     public function debugInfo()
89     {
90         return array(
91                 'ID' => $this->iId,
92                 'Type' => 'special term',
93                 'Info' => array(
94                            'class' => $this->sClass,
95                            'type' => $this->sType,
96                            'operator' => \Nominatim\Operator::toString($this->iOperator)
97                           )
98                );
99     }
100
101     public function debugCode()
102     {
103         return 'S';
104     }
105 }