]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenPostcode.php
move SearchDescription building into tokens
[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      * Derive new searches by adding this token to an existing search.
31      *
32      * @param object  $oSearch      Partial search description derived so far.
33      * @param object  $oPosition    Description of the token position within
34                                     the query.
35      *
36      * @return SearchDescription[] List of derived search descriptions.
37      */
38     public function extendSearch($oSearch, $oPosition)
39     {
40         $aNewSearches = array();
41
42         if ($oSearch->hasPostcode() || !$oPosition->maybePhrase('postalcode')) {
43             return $aNewSearches;
44         }
45
46         // If we have structured search or this is the first term,
47         // make the postcode the primary search element.
48         if ($oSearch->hasOperator(\Nominatim\Operator::NONE) && $oPosition->isFirstToken()) {
49             $oNewSearch = $oSearch->clone(1);
50             $oNewSearch->setPostcodeAsName($this->iId, $this->sPostcode);
51
52             $aNewSearches[] = $oNewSearch;
53         }
54
55         // If we have a structured search or this is not the first term,
56         // add the postcode as an addendum.
57         if (!$oSearch->hasOperator(\Nominatim\Operator::POSTCODE)
58             && ($oPosition->isPhrase('postalcode') || $oSearch->hasName())
59         ) {
60             $iPenalty = 1;
61             if (strlen($this->sPostcode) < 4) {
62                 $iPenalty += 4 - strlen($this->sPostcode);
63             }
64             $oNewSearch = $oSearch->clone($iPenalty);
65             $oNewSearch->setPostcode($this->sPostcode);
66
67             $aNewSearches[] = $oNewSearch;
68         }
69
70         return $aNewSearches;
71     }
72
73     public function debugInfo()
74     {
75         return array(
76                 'ID' => $this->iId,
77                 'Type' => 'postcode',
78                 'Info' => $this->sPostcode.'('.$this->sCountryCode.')'
79                );
80     }
81
82     public function debugCode()
83     {
84         return 'P';
85     }
86 }