Library to query Edmunds' API

I'm attempting to create a library to query Edmunds' API. I'm not sure if the design is over-engineered and would like some feedback. Here is the code:

The API class (takes and endpoint and additional parameters to query Edmunds' API)

<?php namespace Edmunds;  use Edmunds\Endpoints\EdmundsEndpointInterface;  class EdmundsApi {     private $domain = 'https://api.edmunds.com';     private $endpoint;     private $parameters = [         'fmt' => 'json',         'api_key' => 'api_key_goes_here',     ];      public function __construct(EdmundsEndpointInterface $endpoint, $parameters = [])     {         $this->endpoint = $endpoint;         $this->parameters = array_merge($this->parameters, $parameters);     }      public function getResult()     {         return json_decode(file_get_contents($this->getBuiltUrl()));     }      private function getBuiltUrl()     {         $url = strtr($this->getBaseUrl(), $this->getRouteParameters());          if (count($this->getQueryParameters()))         {             $url .= '?' . http_build_query($this->getQueryParameters());         }          return $url;     }      private function getBaseUrl()     {         return $this->domain . $this->endpoint->getPath();     }      private function getAllParameters()     {         return array_merge($this->parameters, $this->endpoint->getParameters());     }      private function getRouteParameters()     {         return array_intersect_key(             $this->getAllParameters(),             array_filter(                 $this->getAllParameters(),                 function($key) {                     return strpos($key, ':') === 0;                 },                 ARRAY_FILTER_USE_KEY             )         );     }      private function getQueryParameters()     {         return array_diff($this->getAllParameters(), $this->getRouteParameters());     } } 

The endpoint interface (promises to get a path and parameters)

<?php namespace Edmunds\Endpoints;  interface EdmundsEndpointInterface {     public function getPath();     public function getParameters(); } 

The abstract endpoint class to reduce boilerplate in extended classes

<?php namespace Edmunds\Endpoints;  abstract class EdmundsEndpoint implements EdmundsEndpointInterface {     protected $path;     protected $parameters = [];      public function getPath()     {         return $this->path;     }      public function getParameters()     {         return $this->parameters;     } } 

An actual implementations for the respective endpoints

<?php namespace Edmunds\Endpoints;  class ModelYearEndpoint extends EdmundsEndpoint {     public function __construct($make, $model, $year)     {         $this->path = '/api/vehicle/v2/:make/:model/:year';         $this->parameters[':make'] = $make;         $this->parameters[':model'] = $model;         $this->parameters[':year'] = $year;     } } 
<?php namespace Edmunds\Endpoints;  class MaintenanceEndpoint extends EdmundsEndpoint {     public function __construct($id)     {         $this->path = '/v1/api/maintenance/actionrepository/findbymodelyearid';         $this->parameters['modelyearid'] = $id;     } } 

Example of use

$model_api = new EdmundsApi(new ModelYearEndpoint($make, $model, $year), ['view' => 'basic']); $maint_api = new EdmundsApi(new MaintenanceEndpoint($id)); $model_result = $model_api->getResult(); $maint_result = $maint_api->getResult(); 

Questions:

  1. The EdmundsEndpointInterface seems unnecessary to me. Should I nix it and simply type-hint the abstract class in the EdmundsApi constructor? Why or why wouldn't you?
  2. Originally, EdmundsEndpoint::$path was a static variable. This made sense to me because it identifies one endpoint from another and is immutable (same thoughts with EdmundsApi::$domain). However, after reading articles advising me to not use static variables, I decided to make it a member variable instead. It also didn't feel right that in order to get the static path from an endpoint, I would have to first use get_class($endpoint) because I cannot get a static variable from an object directly. Would you define this variable as static? Why or why not?
  3. Are there any issues you see with how this was implemented in general?

Replay

Category: php Time: 2016-07-28 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.255 (s). 12 q(s)