GeoFixer::findFiasSettlements()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 15
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
crap 2
1
<?php
2
3
namespace GeoFixer\models;
4
5
use GeoFixer\models\queries\AbstractDatabaseQuery;
6
use GeoFixer\models\queries\RegionsDatabaseQuery;
7
use GeoFixer\models\queries\SettlementsDatabaseQuery;
8
use GeoFixer\models\queries\StreetsDatabaseQuery;
9
use GeoFixer\models\queries\HousesDatabaseQuery;
10
use GeoFixer\helpers\StringHelper;
11
use GeoFixer\helpers\FuzzySearchHelper;
12
13
/**
14
 * Class GeoFixer
15
 *
16
 * @package GeoFixer\models
17
 */
18
class GeoFixer
19
{
20
    protected $strict = false;
21
    protected $first_letters = false;
22
    protected $full_settlements = false;
23
24
    protected $geo_with_ids = [];
25
    protected $geo_titles = [];
26
    protected $title_name;
27
    protected $code_name;
28
29
    protected $string_helper;
30
    protected $fuzzy_helper;
31
32
    /**
33
     * GeoFixer construct
34
     */
35 29
    public function __construct()
36
    {
37 29
        $this->string_helper = new StringHelper();
38 29
        $this->fuzzy_helper = new FuzzySearchHelper();
39
40 29
        $this->title_name = AbstractDatabaseQuery::TITLE;
41 29
        $this->code_name = AbstractDatabaseQuery::FIAS_CODE;
42 29
    }
43
44
    /**
45
     * @param $region
46
     *
47
     * @return bool|mixed
48
     */
49 8 View Code Duplication
    public function findFiasRegion($region)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51 8
        $this->code_name = AbstractDatabaseQuery::KLADR_CODE;
52
53 8
        $regions = new RegionsDatabaseQuery();
54 8
        $regions = $regions->getRegions();
55
56 8
        if (is_integer($this->first_letters)) {
57 2
            $regions = $regions->firstLetters(substr($region, 0, $this->first_letters));
58 2
        }
59
60 8
        $regions = $regions->findAll();
61
62 8
        return $this->getResult($regions, $region);
63
    }
64
65
    /**
66
     * @param $word
67
     * @param $search_array
68
     *
69
     * @return string|false|null
70
     */
71 23
    public function findSimilarWord($word, $search_array)
72
    {
73 23
        if (in_array($word, $search_array)) {
74 7
            return $word;
75
        }
76 19
        $this->fuzzy_helper = new FuzzySearchHelper();
77
78 19
        $word = $this->string_helper->wordTranslit($word);
79
80 19
        $translited_words = $this->string_helper->arrayTranslit($search_array);
81
82 19
        $result = $this->strict === true ?
83 19
            $this->fuzzy_helper->findBestMatch($word, $translited_words) :
84 19
            key($this->fuzzy_helper->findMostSimilarWords($word, $translited_words));
85
86 19
        return $result;
87
    }
88
89
    /**
90
     * @param $city
91
     * @param $region_code
92
     *
93
     * @return bool|mixed
94
     */
95 4 View Code Duplication
    public function findFiasSettlements($city, $region_code)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97 4
        $this->code_name = AbstractDatabaseQuery::FIAS_CODE;
98
99 4
        $settlements = new SettlementsDatabaseQuery();
100 4
        $settlements = $settlements->getSettlements()->regionCode($region_code)->addressLevel($this->full_settlements);
101
102 4
        if (is_integer($this->first_letters)) {
103 1
            $settlements = $settlements->firstLetters(substr($city, 0, $this->first_letters));
104 1
        }
105
106 4
        $settlements = $settlements->findAll();
107
108 4
        return $this->getResult($settlements, $city);
109
    }
110
111
    /**
112
     * @param $city
113
     * @param $region_code
114
     *
115
     * @return bool|mixed
116
     */
117 4 View Code Duplication
    public function findKladrSettlements($city, $region_code)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119 4
        $this->code_name = AbstractDatabaseQuery::KLADR_CODE;
120
121 4
        $settlements = new SettlementsDatabaseQuery();
122 4
        $settlements = $settlements->getSettlements()->regionCode($region_code)->addressLevel($this->full_settlements);
123
124 4
        if (is_integer($this->first_letters)) {
125 1
            $settlements = $settlements->firstLetters(substr($city, 0, $this->first_letters));
126 1
        }
127
128 4
        $settlements = $settlements->findAll();
