1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\responder; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\HttpFoundation\Request; |
5
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
6
|
|
|
use keeko\framework\foundation\AbstractPayloadResponder; |
7
|
|
|
use keeko\framework\domain\payload\NotFound; |
8
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
9
|
|
|
use keeko\core\model\Country; |
10
|
|
|
use keeko\core\model\Continent; |
11
|
|
|
use keeko\core\model\Currency; |
12
|
|
|
use keeko\core\model\RegionType; |
13
|
|
|
use keeko\core\model\Subdivision; |
14
|
|
|
use keeko\framework\domain\payload\Found; |
15
|
|
|
use Tobscure\JsonApi\Document; |
16
|
|
|
use Tobscure\JsonApi\Resource; |
17
|
|
|
use Tobscure\JsonApi\Parameters; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Automatically generated JsonResponder for Reads a country |
21
|
|
|
* |
22
|
|
|
* @author gossi |
23
|
|
|
*/ |
24
|
|
|
class CountryReadJsonResponder extends AbstractPayloadResponder { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Request $request |
28
|
|
|
* @param Found $payload |
29
|
|
|
*/ |
30
|
|
|
public function found(Request $request, Found $payload) { |
31
|
|
|
$params = new Parameters($request->query->all()); |
32
|
|
|
$serializer = Country::getSerializer(); |
33
|
|
|
$resource = new Resource($payload->getModel(), $serializer); |
34
|
|
|
$resource = $resource->with($params->getInclude(['continent', 'currency', 'type', 'subtype', 'subordinate', 'country', 'subdivision'])); |
35
|
|
|
$resource = $resource->fields($params->getFields([ |
36
|
|
|
'country' => Country::getSerializer()->getFields(), |
37
|
|
|
'continent' => Continent::getSerializer()->getFields(), |
38
|
|
|
'currency' => Currency::getSerializer()->getFields(), |
39
|
|
|
'type' => RegionType::getSerializer()->getFields(), |
40
|
|
|
'subtype' => RegionType::getSerializer()->getFields(), |
41
|
|
|
'subordinate' => Country::getSerializer()->getFields(), |
42
|
|
|
'subdivision' => Subdivision::getSerializer()->getFields() |
43
|
|
|
])); |
44
|
|
|
$document = new Document($resource); |
45
|
|
|
|
46
|
|
|
return new JsonResponse($document->toArray(), 200); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Request $request |
51
|
|
|
* @param NotFound $payload |
52
|
|
|
*/ |
53
|
|
|
public function notFound(Request $request, NotFound $payload) { |
54
|
|
|
throw new ResourceNotFoundException($payload->getMessage()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
*/ |
59
|
|
|
protected function getPayloadMethods() { |
60
|
|
|
return [ |
61
|
|
|
'keeko\framework\domain\payload\Found' => 'found', |
62
|
|
|
'keeko\framework\domain\payload\NotFound' => 'notFound' |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|