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