]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenPostcode.php
fix parameters for TokenWord creation
[nominatim.git] / lib-php / TokenPostcode.php
1 <?php
2
3 namespace Nominatim\Token;
4
5 /**
6  * A postcode token.
7  */
8 class Postcode
9 {
10     /// Database word id, if available.
11     private $iId;
12     /// Full nomralized postcode (upper cased).
13     private $sPostcode;
14     // Optional country code the postcode belongs to (currently unused).
15     private $sCountryCode;
16
17     public function __construct($iId, $sPostcode, $sCountryCode = '')
18     {
19         $this->iId = $iId;
20         $this->sPostcode = $sPostcode;
21         $this->sCountryCode = empty($sCountryCode) ? '' : $sCountryCode;
22     }
23
24     public function getId()
25     {
26         return $this->iId;
27     }
28
29     /**
30      * Check if the token can be added to the given search.
31      * Derive new searches by adding this token to an existing search.
32      *
33      * @param object  $oSearch      Partial search description derived so far.
34      * @param object  $oPosition    Description of the token position within
35                                     the query.
36      *
37      * @return True if the token is compatible with the search configuration
38      *         given the position.
39      */
40     public function isExtendable($oSearch, $oPosition)
41     {
42         return !$oSearch->hasPostcode() && $oPosition->maybePhrase('postalcode');
43     }
44
45     /**
46      * Derive new searches by adding this token to an existing search.
47      *
48      * @param object  $oSearch      Partial search description derived so far.
49      * @param object  $oPosition    Description of the token position within
50                                     the query.
51      *
52      * @return SearchDescription[] List of derived search descriptions.
53      */
54     public function extendSearch($oSearch, $oPosition)
55     {
56         $aNewSearches = array();
57
58         // If we have structured search or this is the first term,
59         // make the postcode the primary search element.
60         if ($oSearch->hasOperator(\Nominatim\Operator::NONE) && $oPosition->isFirstToken()) {
61             $oNewSearch = $oSearch->clone(1);
62             $oNewSearch->setPostcodeAsName($this->iId, $this->sPostcode);
63
64             $aNewSearches[] = $oNewSearch;
65         }
66
67         // If we have a structured search or this is not the first term,
68         // add the postcode as an addendum.
69         if (!$oSearch->hasOperator(\Nominatim\Operator::POSTCODE)
70             && ($oPosition->isPhrase('postalcode') || $oSearch->hasName())
71         ) {
72             $iPenalty = 1;
73             if (strlen($this->sPostcode) < 4) {
74                 $iPenalty += 4 - strlen($this->sPostcode);
75             }
76             $oNewSearch = $oSearch->clone($iPenalty);
77             $oNewSearch->setPostcode($this->sPostcode);
78
79             $aNewSearches[] = $oNewSearch;
80         }
81
82         return $aNewSearches;
83     }
84
85     public function debugInfo()
86     {
87         return array(
88                 'ID' => $this->iId,
89                 'Type' => 'postcode',
90                 'Info' => $this->sPostcode.'('.$this->sCountryCode.')'
91                );
92     }
93
94     public function debugCode()
95     {
96         return 'P';
97     }
98 }