]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenPostcode.php
work around strange query planning behaviour
[nominatim.git] / lib-php / TokenPostcode.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 /**
14  * A postcode token.
15  */
16 class Postcode
17 {
18     /// Database word id, if available.
19     private $iId;
20     /// Full normalized postcode (upper cased).
21     private $sPostcode;
22     // Optional country code the postcode belongs to (currently unused).
23     private $sCountryCode;
24
25     public function __construct($iId, $sPostcode, $sCountryCode = '')
26     {
27         $this->iId = $iId;
28         $iSplitPos = strpos($sPostcode, '@');
29         if ($iSplitPos === false) {
30             $this->sPostcode = $sPostcode;
31         } else {
32             $this->sPostcode = substr($sPostcode, 0, $iSplitPos);
33         }
34         $this->sCountryCode = empty($sCountryCode) ? '' : $sCountryCode;
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->hasPostcode() && $oPosition->maybePhrase('postalcode');
56     }
57
58     /**
59      * Derive new searches by adding this token to an existing search.
60      *
61      * @param object  $oSearch      Partial search description derived so far.
62      * @param object  $oPosition    Description of the token position within
63                                     the query.
64      *
65      * @return SearchDescription[] List of derived search descriptions.
66      */
67     public function extendSearch($oSearch, $oPosition)
68     {
69         $aNewSearches = array();
70
71         // If we have structured search or this is the first term,
72         // make the postcode the primary search element.
73         if ($oSearch->hasOperator(\Nominatim\Operator::NONE) && $oPosition->isFirstToken()) {
74             $oNewSearch = $oSearch->clone(1);
75             $oNewSearch->setPostcodeAsName($this->iId, $this->sPostcode);
76
77             $aNewSearches[] = $oNewSearch;
78         }
79
80         // If we have a structured search or this is not the first term,
81         // add the postcode as an addendum.
82         if (!$oSearch->hasOperator(\Nominatim\Operator::POSTCODE)
83             && ($oPosition->isPhrase('postalcode') || $oSearch->hasName())
84         ) {
85             $iPenalty = 1;
86             if (strlen($this->sPostcode) < 4) {
87                 $iPenalty += 4 - strlen($this->sPostcode);
88             }
89             $oNewSearch = $oSearch->clone($iPenalty);
90             $oNewSearch->setPostcode($this->sPostcode);
91
92             $aNewSearches[] = $oNewSearch;
93         }
94
95         return $aNewSearches;
96     }
97
98     public function debugInfo()
99     {
100         return array(
101                 'ID' => $this->iId,
102                 'Type' => 'postcode',
103                 'Info' => $this->sPostcode.'('.$this->sCountryCode.')'
104                );
105     }
106
107     public function debugCode()
108     {
109         return 'P';
110     }
111 }