1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
staticmapservice.php
См. документацию.
1<?php
2
3namespace Bitrix\Location\Service;
4
5use Bitrix\Location\Common\BaseService;
6use Bitrix\Location\Entity\Source;
7use Bitrix\Location\Geometry\Type\Point;
8use Bitrix\Location\Infrastructure\Service\Config\Container;
9use Bitrix\Location\Model\StaticMapFileTable;
10use Bitrix\Location\StaticMap\StaticMapResult;
11use Bitrix\Main\Error;
12use Bitrix\Main\Web\MimeType;
13use InvalidArgumentException;
14
15final class StaticMapService extends BaseService
16{
17 protected static $instance;
18 private ?Source $source;
19
20 protected function __construct(Container $config)
21 {
22 parent::__construct($config);
23
24 $this->source = $config->get('source');
25 }
26
27 public function getStaticMap(
28 float $latitude,
29 float $longitude,
30 int $zoom,
31 int $width,
32 int $height
34 {
35 if (!$this->source)
36 {
37 return (new StaticMapResult())->addError(new Error('Source is not specified'));
38 }
39
40 $validateErrors = $this->validate($latitude, $longitude, $zoom, $width, $height);
41 if (!empty($validateErrors))
42 {
43 return (new StaticMapResult())->addErrors($validateErrors);
44 }
45
46 $point = $this->makePoint($latitude, $longitude);
47 $hash = $this->getHash($point, $zoom, $width, $height);
48
49 $existingFileRow = StaticMapFileTable::getRowById($hash);
50 if ($existingFileRow)
51 {
52 $resultFromCache = $this->getResultFromCache((int)$existingFileRow['FILE_ID']);
53 if ($resultFromCache)
54 {
55 return $resultFromCache;
56 }
57 }
58
59 $staticMapService = $this->source->makeStaticMapService();
60 if (!$staticMapService)
61 {
62 return (new StaticMapResult())->addError(
63 new Error('Static map service is not supported by the source')
64 );
65 }
66
67 $serviceResult = $staticMapService->getStaticMap($point, $zoom, $width, $height);
68 if (!$serviceResult->isSuccess())
69 {
70 return $serviceResult;
71 }
72
73 $this->saveResultToCache($hash, $serviceResult);
74
75 return $serviceResult;
76 }
77
78 private function validate(
79 float $latitude,
80 float $longitude,
81 int $zoom,
82 int $width,
83 int $height
84 ): array
85 {
86 $result = [];
87
88 try
89 {
90 $this->makePoint($latitude, $longitude);
91 }
92 catch (InvalidArgumentException $exception)
93 {
94 $result[] = new Error($exception->getMessage());
95 }
96
97 if ($zoom < 0 || $zoom > 18)
98 {
99 $result[] = new Error('zoom must be a positive number or zero between 0 and 18');
100 }
101
103 {
104 $result[] = new Error('width must be a positive number between 0 and 640');
105 }
106
107 if ($height <= 0 || $height > 640)
108 {
109 $result[] = new Error('height must be a positive number 0 and 640');
110 }
111
112 return $result;
113 }
114
115 private function makePoint(
116 float $latitude,
117 float $longitude,
118 ): Point
119 {
120 return new Point($latitude, $longitude);
121 }
122
123 private function getHash(
124 Point $point,
125 int $zoom,
126 int $width,
127 int $height
128 )
129 {
130 return sha1(
131 implode(';', [
132 $this->source->getCode(),
133 $point->getLng(),
134 $point->getLat(),
135 $zoom,
136 $width,
137 $height
138 ])
139 );
140 }
141
142 private function getResultFromCache(int $fileId): ?StaticMapResult
143 {
144 $file = \CFile::GetByID($fileId)->fetch();
145
146 if ($file && isset($file['SRC']))
147 {
148 return (new StaticMapResult())
149 ->setMimeType($file['CONTENT_TYPE'])
150 ->setPath($file['SRC'])
151 ;
152 }
153
154 return null;
155 }
156
157 private function saveResultToCache(string $hash, StaticMapResult $result): void
158 {
159 $resultMimeType = $result->getMimeType();
160 $resultContent = $result->getContent();
161
162 $resultFileExtension = '';
163 foreach (MimeType::getMimeTypeList() as $extension => $mimeType)
164 {
165 if ($resultMimeType === $mimeType)
166 {
167 $resultFileExtension = $extension;
168 break;
169 }
170 }
171
172 $fileId = (int)\CFile::SaveFile(
173 [
174 'name' => $hash . ($resultFileExtension ? ('.' . $resultFileExtension) : ''),
175 'type' => $resultMimeType,
176 'size' => mb_strlen($resultContent),
177 'content' => $resultContent,
178 'MODULE_ID' => 'location',
179 ],
180 'location/static_map'
181 );
182
183 if ($fileId)
184 {
185 $filePath = \CFile::GetPath($fileId);
186 if ($filePath)
187 {
188 $result->setPath($filePath);
189 }
190
191 StaticMapFileTable::merge(
192 [
193 'HASH' => $hash,
194 'FILE_ID' => $fileId,
195 ],
196 [
197 'FILE_ID' => $fileId,
198 ],
199 [
200 'HASH',
201 ]
202 );
203 }
204 }
205}
$hash
Определения ajax_redirector.php:8
__construct(Container $config)
Определения staticmapservice.php:20
getStaticMap(float $latitude, float $longitude, int $zoom, int $width, int $height)
Определения staticmapservice.php:27
Определения error.php:15
static getRowById($id, array $parameters=[])
Определения datamanager.php:380
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
trait Error
Определения error.php:11
$config
Определения quickway.php:69
$width
Определения html.php:68