Guest User

Untitled

a guest
Jul 28th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. <?php namespace Edmunds;
  2.  
  3. use EdmundsEndpointsEdmundsEndpointInterface;
  4.  
  5. class EdmundsApi {
  6. private $domain = 'https://api.edmunds.com';
  7. private $endpoint;
  8. private $parameters = [
  9. 'fmt' => 'json',
  10. 'api_key' => 'api_key_goes_here',
  11. ];
  12.  
  13. public function __construct(EdmundsEndpointInterface $endpoint, $parameters = [])
  14. {
  15. $this->endpoint = $endpoint;
  16. $this->parameters = array_merge($this->parameters, $parameters);
  17. }
  18.  
  19. public function getResult()
  20. {
  21. return json_decode(file_get_contents($this->getBuiltUrl()));
  22. }
  23.  
  24. private function getBuiltUrl()
  25. {
  26. $url = strtr($this->getBaseUrl(), $this->getRouteParameters());
  27.  
  28. if (count($this->getQueryParameters()))
  29. {
  30. $url .= '?' . http_build_query($this->getQueryParameters());
  31. }
  32.  
  33. return $url;
  34. }
  35.  
  36. private function getBaseUrl()
  37. {
  38. return $this->domain . $this->endpoint->getPath();
  39. }
  40.  
  41. private function getAllParameters()
  42. {
  43. return array_merge($this->parameters, $this->endpoint->getParameters());
  44. }
  45.  
  46. private function getRouteParameters()
  47. {
  48. return array_intersect_key(
  49. $this->getAllParameters(),
  50. array_filter(
  51. $this->getAllParameters(),
  52. function($key) {
  53. return strpos($key, ':') === 0;
  54. },
  55. ARRAY_FILTER_USE_KEY
  56. )
  57. );
  58. }
  59.  
  60. private function getQueryParameters()
  61. {
  62. return array_diff($this->getAllParameters(), $this->getRouteParameters());
  63. }
  64. }
  65.  
  66. <?php namespace EdmundsEndpoints;
  67.  
  68. interface EdmundsEndpointInterface {
  69. public function getPath();
  70. public function getParameters();
  71. }
  72.  
  73. <?php namespace EdmundsEndpoints;
  74.  
  75. abstract class EdmundsEndpoint implements EdmundsEndpointInterface {
  76. protected $path;
  77. protected $parameters = [];
  78.  
  79. public function getPath()
  80. {
  81. return $this->path;
  82. }
  83.  
  84. public function getParameters()
  85. {
  86. return $this->parameters;
  87. }
  88. }
  89.  
  90. <?php namespace EdmundsEndpoints;
  91.  
  92. class ModelYearEndpoint extends EdmundsEndpoint {
  93. public function __construct($make, $model, $year)
  94. {
  95. $this->path = '/api/vehicle/v2/:make/:model/:year';
  96. $this->parameters[':make'] = $make;
  97. $this->parameters[':model'] = $model;
  98. $this->parameters[':year'] = $year;
  99. }
  100. }
  101.  
  102. <?php namespace EdmundsEndpoints;
  103.  
  104. class MaintenanceEndpoint extends EdmundsEndpoint {
  105. public function __construct($id)
  106. {
  107. $this->path = '/v1/api/maintenance/actionrepository/findbymodelyearid';
  108. $this->parameters['modelyearid'] = $id;
  109. }
  110. }
  111.  
  112. $model_api = new EdmundsApi(new ModelYearEndpoint($make, $model, $year), ['view' => 'basic']);
  113. $maint_api = new EdmundsApi(new MaintenanceEndpoint($id));
  114. $model_result = $model_api->getResult();
  115. $maint_result = $maint_api->getResult();
Add Comment
Please, Sign In to add comment