|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\tools\generator\responder; |
|
3
|
|
|
|
|
4
|
|
|
use gossi\codegen\model\AbstractPhpStruct; |
|
5
|
|
|
use keeko\framework\schema\ActionSchema; |
|
6
|
|
|
use gossi\codegen\model\PhpClass; |
|
7
|
|
|
use gossi\codegen\model\PhpMethod; |
|
8
|
|
|
use gossi\codegen\model\PhpParameter; |
|
9
|
|
|
|
|
10
|
|
|
trait PayloadGeneratorTrait { |
|
11
|
|
|
|
|
12
|
|
|
protected function ensureUseStatements(AbstractPhpStruct $struct) { |
|
13
|
|
|
parent::ensureUseStatements($struct); |
|
14
|
|
|
$struct->removeUseStatement('keeko\\framework\\domain\\payload\\PayloadInterface'); |
|
15
|
|
|
$struct->removeUseStatement('keeko\\framework\\foundation\\AbstractResponder'); |
|
16
|
|
|
$struct->addUseStatement('keeko\\framework\\foundation\\AbstractPayloadResponder'); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
protected function generateStruct(ActionSchema $action, $format) { |
|
|
|
|
|
|
20
|
|
|
$class = parent::generateStruct($action, $format); |
|
21
|
|
|
$class->setParentClassName('AbstractPayloadResponder'); |
|
22
|
|
|
|
|
23
|
|
|
return $class; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
protected function generateGetPayloadMethods(PhpClass $class, $body = '') { |
|
27
|
|
|
$class->setMethod(PhpMethod::create('getPayloadMethods') |
|
28
|
|
|
->setVisibility(PhpMethod::VISIBILITY_PROTECTED) |
|
29
|
|
|
->setBody($body) |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function generatePayloadMethod($name, $body, $type = 'PayloadInterface') { |
|
34
|
|
|
return PhpMethod::create($name) |
|
35
|
|
|
->addParameter(PhpParameter::create('request') |
|
36
|
|
|
->setType('Request') |
|
37
|
|
|
) |
|
38
|
|
|
->addParameter(PhpParameter::create('payload') |
|
39
|
|
|
->setType($type) |
|
40
|
|
|
) |
|
41
|
|
|
->setBody($body) |
|
42
|
|
|
; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function generateNotValid(PhpClass $class) { |
|
46
|
|
|
$class->addUseStatement('keeko\\framework\\domain\\payload\\NotValid'); |
|
47
|
|
|
$class->addUseStatement('keeko\framework\exceptions\ValidationException'); |
|
48
|
|
|
$notValid = $this->generatePayloadMethod('notValid', $this->twig->render('payload/notValid.twig'), |
|
|
|
|
|
|
49
|
|
|
'NotValid'); |
|
50
|
|
|
$class->setMethod($notValid); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function generateNotFound(PhpClass $class) { |
|
54
|
|
|
$class->addUseStatement('keeko\\framework\\domain\\payload\\NotFound'); |
|
55
|
|
|
$class->addUseStatement('Symfony\Component\Routing\Exception\ResourceNotFoundException'); |
|
56
|
|
|
$notFound = $this->generatePayloadMethod('notFound', $this->twig->render('payload/notFound.twig'), |
|
57
|
|
|
'NotFound'); |
|
58
|
|
|
$class->setMethod($notFound); |
|
59
|
|
|
} |
|
60
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.