1
+ <?php
2
+ /*
3
+ * File: Payload.php
4
+ * Category: -
5
+ * Author: MSG
6
+ * Created: 05.03.17 05:41
7
+ * Updated: -
8
+ *
9
+ * Description:
10
+ * -
11
+ */
12
+
13
+ namespace Webklex \GitHook \Payload ;
14
+
15
+ class Payload {
16
+
17
+ /**
18
+ * Payload holder
19
+ * @var array $aPayload
20
+ */
21
+ protected $ aPayload = [];
22
+
23
+ /**
24
+ * Map all required attributes
25
+ * @var array $map
26
+ */
27
+ protected $ map = [
28
+ 'commits ' => [[
29
+ 'id ' => null ,
30
+ 'timestamp ' => null ,
31
+ 'message ' => null ,
32
+ 'url ' => null ,
33
+ 'author ' => [
34
+ 'name ' => null ,
35
+ 'email ' => null ,
36
+ ],
37
+ ]],
38
+ 'repository ' => [
39
+ 'name ' => null ,
40
+ 'description ' => null ,
41
+ 'url ' => null ,
42
+ 'homepage ' => null ,
43
+ ],
44
+ 'ref ' => null ,
45
+ 'user_name ' => null ,
46
+ 'before ' => null ,
47
+ 'after ' => null ,
48
+ ];
49
+
50
+ /**
51
+ * Cast any mapped value to a given schema
52
+ * @var array $casts
53
+ */
54
+ protected $ casts = [];
55
+
56
+ /**
57
+ * Payload constructor.
58
+ */
59
+ public function __construct () {}
60
+
61
+ /**
62
+ * Parse a payload string into the service
63
+ * @param $payload
64
+ *
65
+ * @return $this
66
+ */
67
+ public function parsePayload ($ payload ){
68
+ if ($ this ->isJson ($ payload )){
69
+ $ this ->aPayload = $ this ->validate (json_decode ($ payload , true ), $ this ->map );
70
+ $ this ->castPayload ();
71
+ }
72
+
73
+ return $ this ;
74
+ }
75
+
76
+ /**
77
+ * Check if a given string is valid JSON
78
+ * @param $string string
79
+ * @return bool
80
+ */
81
+ public function isJson ($ string ) {
82
+ json_decode ($ string );
83
+ return (json_last_error () == JSON_ERROR_NONE );
84
+ }
85
+
86
+ /**
87
+ * Validate and map a given payload recursive
88
+ * @param array $aPayload
89
+ * @param array $aMap
90
+ *
91
+ * @return array
92
+ */
93
+ public function validate (array $ aPayload , array $ aMap ){
94
+ $ payload = [];
95
+ foreach ($ aMap as $ key => $ map ){
96
+ if ($ map === '' ){
97
+ $ payload [$ key ] = '' ;
98
+ }elseif (isset ($ aPayload [$ key ])){
99
+ if (is_array ($ aPayload [$ key ])){
100
+ $ payload [$ key ] = $ this ->validate ($ aPayload [$ key ], $ map );
101
+ }elseif ($ map != null ){
102
+ $ payload [$ key ] = $ this ->getPayloadValue ($ map , $ aPayload );
103
+ }else {
104
+ $ payload [$ key ] = $ aPayload [$ key ];
105
+ }
106
+ }elseif (is_array ($ map )){
107
+ $ payload [$ key ] = [];
108
+
109
+ foreach ($ map as $ k => $ m ){
110
+ if (is_array ($ m )){
111
+ $ payload [$ key ][$ k ] = $ this ->validate ($ aPayload , $ m );
112
+ }elseif ($ m != null ){
113
+ $ payload [$ key ][$ k ] = $ this ->getPayloadValue ($ m , $ aPayload );
114
+ }elseif ($ m === '' ){
115
+ $ payload [$ key ][$ k ] = '' ;
116
+ }else {
117
+ $ payload [$ key ][$ k ] = $ aPayload [$ key ][$ k ];
118
+ }
119
+ }
120
+ }elseif ($ map != null ){
121
+ $ payload [$ key ] = $ this ->getPayloadValue ($ map , $ aPayload );
122
+ }
123
+ }
124
+ return $ payload ;
125
+ }
126
+
127
+ /**
128
+ * Get a payload value from a given compact string index
129
+ * @param $key
130
+ * @param array $payload
131
+ *
132
+ * @return array|mixed
133
+ */
134
+ protected function getPayloadValue ($ key , array $ payload ){
135
+ $ keys = explode ('. ' , $ key );
136
+ foreach ($ keys as $ i => $ key ){
137
+ unset($ keys [$ i ]);
138
+ if (isset ($ payload [$ key ])){
139
+ $ payload = $ payload [$ key ];
140
+ }
141
+ }
142
+
143
+ return $ payload ;
144
+ }
145
+
146
+ /**
147
+ * Cast available casts on the current payload
148
+ */
149
+ protected function castPayload (){
150
+ foreach ($ this ->casts as $ string => $ cast ){
151
+ $ keys = explode ('. ' , $ string );
152
+
153
+ $ key = $ keys [0 ];
154
+ unset($ keys [0 ]);
155
+ sort ($ keys );
156
+
157
+ $ this ->aPayload = $ this ->replace ($ this ->aPayload , $ key , $ keys , $ cast );
158
+ }
159
+ }
160
+
161
+ /**
162
+ * Cast a specific value within the payload
163
+ * @param $arr array
164
+ * @param $key string
165
+ * @param $keys array
166
+ * @param $cast string
167
+ *
168
+ * @return mixed
169
+ */
170
+ protected function replace ($ arr , $ key , $ keys , $ cast ){
171
+ if (!empty ($ keys )){
172
+ $ kkey = $ keys [0 ];
173
+ unset($ keys [0 ]);
174
+ sort ($ keys );
175
+ $ arr [$ key ] = $ this ->replace ($ arr [$ key ], $ kkey , $ keys , $ cast );
176
+ }else {
177
+ $ arr [$ key ] = str_replace ('{$1} ' , $ arr [$ key ], $ cast );
178
+ }
179
+ return $ arr ;
180
+ }
181
+
182
+ /**
183
+ * Get the current payload
184
+ * @return \Illuminate\Support\Collection
185
+ */
186
+ public function getPayload (){
187
+ return collect ($ this ->aPayload );
188
+ }
189
+ }
0 commit comments