1
+ <?php
2
+
3
+ namespace Lomkit \Rest \Documentation \Schemas ;
4
+
5
+ use Lomkit \Rest \Http \Controllers \Controller ;
6
+
7
+ class MediaType extends Schema
8
+ {
9
+ protected SchemaConcrete $ schemaConcrete ;
10
+
11
+ protected Examples $ examples ;
12
+ protected Example $ example ;
13
+
14
+ public function withSchemaConcrete (SchemaConcrete $ schemaConcrete ): MediaType
15
+ {
16
+ $ this ->schemaConcrete = $ schemaConcrete ;
17
+ return $ this ;
18
+ }
19
+
20
+ public function schemaConcrete (): SchemaConcrete
21
+ {
22
+ return $ this ->schemaConcrete ;
23
+ }
24
+
25
+ public function withExamples (Examples $ examples ): MediaType
26
+ {
27
+ $ this ->examples = $ examples ;
28
+ return $ this ;
29
+ }
30
+
31
+ public function examples (): Examples
32
+ {
33
+ return $ this ->examples ;
34
+ }
35
+
36
+ public function withExample (Example $ example ): MediaType
37
+ {
38
+ $ this ->example = $ example ;
39
+ return $ this ;
40
+ }
41
+
42
+ public function example (): Example
43
+ {
44
+ return $ this ->example ;
45
+ }
46
+
47
+ public function generate (): MediaType
48
+ {
49
+ return $ this ;
50
+ }
51
+
52
+ public function jsonSerialize (): mixed
53
+ {
54
+ return array_merge (
55
+ isset ($ this ->schema ) ? ['schema ' => $ this ->schema ()->jsonSerialize ()] : [],
56
+ isset ($ this ->examples ) ? ['examples ' => $ this ->examples ()->jsonSerialize ()] : [],
57
+ isset ($ this ->example ) ? ['example ' => $ this ->example ()->jsonSerialize ()] : []
58
+ );
59
+ }
60
+
61
+ public function generateDetail (Controller $ controller ): MediaType
62
+ {
63
+ return $ this
64
+ ->withExample (
65
+ (new Example )
66
+ ->withValue (
67
+ ['data ' => $ controller ::newResource ()->jsonSerialize ()]
68
+ )
69
+ )
70
+ ->generate ();
71
+ }
72
+
73
+ public function generateSearch (Controller $ controller ): MediaType
74
+ {
75
+ return $ this
76
+ ->withExample (
77
+ (new Example )
78
+ ->withValue (
79
+ $ controller ::newResource ()::newResponse ()
80
+ ->resource ($ controller ::newResource ())
81
+ ->responsable (
82
+ $ controller ::newResource ()::newModel ()::factory ()->makeOne ()
83
+ ->withoutRelations ()
84
+ )
85
+ ->toResponse (request ())
86
+ )
87
+ )
88
+ ->generate ();
89
+ }
90
+
91
+ public function generateMutate (Controller $ controller ): MediaType
92
+ {
93
+ return $ this
94
+ ->withExample (
95
+ (new Example )
96
+ ->withValue (
97
+ ['created ' => [1 ], 'updated ' => [2 ,3 ]]
98
+ )
99
+ )
100
+ ->generate ();
101
+ }
102
+ }
0 commit comments