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