forked from miroslav-valerian/google-distance-matrix
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exceptions, response mapping objects
- Loading branch information
1 parent
7b10146
commit 25453b3
Showing
6 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |