Skip to content

Commit 6350981

Browse files
committed
Implement getById method
Add responseCode to Response class Add headers to Response class
1 parent aec96ec commit 6350981

File tree

7 files changed

+155
-83
lines changed

7 files changed

+155
-83
lines changed

src/API.php renamed to src/Client.php

Lines changed: 78 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
use Phramework\JSONAPI\Client\Response\Resource;
2222

2323
/**
24-
* Class API
2524
* @author Xenofon Spafaridis <[email protected]>
2625
* @since 0.0.0
2726
* @todo handle errors
2827
*/
29-
abstract class API
28+
abstract class Client
3029
{
3130
/**
3231
* @var object|null
@@ -142,7 +141,18 @@ protected function prepareHeaders(\stdClass $additional = null)
142141
* )
143142
* );
144143
* ```
145-
* @todo provide examples for all arguments
144+
* @example
145+
* ```php
146+
* $users = Users::get(
147+
* null,
148+
* null,
149+
* new Sort(null, 'created', false),
150+
* new Fields((object) [
151+
* 'user' => ['name', 'email']
152+
* ]),
153+
* new IncludeRelationship('project', 'group')
154+
* );
155+
* ```
146156
*/
147157
public static function get(
148158
Page $page = null,
@@ -155,7 +165,7 @@ public static function get(
155165
) {
156166
$API = self::getGlobalAPI();
157167

158-
$url = $API . static::$endpoint;
168+
$url = $API . static::$endpoint . '/';
159169

160170
//prepare parts
161171

@@ -199,8 +209,6 @@ public static function get(
199209

200210
$headers = static::prepareHeaders($additionalHeaders);
201211

202-
/*var_dump($url);*/
203-
204212
list(
205213
$responseStatusCode,
206214
$responseHeaders,
@@ -210,37 +218,86 @@ public static function get(
210218
self::METHOD_GET,
211219
$headers
212220
);
213-
/*
214-
var_dump(
215-
$responseStatusCode,
216-
$responseHeaders,
217-
$responseBody
218-
);*/
219221

220222
$collection = (new Collection())->parse(
221223
$responseBody
222224
);
223225

224-
$resource = (new Resource())->parse(
225-
$responseBody
226-
);
226+
$collection->setStatusCode($responseStatusCode);
227+
$collection->setHeaders($responseHeaders);
227228

228229
return $collection;
229-
230-
//data
231-
//include
232-
//links
233-
//meta
234230
}
235231

232+
/**
233+
* @param string $id Resource id
234+
* @param Fields|null $fields Fields directive
235+
* @param IncludeRelationship|null $include Include directive
236+
* @param \stdClass|null $additionalHeaders Will override global
237+
* headers
238+
* @param \string[] ...$additional Additional url
239+
* @return Resource
240+
* @throws Exception
241+
* @example
242+
* ```php
243+
* $user = User::getById(
244+
* '10',
245+
* null,
246+
* new IncludeRelationship('project', 'group')
247+
* )
248+
* ```
249+
*/
236250
public static function getById(
237-
$id,
251+
string $id,
238252
Fields $fields = null,
239253
IncludeRelationship $include = null,
240254
\stdClass $additionalHeaders = null,
241255
string ...$additional
242256
) {
257+
$API = self::getGlobalAPI();
258+
259+
$url = $API . static::$endpoint . '/' . $id . '/';
260+
261+
//prepare parts
262+
$fieldsPart = ($fields ? $fields->toURL() : '');
263+
$includePart = ($include ? $include->toURL() : '');
264+
265+
$questionMark = false;
266+
267+
//append parts
268+
if (!empty($fieldsPart)) {
269+
$url = $url . ($questionMark ? '&' : '?') . $fieldsPart;
270+
$questionMark = true;
271+
}
272+
273+
if (!empty($includePart)) {
274+
$url = $url . ($questionMark ? '&' : '?') . $includePart;
275+
$questionMark = true;
276+
}
277+
278+
//Append additional
279+
$url = $url . implode('', $additional);
280+
281+
$headers = static::prepareHeaders($additionalHeaders);
282+
283+
list(
284+
$responseStatusCode,
285+
$responseHeaders,
286+
$responseBody
287+
) = static::request(
288+
$url,
289+
self::METHOD_GET,
290+
$headers
291+
);
292+
293+
$resource = (new Resource())->parse(
294+
$responseBody
295+
);
296+
297+
$resource->setStatusCode($responseStatusCode);
298+
$resource->setHeaders($responseHeaders);
243299

300+
return $resource;
244301
}
245302

246303
public static function post(

src/Response/Collection.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,4 @@ class Collection extends Response
3333
* @var ResourceObject[]
3434
*/
3535
public $included;
36-
37-
/**
38-
* Collection constructor.
39-
* @param ResourceObject[] $data
40-
* @param ResourceObject[] $included
41-
*/
42-
public function __construct(
43-
array $data = null,
44-
array $included = null,
45-
$links = null,
46-
$meta = null
47-
) {
48-
$this->data = $data;
49-
$this->included = $included;
50-
51-
parent::__construct($meta, $links);
52-
}
5336
}

src/Response/Resource.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,4 @@ class Resource extends Response
3434
* @var ResourceObject[]
3535
*/
3636
public $included;
37-
38-
/**
39-
* Collection constructor.
40-
* @param ResourceObject[] $data
41-
* @param ResourceObject[] $included
42-
*/
43-
/* public function __construct(
44-
$data = null,
45-
array $included = null,
46-
$links = null,
47-
$meta = null
48-
) {
49-
$this->data = $data;
50-
$this->included = $included;
51-
52-
parent::__construct($meta, $links);
53-
}*/
5437
}

src/Response/Response.php

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,72 @@ class Response
3232
*/
3333
public $links;
3434

35+
/**
36+
* @var integer
37+
*/
38+
protected $statusCode;
39+
40+
/**
41+
* @var \stdClass
42+
*/
43+
protected $headers;
44+
3545
/**
3646
* Response constructor.
37-
* @param $links
38-
* @param $meta
3947
*/
4048
public function __construct(
41-
$links = null,
42-
$meta = null
4349
) {
44-
$this->links = $links;
45-
$this->meta = $meta;
4650
}
4751

4852
/**
49-
* @return mixed
53+
* @param \stdClass $response
54+
* @return $this
5055
*/
51-
public function getMeta()
56+
public function parse(\stdClass $response) {
57+
$members = array_keys(get_object_vars($this));
58+
59+
foreach ($members as $member) {
60+
$this->{$member} = $response->{$member} ?? null;
61+
}
62+
63+
return $this;
64+
}
65+
66+
/**
67+
* @return int
68+
*/
69+
public function getStatusCode()
5270
{
53-
return $this->meta;
71+
return $this->statusCode;
5472
}
5573

5674
/**
57-
* @return mixed
75+
* @param int $statusCode
76+
* @return $this
5877
*/
59-
public function getLinks()
78+
public function setStatusCode($statusCode)
6079
{
61-
return $this->links;
80+
$this->statusCode = $statusCode;
81+
82+
return $this;
6283
}
63-
64-
public function parse(\stdClass $response) {
65-
$members = array_keys(get_object_vars($this));
6684

67-
foreach ($members as $member) {
68-
$this->{$member} = $response->{$member} ?? null;
69-
}
85+
/**
86+
* @return \stdClass
87+
*/
88+
public function getHeaders()
89+
{
90+
return $this->headers;
91+
}
92+
93+
/**
94+
* @param \stdClass $headers
95+
* @return $this
96+
*/
97+
public function setHeaders($headers)
98+
{
99+
$this->headers = $headers;
70100

71101
return $this;
72102
}
73-
}
103+
}

tests/APP/User.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
*/
1717
namespace Phramework\JSONAPI\Client\APP;
1818

19-
use Phramework\JSONAPI\Client\API;
19+
use Phramework\JSONAPI\Client\Client;
2020

2121
/**
2222
* @author Xenofon Spafaridis <[email protected]>
2323
* @since 0.0.0
2424
*/
25-
class User extends API
25+
class User extends Client
2626
{
2727
protected static $endpoint = 'user';
2828
protected static $type = 'user';

tests/bootstrap.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@
1616
*/
1717
require __DIR__ . '/../vendor/autoload.php';
1818

19-
\Phramework\JSONAPI\Client\API::setGlobalAPI(
19+
\Phramework\JSONAPI\Client\Client::setGlobalAPI(
20+
''
2021
);
2122

22-
\Phramework\JSONAPI\Client\API::setGlobalHeader(
23+
\Phramework\JSONAPI\Client\Client::setGlobalHeader(
2324
\Phramework\Models\Request::HEADER_CONTENT_TYPE,
2425
'application/vnd.api+json'
2526
);
2627

27-
\Phramework\JSONAPI\Client\API::setGlobalHeader(
28+
\Phramework\JSONAPI\Client\Client::setGlobalHeader(
2829
\Phramework\Models\Request::HEADER_ACCEPT,
2930
'application/vnd.api+json'
3031
);
3132

32-
\Phramework\JSONAPI\Client\API::setGlobalHeader(
33+
\Phramework\JSONAPI\Client\Client::setGlobalHeader(
3334
'Authorization',
34-
'xxx'
35+
''
3536
);

tests/src/APITest.php renamed to tests/src/ClientTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
/**
2424
* @author Xenofon Spafaridis <[email protected]>
2525
* @since 0.0.0
26-
* @coversDefaultClass \Phramework\JSONAPI\Client\API
26+
* @coversDefaultClass \Phramework\JSONAPI\Client\Client
2727
*/
28-
class APITest extends \PHPUnit_Framework_TestCase
28+
class ClientTest extends \PHPUnit_Framework_TestCase
2929
{
3030
/**
3131
* @covers ::get
@@ -49,6 +49,24 @@ public function testGet()
4949
new IncludeRelationship('project', 'group')
5050
);*/
5151

52+
$userId = $users->data[0]->id;
53+
5254
var_dump($users);
55+
56+
return $userId;
57+
}
58+
59+
/**
60+
* @param string $userId
61+
* @covers ::get
62+
* @depends testGet
63+
*/
64+
public function testGetById($userId)
65+
{
66+
$user = User::getById(
67+
$userId
68+
);
69+
70+
var_dump($user);
5371
}
5472
}

0 commit comments

Comments
 (0)