1
+ <?php
2
+ /**
3
+ * This file contains the RequestBodyElement
4
+ *
5
+ * @package PHPDraft\Model
6
+ * @author Sean Molenaar<[email protected] >
7
+ */
8
+
9
+ namespace PHPDraft \Model ;
10
+
11
+
12
+ class RequestBodyElement extends DataStructureElement
13
+ {
14
+ /**
15
+ * Parse a JSON object to a data structure
16
+ *
17
+ * @param \stdClass $object An object to parse
18
+ * @param array $dependencies Dependencies of this object
19
+ *
20
+ * @return DataStructureElement self reference
21
+ */
22
+ public function parse ($ object , &$ dependencies )
23
+ {
24
+ if (empty ($ object ) || !isset ($ object ->content ))
25
+ {
26
+ return $ this ;
27
+ }
28
+ $ this ->element = $ object ->element ;
29
+ if (isset ($ object ->content ) && is_array ($ object ->content ))
30
+ {
31
+ foreach ($ object ->content as $ value )
32
+ {
33
+ $ struct = new RequestBodyElement ();
34
+ $ this ->value [] = $ struct ->parse ($ value , $ dependencies );
35
+ }
36
+
37
+ return $ this ;
38
+ }
39
+
40
+ $ this ->key = $ object ->content ->key ->content ;
41
+ $ this ->type = $ object ->content ->value ->element ;
42
+ $ this ->description = isset ($ object ->meta ->description ) ? $ object ->meta ->description : NULL ;
43
+ $ this ->status =
44
+ isset ($ object ->attributes ->typeAttributes [0 ]) ? $ object ->attributes ->typeAttributes [0 ] : NULL ;
45
+
46
+ if (!in_array ($ this ->type , $ this ->defaults ))
47
+ {
48
+ $ dependencies [] = $ this ->type ;
49
+ }
50
+
51
+ if ($ this ->type === 'object ' )
52
+ {
53
+ $ value = isset ($ object ->content ->value ->content ) ? $ object ->content ->value : NULL ;
54
+ $ this ->value = new RequestBodyElement ();
55
+ $ this ->value = $ this ->value ->parse ($ value , $ dependencies );
56
+
57
+ return $ this ;
58
+ }
59
+
60
+ $ this ->value = isset ($ object ->content ->value ->content ) ? $ object ->content ->value ->content : NULL ;
61
+
62
+ return $ this ;
63
+ }
64
+
65
+ public function print_request ($ type = 'application/x-www-form-urlencoded ' )
66
+ {
67
+ if (is_array ($ this ->value ))
68
+ {
69
+ $ return = '<code> ' ;
70
+ foreach ($ this ->value as $ object )
71
+ {
72
+ if (get_class ($ object ) === self ::class)
73
+ {
74
+ $ return .= $ object ->print_request ($ type );
75
+ }
76
+ }
77
+
78
+ $ return .= '</code> ' ;
79
+
80
+ return $ return ;
81
+ }
82
+
83
+ $ value = (empty ($ this ->value )) ? '? ' : $ this ->value ;
84
+
85
+ switch ($ type )
86
+ {
87
+ case 'application/x-www-form-urlencoded ' :
88
+ return $ this ->key . "=<kbd> " . $ value . '</kbd> ' ;
89
+ break ;
90
+ default :
91
+ $ object = [];
92
+ $ object [$ this ->key ] = $ value ;
93
+
94
+ return json_encode ($ object ) . PHP_EOL ;
95
+ break ;
96
+ }
97
+ }
98
+ }
0 commit comments