Skip to content

Commit 66e81d6

Browse files
authored
Merge pull request #3 from niden/master
Corrected includes as per the specification
2 parents 3deca17 + 9f1d980 commit 66e81d6

File tree

15 files changed

+459
-1047
lines changed

15 files changed

+459
-1047
lines changed

README.md

Lines changed: 104 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,15 @@ The routes available are:
5656
5757
#### Relationships
5858

59-
`/companies/<number>/individuals`
60-
`/companies/<number>/products`
61-
`/companies/<number>/individuals,products`
59+
`/companies/<number>?included=<individuals>,<products>`
6260

63-
`/companies/<number>/relationships/individuals`
64-
`/companies/<number>/relationships/products`
65-
`/companies/<number>/relationships/individuals,products`
61+
`individuals/<number>?included=<companies>,<individual-types>`
6662

67-
`individuals/<number>/companies`
68-
`individuals/<number>/individual-types`
69-
`individuals/<number>/companies,individual-types`
63+
`individual-types/<number>?included=<individuals>`
7064

71-
`individuals/<number>/relationships/companies`
72-
`individuals/<number>/relationships/individual-types`
73-
`individuals/<number>/relationships/companies,individual-types`
65+
`products/<number>?included=<companies>,<product-types>`
7466

75-
`individual-types/<number>/individuals`
76-
`individual-types/<number>/relationships/individuals`
77-
78-
`products/<number>/companies`
79-
`products/<number>/product-types`
80-
`products/<number>/companies,product-types`
81-
82-
`products/<number>/relationships/companies`
83-
`products/<number>/relationships/product-types`
84-
`products/<number>/relationships/companies,product-types`
85-
86-
`product-types/<number>/products`
87-
`product-types/<number>/relationships/products`
67+
`product-types/<number>?included=<products>`
8868

8969

9070

