Skip to content
Snippets Groups Projects
Location.php 3.18 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * @copyright  2021 Podlibre
     * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
     * @link       https://castopod.org/
     */
    
    namespace App\Entities;
    
    use CodeIgniter\Entity\Entity;
    
    
    /**
     * @property string $url
     * @property string $name
     * @property string|null $geo
    
     * @property double|null $latitude
     * @property double|null $longitude
    
     */
    class Location extends Entity
    {
        /**
         * @var string
         */
    
        private const OSM_URL = 'https://www.openstreetmap.org/';
    
        private const NOMINATIM_URL = 'https://nominatim.openstreetmap.org/';
    
    
        public function __construct(
            protected string $name,
            protected ?string $geo = null,
    
            protected ?string $osm = null,
    
            $latitude = null;
            $longitude = null;
            if ($geo !== null) {
                $geoArray = explode(',', substr($geo, 4));
                $latitude = floatval($geoArray[0]);
                $longitude = floatval($geoArray[1]);
            }
    
                'latitude' => $latitude,
                'longitude' => $longitude,
    
        public function getUrl(): string
        {
    
                    [
                        'N' => 'node',
                        'W' => 'way',
                        'R' => 'relation',
                    ][substr($this->osm, 0, 1)] .
    
            }
    
            if ($this->geo !== null) {
                return self::OSM_URL .
                    '#map=17/' .
                    str_replace(',', '/', substr($this->geo, 4));
            }
    
            return self::OSM_URL . 'search?query=' . urlencode($this->name);
        }
    
        public function fetchOsmLocation(): static
    
        {
            $client = Services::curlrequest();
    
            $response = $client->request(
                'GET',
                self::NOMINATIM_URL .
                    'search.php?q=' .
                    urlencode($this->name) .
                    '&polygon_geojson=1&format=jsonv2',
                [
                    'headers' => [
                        'User-Agent' => 'Castopod/' . CP_VERSION,
                        'Accept' => 'application/json',
                    ],
                ],
            );
    
    
            $places = json_decode($response->getBody(), false, 512, JSON_THROW_ON_ERROR);
    
            if (property_exists($places[0], 'lat') && $places[0]->lat !== null && (property_exists(
                $places[0],
                'lon'
            ) && $places[0]->lon !== null)) {
    
                $this->attributes['geo'] = "geo:{$places[0]->lat},{$places[0]->lon}";
            }
    
    
            if (property_exists($places[0], 'osm_type') && $places[0]->osm_type !== null && (property_exists(
                $places[0],
                'osm_id'
            ) && $places[0]->osm_id !== null)) {
    
                $this->attributes['osm'] = strtoupper(substr($places[0]->osm_type, 0, 1)) . $places[0]->osm_id;
            }
    
            return $this;
        }