x = $x; $this->y = $y; $this->width = (int)$width; $this->height = (int)$height; $this->zoom = (int)$zoom; } public function addPoint($x, $y) { $this->points[] = [$x, $y]; return $this; } private function getPointsString() { $points = empty($this->points) ? [[$this->x, $this->y]] : $this->points; $p = []; foreach ($points as $point) { $p[] = implode(',', $point); } return implode('~', $p); } private function buildUrl() { $params = [ 'll' => $this->x . ',' . $this->y, 'size' => $this->width . ',' . $this->height, 'z' => (int)$this->zoom, 'l' => 'map', 'pt' => $this->getPointsString(), ]; return self::DOMAIN . '?' . http_build_query($params); } private function loadData($url) { $data = false; $attempt = self::ATTEMPTS_COUNT; do { if ($attempt != self::ATTEMPTS_COUNT) { sleep(self::ATTEMPTS_TIMEOUT); } try { $data = file_get_contents($url); } catch (\Exception $e) { $data = false; } } while (--$attempt > 0 && $data === false); return $data; } private function getTmpFile() { if (!empty($this->imageFile) && is_file($this->imageFile)) { return $this->imageFile; } $tmpDir = sys_get_temp_dir(); do { $filename = $tmpDir.'/'.uniqid(); } while (file_exists($filename)); $data = $this->loadData($this->buildUrl()); file_put_contents($filename, $data); return $this->imageFile = $filename; } public function __toString() { return $this->getTmpFile(); } }