1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Mikemirten\Bundle\JsonApiBundle\Response; |
5
|
|
|
|
6
|
|
|
use Mikemirten\Bundle\JsonApiBundle\Response\Behaviour\HttpAttributesAwareInterface; |
7
|
|
|
use Mikemirten\Bundle\JsonApiBundle\Response\Behaviour\HttpAttributesContainer; |
8
|
|
|
use Mikemirten\Bundle\JsonApiBundle\Response\Behaviour\IncludedObjectsAwareInterface; |
9
|
|
|
use Mikemirten\Bundle\JsonApiBundle\Response\Behaviour\IncludedObjectsContainer; |
10
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Link; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Abstract Json API view supposed to get handled and converted into a response |
14
|
|
|
* |
15
|
|
|
* @package Mikemirten\Bundle\JsonApiBundle\Response |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractJsonApiView implements HttpAttributesAwareInterface, IncludedObjectsAwareInterface |
18
|
|
|
{ |
19
|
|
|
use HttpAttributesContainer; |
20
|
|
|
use IncludedObjectsContainer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Callback to call after a resource-object has created. |
24
|
|
|
* |
25
|
|
|
* @var callable |
26
|
|
|
*/ |
27
|
|
|
protected $resourceCallback; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Callback to call after a document has created. |
31
|
|
|
* |
32
|
|
|
* @var callable |
33
|
|
|
*/ |
34
|
|
|
protected $documentCallback; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Definitions of links have to be added to document |
38
|
|
|
* |
39
|
|
|
* @var Link[] |
40
|
|
|
*/ |
41
|
|
|
protected $documentLinks; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set a callback to call after a resource-object has created. |
45
|
|
|
* |
46
|
|
|
* @param callable $callback |
47
|
|
|
*/ |
48
|
|
|
public function setResourceCallback(callable $callback) |
49
|
|
|
{ |
50
|
|
|
$this->resourceCallback = $callback; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Has a callback to call after a resource-object has created ? |
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function hasResourceCallback(): bool |
59
|
|
|
{ |
60
|
|
|
return $this->resourceCallback !== null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get a callback to call after a resource-object has created. |
65
|
|
|
* |
66
|
|
|
* @return callable |
67
|
|
|
*/ |
68
|
|
|
public function getResourceCallback(): callable |
69
|
|
|
{ |
70
|
|
|
return $this->resourceCallback; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set a callback to call after a document has created |
75
|
|
|
* |
76
|
|
|
* @param callable $callback |
77
|
|
|
*/ |
78
|
|
|
public function setDocumentCallback(callable $callback) |
79
|
|
|
{ |
80
|
|
|
$this->documentCallback = $callback; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Has a callback to call after a document has created |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public function hasDocumentCallback(): bool |
89
|
|
|
{ |
90
|
|
|
return $this->documentCallback !== null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get a callback to call after a document has created |
95
|
|
|
* |
96
|
|
|
* @return callable |
97
|
|
|
*/ |
98
|
|
|
public function getDocumentCallback(): callable |
99
|
|
|
{ |
100
|
|
|
return $this->documentCallback; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Add a definition of link have to be added to document |
105
|
|
|
* |
106
|
|
|
* @param Link $link |
107
|
|
|
*/ |
108
|
2 |
|
public function addDocumentLink(Link $link) |
109
|
|
|
{ |
110
|
2 |
|
$name = $link->getName(); |
111
|
|
|
|
112
|
2 |
|
if (isset($this->documentLinks[$name])) { |
113
|
|
|
throw new \LogicException(sprintf('Link "%s" already added to document.')); |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
$this->documentLinks[$name] = $link; |
117
|
2 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get definitions of links have to be added to document |
121
|
|
|
* |
122
|
|
|
* @return Link[] |
123
|
|
|
*/ |
124
|
2 |
|
public function getDocumentLinks(): array |
125
|
|
|
{ |
126
|
2 |
|
return $this->documentLinks; |
127
|
|
|
} |
128
|
|
|
} |