|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stevenmaguire\Yelp\v3; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
6
|
|
|
use Stevenmaguire\Yelp\Contract\Http as HttpContract; |
|
7
|
|
|
use Stevenmaguire\Yelp\Tool\ConfigurationTrait; |
|
8
|
|
|
use Stevenmaguire\Yelp\Tool\HttpTrait; |
|
9
|
|
|
|
|
10
|
|
|
class Client implements HttpContract |
|
11
|
|
|
{ |
|
12
|
|
|
use ConfigurationTrait, |
|
13
|
|
|
HttpTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Access token |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $accessToken; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Creates new client |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $options |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(array $options = array()) |
|
28
|
|
|
{ |
|
29
|
|
|
$defaults = [ |
|
30
|
|
|
'accessToken' => null, |
|
31
|
|
|
'apiHost' => 'api.yelp.com' |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
$this->parseConfiguration($options, $defaults); |
|
35
|
|
|
|
|
36
|
|
|
if (!$this->getHttpClient()) { |
|
37
|
|
|
$this->setHttpClient($this->createDefaultHttpClient()); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Creates default http client with appropriate authorization configuration. |
|
43
|
|
|
* |
|
44
|
|
|
* @return HttpClient |
|
45
|
|
|
*/ |
|
46
|
|
|
public function createDefaultHttpClient() |
|
47
|
|
|
{ |
|
48
|
|
|
return new HttpClient([ |
|
|
|
|
|
|
49
|
|
|
'headers' => [ |
|
50
|
|
|
'Authorization' => 'Bearer ' . $this->accessToken, |
|
51
|
|
|
] |
|
52
|
|
|
]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Fetches results from the Autocomplete API. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $parameters |
|
59
|
|
|
* |
|
60
|
|
|
* @return stdClass |
|
61
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
|
62
|
|
|
* @link https://www.yelp.com/developers/documentation/v3/autocomplete |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getAutocompleteResults($parameters = []) |
|
65
|
|
|
{ |
|
66
|
|
|
$path = $this->appendParametersToUrl('/v3/autocomplete', $parameters); |
|
67
|
|
|
$request = $this->getRequest('GET', $path); |
|
68
|
|
|
|
|
69
|
|
|
return $this->processRequest($request); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Fetches a specific business by id. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $businessId |
|
76
|
|
|
* @param array $parameters |
|
77
|
|
|
* |
|
78
|
|
|
* @return stdClass |
|
79
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
|
80
|
|
|
* @link https://www.yelp.com/developers/documentation/v3/business |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getBusiness($businessId, $parameters = []) |
|
83
|
|
|
{ |
|
84
|
|
|
$path = $this->appendParametersToUrl('/v3/businesses/'.$businessId, $parameters); |
|
85
|
|
|
$request = $this->getRequest('GET', $path); |
|
86
|
|
|
|
|
87
|
|
|
return $this->processRequest($request); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Fetches reviews for a specific business by id. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $businessId |
|
94
|
|
|
* @param array $parameters |
|
95
|
|
|
* |
|
96
|
|
|
* @return stdClass |
|
97
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
|
98
|
|
|
* @link https://www.yelp.com/developers/documentation/v3/business_reviews |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getBusinessReviews($businessId, $parameters = []) |
|
101
|
|
|
{ |
|
102
|
|
|
$path = $this->appendParametersToUrl('/v3/businesses/'.$businessId.'/reviews', $parameters); |
|
103
|
|
|
$request = $this->getRequest('GET', $path); |
|
104
|
|
|
|
|
105
|
|
|
return $this->processRequest($request); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Fetches results from the Business Search API. |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $parameters |
|
112
|
|
|
* |
|
113
|
|
|
* @return stdClass |
|
114
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
|
115
|
|
|
* @link https://www.yelp.com/developers/documentation/v3/business_search |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getBusinessesSearchResults($parameters = []) |
|
118
|
|
|
{ |
|
119
|
|
|
$csvParams = ['attributes', 'categories', 'price']; |
|
120
|
|
|
|
|
121
|
|
|
$path = $this->appendParametersToUrl('/v3/businesses/search', $parameters, $csvParams); |
|
122
|
|
|
$request = $this->getRequest('GET', $path); |
|
123
|
|
|
|
|
124
|
|
|
return $this->processRequest($request); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Fetches results from the Business Search API by Phone. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $phoneNumber |
|
131
|
|
|
* |
|
132
|
|
|
* @return stdClass |
|
133
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
|
134
|
|
|
* @link https://www.yelp.com/developers/documentation/v3/business_search_phone |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getBusinessesSearchResultsByPhone($phoneNumber) |
|
137
|
|
|
{ |
|
138
|
|
|
$parameters = [ |
|
139
|
|
|
'phone' => $phoneNumber |
|
140
|
|
|
]; |
|
141
|
|
|
|
|
142
|
|
|
$path = $this->appendParametersToUrl('/v3/businesses/search/phone', $parameters); |
|
143
|
|
|
$request = $this->getRequest('GET', $path); |
|
144
|
|
|
|
|
145
|
|
|
return $this->processRequest($request); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Fetches results from the Business Search API by Type. |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $type |
|
152
|
|
|
* @param array $parameters |
|
153
|
|
|
* |
|
154
|
|
|
* @return stdClass |
|
155
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
|
156
|
|
|
* @link https://www.yelp.com/developers/documentation/v3/transactions_search |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getTransactionsSearchResultsByType($type, $parameters = []) |
|
159
|
|
|
{ |
|
160
|
|
|
$path = $this->appendParametersToUrl('/v3/transactions/'.$type.'/search', $parameters); |
|
161
|
|
|
$request = $this->getRequest('GET', $path); |
|
162
|
|
|
|
|
163
|
|
|
return $this->processRequest($request); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.