1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace Xervice\Service\Controller; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Xervice\DataProvider\DataProvider\AbstractDataProvider; |
10
|
|
|
use Xervice\Service\Application\Response\ApiResponse; |
11
|
|
|
use Xervice\Service\Controller\Exception\ApiControllerException; |
12
|
|
|
|
13
|
|
|
abstract class AbstractApiController extends AbstractController |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \Xervice\DataProvider\DataProvider\AbstractDataProvider |
17
|
|
|
*/ |
18
|
|
|
private $dataProvider; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Illuminate\Http\Request |
22
|
|
|
*/ |
23
|
|
|
private $request; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param \Illuminate\Http\Request $request |
27
|
|
|
* @param string $method |
28
|
|
|
* @param mixed ...$params |
29
|
|
|
* |
30
|
|
|
* @return mixed |
31
|
|
|
* @throws \Xervice\Service\Controller\Exception\ApiControllerException |
32
|
|
|
*/ |
33
|
|
|
public function apiAction(Request $request, string $method, ...$params) |
34
|
|
|
{ |
35
|
|
|
$this->request = $request; |
36
|
|
|
|
37
|
|
|
$jsonBody = $request->getContent(); |
38
|
|
|
$data = json_decode($jsonBody, true); |
39
|
|
|
|
40
|
|
|
if (!$data || !isset($data['class']) || !isset($data['data'])) { |
41
|
|
|
throw new ApiControllerException('Request body is no valid json' . PHP_EOL . $jsonBody); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$class = $data['class']; |
45
|
|
|
$this->dataProvider = new $class(); |
46
|
|
|
$this->dataProvider->fromArray($data['data']); |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
$method = $this->dashesToCamelCase($method); |
50
|
|
|
$execMethod = $method . 'Action'; |
51
|
|
|
|
52
|
|
|
if (!method_exists($this, $execMethod)) { |
53
|
|
|
throw new ApiControllerException('API method not found ' . $method); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (\count($params) > 0) { |
57
|
|
|
$result = $this->$execMethod($this->dataProvider, ...$params); |
58
|
|
|
} else { |
59
|
|
|
$result = $this->$execMethod($this->dataProvider); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $result; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \Xervice\DataProvider\DataProvider\AbstractDataProvider $dataProvider |
67
|
|
|
* @param int $statusCode |
68
|
|
|
* |
69
|
|
|
* @return \Xervice\Service\Application\Response\ApiResponse |
70
|
|
|
*/ |
71
|
|
|
public function jsonResponse(AbstractDataProvider $dataProvider, int $statusCode = 200): ApiResponse |
72
|
|
|
{ |
73
|
|
|
$response = new ApiResponse(); |
74
|
|
|
$response->setStatusCode($statusCode); |
75
|
|
|
$response->setDataProvider($dataProvider); |
76
|
|
|
return $response; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $string |
81
|
|
|
* @param bool $capitalizeFirstCharacter |
82
|
|
|
* |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
private function dashesToCamelCase($string, $capitalizeFirstCharacter = false) |
86
|
|
|
{ |
87
|
|
|
$str = str_replace(' ', '', ucwords(str_replace('-', ' ', $string))); |
88
|
|
|
|
89
|
|
|
if (!$capitalizeFirstCharacter) { |
90
|
|
|
$str[0] = strtolower($str[0]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $str; |
94
|
|
|
} |
95
|
|
|
} |