]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/Result.php
Merge pull request #2219 from lonvia/bdd-test-remove-php
[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_AUX = 3;
17     const TABLE_TIGER = 4;
18
19     /// Database table that contains the result.
20     public $iTable;
21     /// Id of the result.
22     public $iId;
23     /// House number (only for interpolation results).
24     public $iHouseNumber = -1;
25     /// Number of exact matches in address (address searches only).
26     public $iExactMatches = 0;
27     /// Subranking within the results (the higher the worse).
28     public $iResultRank = 0;
29     /// Address rank of the result.
30     public $iAddressRank;
31
32     public function debugInfo()
33     {
34         return array(
35                 'Table' => $this->iTable,
36                 'ID' => $this->iId,
37                 'House number' => $this->iHouseNumber,
38                 'Exact Matches' => $this->iExactMatches,
39                 'Result rank' => $this->iResultRank
40                );
41     }
42
43
44     public function __construct($sId, $iTable = Result::TABLE_PLACEX)
45     {
46         $this->iTable = $iTable;
47         $this->iId = (int) $sId;
48     }
49
50     public static function joinIdsByTable($aResults, $iTable)
51     {
52         return join(',', array_keys(array_filter(
53             $aResults,
54             function ($aValue) use ($iTable) {
55                 return $aValue->iTable == $iTable;
56             }
57         )));
58     }
59     public static function sqlHouseNumberTable($aResults, $iTable)
60     {
61         $sHousenumbers = '';
62         $sSep = '';
63         foreach ($aResults as $oResult) {
64             if ($oResult->iTable == $iTable) {
65                 $sHousenumbers .= $sSep.'('.$oResult->iId.',';
66                 $sHousenumbers .= $oResult->iHouseNumber.')';
67                 $sSep = ',';
68             }
69         }
70
71         return $sHousenumbers;
72     }
73
74     /**
75      * Split a result array into highest ranked result and the rest
76      *
77      * @param object[] $aResults List of results to split.
78      *
79      * @return array[]
80      */
81     public static function splitResults($aResults)
82     {
83         $aHead = array();
84         $aTail = array();
85         $iMinRank = 10000;
86
87         foreach ($aResults as $oRes) {
88             if ($oRes->iResultRank < $iMinRank) {
89                 $aTail += $aHead;
90                 $aHead = array($oRes->iId => $oRes);
91                 $iMinRank = $oRes->iResultRank;
92             } elseif ($oRes->iResultRank == $iMinRank) {
93                 $aHead[$oRes->iId] = $oRes;
94             } else {
95                 $aTail[$oRes->iId] = $oRes;
96             }
97         }
98
99         return array('head' => $aHead, 'tail' => $aTail);
100     }
101 }