]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/Result.php
add PHP part for new ICU-base tokenizer
[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     public static function sqlHouseNumberTable($aResults, $iTable)
59     {
60         $sHousenumbers = '';
61         $sSep = '';
62         foreach ($aResults as $oResult) {
63             if ($oResult->iTable == $iTable) {
64                 $sHousenumbers .= $sSep.'('.$oResult->iId.',';
65                 $sHousenumbers .= $oResult->iHouseNumber.')';
66                 $sSep = ',';
67             }
68         }
69
70         return $sHousenumbers;
71     }
72
73     /**
74      * Split a result array into highest ranked result and the rest
75      *
76      * @param object[] $aResults List of results to split.
77      *
78      * @return array[]
79      */
80     public static function splitResults($aResults)
81     {
82         $aHead = array();
83         $aTail = array();
84         $iMinRank = 10000;
85
86         foreach ($aResults as $oRes) {
87             if ($oRes->iResultRank < $iMinRank) {
88                 $aTail += $aHead;
89                 $aHead = array($oRes->iId => $oRes);
90                 $iMinRank = $oRes->iResultRank;
91             } elseif ($oRes->iResultRank == $iMinRank) {
92                 $aHead[$oRes->iId] = $oRes;
93             } else {
94                 $aTail[$oRes->iId] = $oRes;
95             }
96         }
97
98         return array('head' => $aHead, 'tail' => $aTail);
99     }
100 }