]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenHousenumber.php
move SearchDescription building into tokens
[nominatim.git] / lib-php / TokenHousenumber.php
1 <?php
2
3 namespace Nominatim\Token;
4
5 /**
6  * A house number token.
7  */
8 class HouseNumber
9 {
10     /// Database word id, if available.
11     private $iId;
12     /// Normalized house number.
13     private $sToken;
14
15     public function __construct($iId, $sToken)
16     {
17         $this->iId = $iId;
18         $this->sToken = $sToken;
19     }
20
21     public function getId()
22     {
23         return $this->iId;
24     }
25
26     /**
27      * Derive new searches by adding this token to an existing search.
28      *
29      * @param object  $oSearch      Partial search description derived so far.
30      * @param object  $oPosition    Description of the token position within
31                                     the query.
32      *
33      * @return SearchDescription[] List of derived search descriptions.
34      */
35     public function extendSearch($oSearch, $oPosition)
36     {
37         $aNewSearches = array();
38
39         if ($oSearch->hasHousenumber()
40             || $oSearch->hasOperator(\Nominatim\Operator::POSTCODE)
41             || !$oPosition->maybePhrase('street')
42         ) {
43             return $aNewSearches;
44         }
45
46         // sanity check: if the housenumber is not mainly made
47         // up of numbers, add a penalty
48         $iSearchCost = 1;
49         if (preg_match('/\\d/', $this->sToken) === 0
50             || preg_match_all('/[^0-9]/', $this->sToken, $aMatches) > 2) {
51             $iSearchCost++;
52         }
53         if (!$oSearch->hasOperator(\Nominatim\Operator::NONE)) {
54             $iSearchCost++;
55         }
56         if (empty($this->iId)) {
57             $iSearchCost++;
58         }
59         // also must not appear in the middle of the address
60         if ($oSearch->hasAddress() || $oSearch->hasPostcode()) {
61             $iSearchCost++;
62         }
63
64         $oNewSearch = $oSearch->clone($iSearchCost);
65         $oNewSearch->setHousenumber($this->sToken);
66         $aNewSearches[] = $oNewSearch;
67
68         // Housenumbers may appear in the name when the place has its own
69         // address terms.
70         if ($this->iId !== null
71             && ($oSearch->getNamePhrase() >= 0 || !$oSearch->hasName())
72             && !$oSearch->hasAddress()
73         ) {
74             $oNewSearch = $oSearch->clone($iSearchCost);
75             $oNewSearch->setHousenumberAsName($this->iId);
76
77             $aNewSearches[] = $oNewSearch;
78         }
79
80         return $aNewSearches;
81     }
82
83
84     public function debugInfo()
85     {
86         return array(
87                 'ID' => $this->iId,
88                 'Type' => 'house number',
89                 'Info' => array('nr' => $this->sToken)
90                );
91     }
92
93     public function debugCode()
94     {
95         return 'H';
96     }
97 }