Skip to content

Commit 6f5f492

Browse files
committed
Add some documentation
1 parent 362b3e6 commit 6f5f492

File tree

5 files changed

+376
-17
lines changed

5 files changed

+376
-17
lines changed

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,44 @@ A PHP SDK for Firebase REST API.
55

66
[![Build status][Master image]][Master]
77

8+
-----------------------------------
9+
10+
## Installation
11+
12+
```
13+
composer require adrorocker/php-firebase
14+
```
15+
-----------------------------------
16+
17+
## Usage
18+
19+
```
20+
require '../vendor/autoload.php';
21+
22+
use PhpFirebase\Firebase;
23+
24+
// Base endpoint
25+
$base = 'https://hey-123.firebaseio.com/somesubendpoint';
26+
27+
// Auth token
28+
$token = 'a1b2c3d4e5f6g7h8i9';
29+
30+
$firebase = new Firebase($base,$token);
31+
32+
// Unique ID
33+
$id = (new \DateTime())->getTimestamp();
34+
35+
$data = ['key' => 'value']; // Or even just a string
36+
37+
// Make a PUT request and retive the response
38+
$put = $firebase->put('/logs/'.$id, $data)->getResponse();
839
40+
// Make a GET request and retive the response, you will see all the logs
41+
$get = $firebase->get('/logs')->getResponse();
42+
```
943
-----------------------------------
1044

11-
Authors:
45+
## Authors:
1246

1347
[Alejandro Morelos](https://github.com/adrorocker).
1448

src/Clients/GuzzleClient.php

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,44 @@
1717
use GuzzleHttp\Psr7\Uri;
1818
use function GuzzleHttp\Psr7\stream_for;
1919

20+
/**
21+
* Guzzle Client.
22+
*
23+
* @package PhpFirebase
24+
* @subpackage Clients
25+
* @since 0.1.0
26+
*/
2027
class GuzzleClient implements ClientInterface
2128
{
29+
/**
30+
* Guzzle client
31+
*
32+
* @var \GuzzleHttp\Client
33+
*/
2234
protected $guzzle;
2335

36+
/**
37+
* Base endpoint
38+
*
39+
* @var string
40+
*/
2441
protected $base;
2542

43+
/**
44+
* Token
45+
*
46+
* @var string
47+
*/
2648
protected $token;
2749

50+
/**
51+
* Set the base path for Firebase endpont
52+
* the token to authenticate and the guzzle client
53+
*
54+
* @param string $base The base endpoint
55+
* @param string $token The token
56+
* @param \PhpFirebase\Interfaces\ClientInterface|null $client Client to make the request
57+
*/
2858
public function __construct(array $options = [])
2959
{
3060
if (!isset($options['base'])) {
@@ -41,6 +71,14 @@ public function __construct(array $options = [])
4171
$this->guzzle = new HttpClient($options);
4272
}
4373

74+
/**
75+
* Create a new GET reuest
76+
*
77+
* @param string $endpoint The sub endpoint
78+
* @param array $query Query parameters
79+
*
80+
* @return array
81+
*/
4482
public function get($endpoint, $query = [])
4583
{
4684
$request = new Request('GET',$this->buildUri($endpoint, $query), $this->buildHeaders());
@@ -50,6 +88,15 @@ public function get($endpoint, $query = [])
5088
return $this->handle($response);
5189
}
5290

91+
/**
92+
* Create a new POST reuest
93+
*
94+
* @param string $endpoint The sub endpoint
95+
* @param string|array $data The data to be submited
96+
* @param array $query Query parameters
97+
*
98+
* @return array
99+
*/
53100
public function post($endpoint, $data, $query = [])
54101
{
55102
$data = $this->prepareData($data);
@@ -61,6 +108,15 @@ public function post($endpoint, $data, $query = [])
61108
return $this->handle($response);
62109
}
63110

111+
/**
112+
* Create a new PUT reuest
113+
*
114+
* @param string $endpoint The sub endpoint
115+
* @param string|array $data The data to be submited
116+
* @param array $query Query parameters
117+
*
118+
* @return array
119+
*/
64120
public function put($endpoint, $data, $query = [])
65121
{
66122
$data = $this->prepareData($data);
@@ -72,6 +128,15 @@ public function put($endpoint, $data, $query = [])
72128
return $this->handle($response);
73129
}
74130

131+
/**
132+
* Create a new PATCH reuest
133+
*
134+
* @param string $endpoint The sub endpoint
135+
* @param string|array $data The data to be submited
136+
* @param array $query Query parameters
137+
*
138+
* @return array
139+
*/
75140
public function patch($endpoint, $data, $query = [])
76141
{
77142
$data = $this->prepareData($data);
@@ -83,6 +148,14 @@ public function patch($endpoint, $data, $query = [])
83148
return $this->handle($response);
84149
}
85150

151+
/**
152+
* Create a new DELETE reuest
153+
*
154+
* @param string $endpoint The sub endpoint
155+
* @param array $query Query parameters
156+
*
157+
* @return array
158+
*/
86159
public function delete($endpoint, $query = [])
87160
{
88161
$request = new Request('DELETE',$this->buildUri($endpoint, $query), $this->buildHeaders());
@@ -92,11 +165,27 @@ public function delete($endpoint, $query = [])
92165
return $this->handle($response);
93166
}
94167

168+
/**
169+
* Convert array|string to json
170+
*
171+
* @param array $data Data to be converted
172+
*
173+
* @return array
174+
*/
95175
protected function prepareData($data = [])
96176
{
97177
return json_encode($data);
98178
}
99179

180+
/**
181+
* Create a standard uri based on the end point
182+
* and add the auth token
183+
*
184+
* @param string $endpoint The sub endpoint
185+
* @param array $options Extra options to be added
186+
*
187+
* @return string
188+
*/
100189
protected function buildUri($endpoint, $options = [])
101190
{
102191
if ($this->token !== '') {
@@ -106,6 +195,13 @@ protected function buildUri($endpoint, $options = [])
106195
return $this->base . '/' . ltrim($endpoint, '/') . '.json?' . http_build_query($options, '', '&');
107196
}
108197

198+
/**
199+
* Build all headers
200+
*
201+
* @param array $extraHeaders Extra headers to be added
202+
*
203+
* @return array
204+
*/
109205
protected function buildHeaders($extraHeaders = [])
110206
{
111207
$headers = [
@@ -116,7 +212,14 @@ protected function buildHeaders($extraHeaders = [])
116212
return array_merge($headers, $extraHeaders);
117213
}
118214

119-
private function handle(Response $response, $default = null)
215+
/**
216+
* Handle the response
217+
*
218+
* @param \GuzzleHttp\Psr7\Response $response The response
219+
*
220+
* @return array
221+
*/
222+
private function handle(Response $response)
120223
{
121224
$stream = stream_for($response->getBody());
122225

0 commit comments

Comments
 (0)