]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/Result.php
move SearchDescription building into tokens
[nominatim.git] / lib-php / Result.php
1 <?php
2
3 namespace Nominatim;
4
5 /**
6  * A single result of a search operation or a reverse lookup.
7  *
8  * This object only contains the id of the result. It does not yet
9  * have any details needed to format the output document.
10  */
11 class Result
12 {
13     const TABLE_PLACEX = 0;
14     const TABLE_POSTCODE = 1;
15     const TABLE_OSMLINE = 2;
16     const TABLE_TIGER = 3;
17
18     /// Database table that contains the result.
19     public $iTable;
20     /// Id of the result.
21     public $iId;
22     /// House number (only for interpolation results).
23     public $iHouseNumber = -1;
24     /// Number of exact matches in address (address searches only).
25     public $iExactMatches = 0;
26     /// Subranking within the results (the higher the worse).
27     public $iResultRank = 0;
28     /// Address rank of the result.
29     public $iAddressRank;
30
31     public function debugInfo()
32     {
33         return array(
34                 'Table' => $this->iTable,
35                 'ID' => $this->iId,
36                 'House number' => $this->iHouseNumber,
37                 'Exact Matches' => $this->iExactMatches,
38                 'Result rank' => $this->iResultRank
39                );
40     }
41
42
43     public function __construct($sId, $iTable = Result::TABLE_PLACEX)
44     {
45         $this->iTable = $iTable;
46         $this->iId = (int) $sId;
47     }
48
49     public static function joinIdsByTable($aResults, $iTable)
50     {
51         return join(',', array_keys(array_filter(
52             $aResults,
53             function ($aValue) use ($iTable) {
54                 return $aValue->iTable == $iTable;
55             }
56         )));
57     }
58
59     public static function joinIdsByTableMinRank($aResults, $iTable, $iMinAddressRank)
60     {
61         return join(',', array_keys(array_filter(
62             $aResults,
63             function ($aValue) use ($iTable, $iMinAddressRank) {
64                 return $aValue->iTable == $iTable && $aValue->iAddressRank >= $iMinAddressRank;
65             }
66         )));
67     }
68
69     public static function joinIdsByTableMaxRank($aResults, $iTable, $iMaxAddressRank)
70     {
71         return join(',', array_keys(array_filter(
72             $aResults,
73             function ($aValue) use ($iTable, $iMaxAddressRank) {
74                 return $aValue->iTable == $iTable && $aValue->iAddressRank <= $iMaxAddressRank;
75             }
76         )));
77     }
78
79     public static function sqlHouseNumberTable($aResults, $iTable)
80     {
81         $sHousenumbers = '';
82         $sSep = '';
83         foreach ($aResults as $oResult) {
84             if ($oResult->iTable == $iTable) {
85                 $sHousenumbers .= $sSep.'('.$oResult->iId.',';
86                 $sHousenumbers .= $oResult->iHouseNumber.')';
87                 $sSep = ',';
88             }
89         }
90
91         return $sHousenumbers;
92     }
93
94     /**
95      * Split a result array into highest ranked result and the rest
96      *
97      * @param object[] $aResults List of results to split.
98      *
99      * @return array[]
100      */
101     public static function splitResults($aResults)
102     {
103         $aHead = array();
104         $aTail = array();
105         $iMinRank = 10000;
106
107         foreach ($aResults as $oRes) {
108             if ($oRes->iResultRank < $iMinRank) {
109                 $aTail += $aHead;
110                 $aHead = array($oRes->iId => $oRes);
111                 $iMinRank = $oRes->iResultRank;
112             } elseif ($oRes->iResultRank == $iMinRank) {
113                 $aHead[$oRes->iId] = $oRes;
114             } else {
115                 $aTail[$oRes->iId] = $oRes;
116             }
117         }
118
119         return array('head' => $aHead, 'tail' => $aTail);
120     }
121 }