Skip to content

Commit

Permalink
Exceptions, response mapping objects
Browse files Browse the repository at this point in the history
  • Loading branch information
cirovargas committed Jul 10, 2017
1 parent 7b10146 commit 25453b3
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 0 deletions.
Empty file modified src/Response/Address.php
100644 → 100755
Empty file.
Empty file modified src/Response/Distance.php
100644 → 100755
Empty file.
Empty file modified src/Response/Duration.php
100644 → 100755
Empty file.
Empty file modified src/Response/Element.php
100644 → 100755
Empty file.
123 changes: 123 additions & 0 deletions src/Response/GoogleDistanceMatrixResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace Valerian\GoogleDistanceMatrix\Response;

class GoogleDistanceMatrixResponse
{

const RESPONSE_STATUS_OK = 'OK';
const RESPONSE_STATUS_INVALID_REQUEST = 'INVALID_REQUEST';
const RESPONSE_STATUS_MAX_ELEMENTS_EXCEEDED = 'MAX_ELEMENTS_EXCEEDED';
const RESPONSE_STATUS_OVER_QUERY_LIMIT = 'OVER_QUERY_LIMIT';
const RESPONSE_STATUS_REQUEST_DENIED = 'REQUEST_DENIED';
const RESPONSE_STATUS_UNKNOWN_ERROR = 'UNKNOWN_ERROR';

const RESPONSE_STATUS = [
self::RESPONSE_STATUS_OK,
self::RESPONSE_STATUS_INVALID_REQUEST,
self::RESPONSE_STATUS_MAX_ELEMENTS_EXCEEDED,
self::RESPONSE_STATUS_OVER_QUERY_LIMIT,
self::RESPONSE_STATUS_REQUEST_DENIED,
self::RESPONSE_STATUS_UNKNOWN_ERROR
];

private $status;

private $responseObject;

private $originAddresses;

private $destinationAddresses;

private $rows;

public function __construct(\stdClass $responseObject)
{
$this->responseObject = $responseObject;
$this->originAddresses = array();
$this->destinationAddresses = array();
$this->rows = array();
$this->construct();
}

private function addOriginAddress(Address $originAddress)
{
$this->originAddresses[] = $originAddress;
}

private function addDestinationAddress(Address $destinationAddress)
{
$this->destinationAddresses[] = $destinationAddress;
}

private function addRow(Row $row)
{
$this->rows[] = $row;
}

/**
* @return mixed
*/
public function getStatus()
{
return $this->status;
}

/**
* @return stdClass
*/
public function getResponseObject()
{
return $this->responseObject;
}

/**
* @return array
*/
public function getOriginAddresses()
{
return $this->originAddresses;
}

/**
* @return array
*/
public function getDestinationAddresses()
{
return $this->destinationAddresses;
}

/**
* @return array
*/
public function getRows()
{
return $this->rows;
}


private function construct()
{
$this->status = $this->responseObject->status;

foreach ($this->responseObject->origin_addresses as $originAddress){
$this->addOriginAddress(new Address($originAddress));
}

foreach ($this->responseObject->destination_addresses as $destinationAddress){
$this->addDestinationAddress(new Address($destinationAddress));
}

foreach ($this->responseObject->rows as $row){

$elements = array();
foreach($row->elements as $element){
$duration = new Duration($element->duration->text,$element->duration->value);
$distance = new Distance($element->distance->text,$element->distance->value);
$elements[] = new Element($element->status,$duration,$distance);
}
$this->addRow(new Row($elements));
}

}
}
32 changes: 32 additions & 0 deletions src/Response/Row.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Created by PhpStorm.
* User: ciro
* Date: 10/07/17
* Time: 17:19
*/

namespace Valerian\GoogleDistanceMatrix\Response;


class Row
{
/**
* @var array
*/
private $elements;

public function __construct($elements)
{
$this->elements = $elements;
}

/**
* @return array
*/
public function getElements()
{
return $this->elements;
}

}

0 comments on commit 25453b3

Please sign in to comment.