<?php

namespace App\Services\GoogleMaps\Unit;

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

	public $id;

	public $description;

	public $mainText;

	public $secondaryText;

	public $googlePlaceId;

	/**
	 * 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->description = array_get($addressData, 'description');
		$this->mainText = array_get($addressData, 'structured_formatting.main_text');
		$this->secondaryText = array_get($addressData, 'structured_formatting.secondary_text');
		$this->googlePlaceId = array_get($addressData, 'place_id');
	}

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