<?php

namespace App\Services\GoogleMaps\Unit;

/**
 * Class GoogleAddressUnit
 *
 * @package App\Packages\GoogleMaps
 */
class GoogleLatLngPlaceUnit
{
	public $isValid = true;

	public $id;
	public $googlePlaceId;

	public $lat;
	public $lng;

	public $name;
	public $vicinity;

	/**
	 * GoogleAddressUnit constructor.
	 *
	 * @param array $googlePredictionResult
	 */
	public function __construct($googlePredictionResult)
	{
		if ($googlePredictionResult !== null) {
			$this->decode($googlePredictionResult);
		} else {
			$this->isValid = false;
		}
	}

	/**
	 * @param $addressData
	 */
	public function decode($addressData)
	{
		$this->id = array_get($addressData, 'id');
		$this->googlePlaceId = array_get($addressData, 'place_id');
		$this->name = array_get($addressData, 'name');
		$this->vicinity = array_get($addressData, 'vicinity');

		$this->lat = array_get($addressData, 'geometry.location.lat');
		$this->lng = array_get($addressData, 'geometry.location.lng');

	}

	/**
	 * @return array
	 */
	public function toSimpleArray()
	{
		return [
			'id' => $this->googlePlaceId,
			'name' => $this->name,
			'vicinity' => $this->vicinity,
			'lat' => $this->lat,
			'lng' => $this->lng,
		];
	}
}