@@ -165,13 +145,111 @@ The record always has `id` and `type` present at the top level. `id` is the uniq
165145
}
166146
```
167147

148+
`/products/1134?includes=companies,product-types`
149+
150+
```json
151+
{
152+
"jsonapi": {
153+
"version": "1.0"
154+
},
155+
"data": [
156+
{
157+
"type": "products",
158+
"id": "1134",
159+
"attributes": {
160+
"typeId": 890,
161+
"name": "prd-a-5b64af7e70741",
162+
"description": "5b64af7e7074a",
163+
"quantity": 25,
164+
"price": "19.99"
165+
},
166+
"links": {
167+
"self": "http:\/\/api.phalcon.ld\/products\/1134"
168+
},
169+
"relationships": {
170+
"companies": {
171+
"links": {
172+
"self": "http:\/\/api.phalcon.ld\/products\/1134\/relationships\/companies",
173+
"related": "http:\/\/api.phalcon.ld\/products\/1134\/companies"
174+
},
175+
"data": [
176+
{
177+
"type": "companies",
178+
"id": "1430"
179+
},
180+
{
181+
"type": "companies",
182+
"id": "1431"
183+
}
184+
]
185+
},
186+
"product-types": {
187+
"links": {
188+
"self": "http:\/\/api.phalcon.ld\/products\/1134\/relationships\/product-types",
189+
"related": "http:\/\/api.phalcon.ld\/products\/1134\/product-types"
190+
},
191+
"data": {
192+
"type": "product-types",
193+
"id": "890"
194+
}
195+
}
196+
}
197+
}
198+
],
199+
"included": [
200+
{
201+
"type": "companies",
202+
"id": "1430",
203+
"attributes": {
204+
"name": "com-a5b64af7e6c846",
205+
"address": "5b64af7e6c84f",
206+
"city": "5b64af7e6c855",
207+
"phone": "5b64af7e6c85c"
208+
},
209+
"links": {
210+
"self": "http:\/\/api.phalcon.ld\/companies\/1430"
211+
}
212+
},
213+
{
214+
"type": "companies",
215+
"id": "1431",
216+
"attributes": {
217+
"name": "com-b5b64af7e6e3d3",
218+
"address": "5b64af7e6e3dc",
219+
"city": "5b64af7e6e3e2",
220+
"phone": "5b64af7e6e3e9"
221+
},
222+
"links": {
223+
"self": "http:\/\/api.phalcon.ld\/companies\/1431"
224+
}
225+
},
226+
{
227+
"type": "product-types",
228+
"id": "890",
229+
"attributes": {
230+
"name": "prt-a-5b64af7e6f638",
231+
"description": "5b64af7e6f641"
232+
},
233+
"links": {
234+
"self": "http:\/\/api.phalcon.ld\/product-types\/890"
235+
}
236+
}
237+
],
238+
"meta": {
239+
"timestamp": "2018-08-03T19:39:42+00:00",
240+
"hash": "384c6b3772727b1a9532865d2ae2d51c095c0fd9"
241+
}
242+
}
243+
```
244+
168245
For more information regarding responses, please check [JSON API](https://jsonapi.org)
169246

170247
### TODO
171248
- ~~Work on companies GET~~
172-
- ~~Work on relationships and data returned~~
249+
- ~~Work on included data~~
173250
- Write examples of code to send to the client
174251
- Create docs endpoint
252+
- Work on relationships
175253
- Work on pagination
176254
- Work on filters
177255
- Work on sorting

api/controllers/BaseController.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class BaseController extends Controller
3737
protected $model = '';
3838

3939
/** @var array */
40-
protected $relationships = [];
40+
protected $includes = [];
4141

4242
/** @var string */
4343
protected $resource = '';
@@ -52,28 +52,25 @@ class BaseController extends Controller
5252
* Get the company/companies
5353
*
5454
* @param int $id
55-
* @param string $relationships
5655
*
5756
* @return array
5857
*/
59-
public function callAction($id = 0, $relationships = '')
58+
public function callAction($id = 0)
6059
{
60+
$includes = $this->request->getQuery('includes', [Filter::FILTER_STRING, Filter::FILTER_TRIM], '');
6161
$parameters = $this->checkIdParameter($id);
62-
$parameter = $this->filter->sanitize($relationships, [Filter::FILTER_STRING, Filter::FILTER_TRIM]);
6362
$results = $this->getRecords($this->config, $this->cache, $this->model, $parameters, $this->orderBy);
6463
$related = [];
6564

6665
if (count($parameters) > 0 && 0 === count($results)) {
6766
return $this->send404();
6867
} else {
69-
if (true !== empty($parameter)) {
70-
$allRelationships = explode(',', $relationships);
71-
foreach ($allRelationships as $relationship) {
72-
if (true !== in_array($relationship, $this->relationships)) {
73-
return $this->send404();
68+
if (true !== empty($includes)) {
69+
$requestedIncludes = explode(',', $includes);
70+
foreach ($requestedIncludes as $include) {
71+
if (true === in_array($include, $this->includes)) {
72+
$related[] = strtolower($include);
7473
}
75-
76-
$related[] = strtolower($relationship);
7774
}
7875
}
7976
}

api/controllers/Companies/GetController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
class GetController extends BaseController
1818
{
1919
/** @var string */
20-
protected $model = Companies::class;
20+
protected $model = Companies::class;
2121

2222
/** @var array */
23-
protected $relationships = [
23+
protected $includes = [
2424
Relationships::INDIVIDUALS,
2525
Relationships::PRODUCTS,
2626
];
2727

2828
/** @var string */
29-
protected $resource = Relationships::COMPANIES;
29+
protected $resource = Relationships::COMPANIES;
3030

3131
/** @var string */
32-
protected $transformer = CompaniesTransformer::class;
32+
protected $transformer = CompaniesTransformer::class;
3333
}

api/controllers/IndividualTypes/GetController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class GetController extends BaseController
2020
protected $model = IndividualTypes::class;
2121

2222
/** @var array */
23-
protected $relationships = [
23+
protected $includes = [
2424
Relationships::INDIVIDUALS,
2525
];
2626

api/controllers/Individuals/GetController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@
1717
class GetController extends BaseController
1818
{
1919
/** @var string */
20-
protected $model = Individuals::class;
20+
protected $model = Individuals::class;
2121

2222
/** @var array */
23-
protected $relationships = [
23+
protected $includes = [
2424
Relationships::COMPANIES,
2525
Relationships::INDIVIDUAL_TYPES,
2626
];
2727

2828
/** @var string */
29-
protected $resource = Relationships::INDIVIDUALS;
29+
protected $resource = Relationships::INDIVIDUALS;
3030

3131
/** @var string */
32-
protected $transformer = IndividualsTransformer::class;
32+
protected $transformer = IndividualsTransformer::class;
3333

3434
/** @var string */
35-
protected $orderBy = 'last, first';
35+
protected $orderBy = 'last, first';
3636
}

api/controllers/ProductTypes/GetController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class GetController extends BaseController
2020
protected $model = ProductTypes::class;
2121

2222
/** @var array */
23-
protected $relationships = [
23+
protected $includes = [
2424
Relationships::PRODUCTS,
2525
];
2626

api/controllers/Products/GetController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class GetController extends BaseController
2020
protected $model = Products::class;
2121

2222
/** @var array */
23-
protected $relationships = [
23+
protected $includes = [
2424
Relationships::COMPANIES,
2525
Relationships::PRODUCT_TYPES,
2626
];

library/Providers/CacheDataProvider.php

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,30 @@ public function register(DiInterface $container)
1717
{
1818
$container->setShared(
1919
'cache',
20-
$this->createCache('data')
21-
);
22-
}
23-
24-
/**
25-
* Returns a cache object
26-
*
27-
* @param string $prefix
28-
*
29-
* @return Libmemcached
30-
*/
31-
protected function createCache(string $prefix): Libmemcached
32-
{
33-
$frontAdapter = Data::class;
34-
$frontOptions = [
35-
'lifetime' => envValue('CACHE_LIFETIME', 86400),
36-
];
37-
$backOptions = $this->createOptions($prefix);
20+
function () {
21+
$prefix = 'data';
22+
$frontAdapter = Data::class;
23+
$frontOptions = [
24+
'lifetime' => envValue('CACHE_LIFETIME', 86400),
25+
];
26+
$backOptions = [
27+
'servers' => [
28+
0 => [
29+
'host' => envValue('DATA_API_MEMCACHED_HOST', '127.0.0.1'),
30+
'port' => envValue('DATA_API_MEMCACHED_PORT', 11211),
31+
'weight' => envValue('DATA_API_MEMCACHED_WEIGHT', 100),
32+
],
33+
],
34+
'client' => [
35+
\Memcached::OPT_HASH => \Memcached::HASH_MD5,
36+
\Memcached::OPT_PREFIX_KEY => 'api-',
37+
],
38+
'lifetime' => 3600,
39+
'prefix' => $prefix . '-',
40+
];
3841

39-
return new Libmemcached(new $frontAdapter($frontOptions), $backOptions);
40-
}
41-
42-
/**
43-
* Returns memcached options
44-
*
45-
* @param string $prefix
46-
*
47-
* @return array
48-
*/
49-
protected function createOptions(string $prefix): array
50-
{
51-
return [
52-
'servers' => [
53-
0 => [
54-
'host' => envValue('DATA_API_MEMCACHED_HOST', '127.0.0.1'),
55-
'port' => envValue('DATA_API_MEMCACHED_PORT', 11211),
56-
'weight' => envValue('DATA_API_MEMCACHED_WEIGHT', 100),
57-
],
58-
],
59-
'client' => [
60-
\Memcached::OPT_HASH => \Memcached::HASH_MD5,
61-
\Memcached::OPT_PREFIX_KEY => 'api-',
62-
],
63-
'lifetime' => 3600,
64-
'prefix' => $prefix . '-',
65-
];
42+
return new Libmemcached(new $frontAdapter($frontOptions), $backOptions);
43+
}
44+
);
6645
}
6746
}

library/Providers/ResponseProvider.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,21 @@ class ResponseProvider implements ServiceProviderInterface
1515
*/
1616
public function register(DiInterface $container)
1717
{
18-
$response = new Response();
18+
$container->setShared(
19+
'response',
20+
function () {
21+
$response = new Response();
1922

20-
/**
21-
* Assume success. We will work with the edge cases in the code
22-
*/
23-
$response
24-
->setStatusCode(200)
25-
->setContentType('application/vnd.api+json', 'UTF-8')
26-
;
23+
/**
24+
* Assume success. We will work with the edge cases in the code
25+
*/
26+
$response
27+
->setStatusCode(200)
28+
->setContentType('application/vnd.api+json', 'UTF-8')
29+
;
2730

28-
$container->setShared('response', $response);
31+
return $response;
32+
}
33+
);
2934
}
3035
}

0 commit comments

Comments
 (0)