129
130 4
        return $this->getResult($settlements, $city);
131
    }
132
133
    /**
134
     * @param $street
135
     * @param $city_id
136
     *
137
     * @return bool|mixed
138
     */
139 3 View Code Duplication
    public function findFiasStreets($street, $city_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141 3
        $this->code_name = AbstractDatabaseQuery::FIAS_CODE;
142
143 3
        $streets = new StreetsDatabaseQuery();
144 3
        $streets = $streets->getStreets()->parentId($city_id)->addressLevel();
145
146 3
        if (is_integer($this->first_letters)) {
147 1
            $streets = $streets->firstLetters(substr($street, 0, $this->first_letters));
148 1
        }
149
150 3
        $streets = $streets->findAll();
151
152 3
        return $this->getResult($streets, $street);
153
    }
154
155
    /**
156
     * @param $street
157
     * @param $city_code
158
     *
159
     * @return bool|mixed
160
     */
161 4
    public function findKladrStreets($street, $city_code)
162
    {
163 4
        $this->code_name = AbstractDatabaseQuery::KLADR_CODE;
164
165 4
        $streets = new StreetsDatabaseQuery();
166 4
        $city = new SettlementsDatabaseQuery();
167 4
        $city_id = $city->getSettlements()->addressLevel(true)->kladrCode($city_code)->findOne();
168
169 4
        if ($city_id === false) {
170 1
            return false;
171
        }
172
173 3
        $city_id = $city_id['address_id'];
174 3
        $streets = $streets->getStreets()->parentId($city_id)->addressLevel();
175
176 3
        if (is_integer($this->first_letters)) {
177 1
            $streets = $streets->firstLetters(substr($street, 0, $this->first_letters));
178 1
        }
179
180 3
        $streets = $streets->findAll();
181
182 3
        return $this->getResult($streets, $street);
183
    }
184
185
    /**
186
     * @param $house
187
     * @param $street_id
188
     *
189
     * @return bool
190
     */
191 4
    public function findFiasHouses($house, $street_id, $building = false)
192
    {
193 4
        $house_id = new HousesDatabaseQuery();
194 4
        $house_id = $house_id->getHouses()->addressId($street_id)->houseNumber($house);
195
196 4
        if ($building !== false) {
197 2
            $house_id = $house_id->building($building);
198 2
        }
199
200 4
        $house_id = $house_id->findOne();
201
202 4
        return $house_id ? $house_id['house_id'] : false;
203
    }
204
205
    /**
206
     * Включаем строгий режим поиска
207
     *
208
     * @param bool $strict
209
     */
210 24
    public function isStrict($strict = false)
211
    {
212 24
        $this->strict = $strict;
213 24
    }
214
215
216
    /**
217
     * Сколько первых букв должны совпадать при поиске по базам ФИАС
218
     *
219
     * (теоретически, снизит кол-во слов, которые придется обрабатывать алгоритмом и тем самым увеличит скорость работы, но может не найти слово, если первые буквы не совпадают
220
     * из-за опечатки или префиксов)
221
     *
222
     * @param int|bool $count
223
     */
224 21
    public function isFirstLetters($count = false)
225
    {
226 21
        if (is_int($count)) {
227 6
            $this->first_letters = $count;
0 ignored issues
show
Documentation Bug introduced by
The property $first_letters was declared of type boolean, but $count is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
228 6
        }
229 21
    }
230
231
    /**
232
     * Только города, или города и поселения
233
     *
234
     * @param bool $is_full
235
     */
236 8
    public function isFullSettlements($is_full = false)
237
    {
238 8
        $this->full_settlements = $is_full;
239 8
    }
240
241
    /**
242
     * @param $geo_array
243
     */
244 20
    protected function geoDataHandler($geo_array)
245
    {
246 20
        $this->geo_with_ids[$geo_array[$this->title_name]] = $geo_array[$this->code_name];
247 20
        $this->geo_titles[] = $geo_array[$this->title_name];
248 20
    }
249
250
    /**
251
     * @param $geo_array
252
     * @param $word
253
     * @return bool|mixed
254
     */
255 20
    protected function getResult($geo_array, $word)
256
    {
257 20
        array_map(array($this, 'geoDataHandler'), $geo_array);
258
259 20
        $result = $this->findSimilarWord($word, $this->geo_titles);
260
261 20
        return $result ? $this->geo_with_ids[$result] : false;
262
    }
263
}
264
265