]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/SearchPosition.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / lib-php / SearchPosition.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  * Description of the position of a token within a query.
15  */
16 class SearchPosition
17 {
18     private $sPhraseType;
19
20     private $iPhrase;
21     private $iNumPhrases;
22
23     private $iToken;
24     private $iNumTokens;
25
26
27     public function __construct($sPhraseType, $iPhrase, $iNumPhrases)
28     {
29         $this->sPhraseType = $sPhraseType;
30         $this->iPhrase = $iPhrase;
31         $this->iNumPhrases = $iNumPhrases;
32     }
33
34     public function setTokenPosition($iToken, $iNumTokens)
35     {
36         $this->iToken = $iToken;
37         $this->iNumTokens = $iNumTokens;
38     }
39
40     /**
41      * Check if the phrase can be of the given type.
42      *
43      * @param string  $sType  Type of phrse requested.
44      *
45      * @return True if the phrase is untyped or of the given type.
46      */
47     public function maybePhrase($sType)
48     {
49         return $this->sPhraseType == '' || $this->sPhraseType == $sType;
50     }
51
52     /**
53      * Check if the phrase is exactly of the given type.
54      *
55      * @param string  $sType  Type of phrse requested.
56      *
57      * @return True if the phrase of the given type.
58      */
59     public function isPhrase($sType)
60     {
61         return $this->sPhraseType == $sType;
62     }
63
64     /**
65      * Return true if the token is the very first in the query.
66      */
67     public function isFirstToken()
68     {
69         return $this->iPhrase == 0 && $this->iToken == 0;
70     }
71
72     /**
73      * Check if the token is the final one in the query.
74      */
75     public function isLastToken()
76     {
77         return $this->iToken + 1 == $this->iNumTokens && $this->iPhrase + 1 == $this->iNumPhrases;
78     }
79
80     /**
81      * Check if the current token is part of the first phrase in the query.
82      */
83     public function isFirstPhrase()
84     {
85         return $this->iPhrase == 0;
86     }
87
88     /**
89      * Get the phrase position in the query.
90      */
91     public function getPhrase()
92     {
93         return $this->iPhrase;
94     }
95 }