1
|
|
|
<?php namespace CalDAVClient\Facade\Utils; |
2
|
|
|
/** |
3
|
|
|
* Copyright 2017 OpenStack Foundation |
4
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
5
|
|
|
* you may not use this file except in compliance with the License. |
6
|
|
|
* You may obtain a copy of the License at |
7
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
8
|
|
|
* Unless required by applicable law or agreed to in writing, software |
9
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
10
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
11
|
|
|
* See the License for the specific language governing permissions and |
12
|
|
|
* limitations under the License. |
13
|
|
|
**/ |
14
|
|
|
|
15
|
|
|
use GuzzleHttp\Psr7\Request; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class RequestFactory |
19
|
|
|
* @package CalDAVClient\Facade\Utils |
20
|
|
|
*/ |
21
|
|
|
final class RequestFactory |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $http_method |
26
|
|
|
* @param array $params |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
private static function createHeadersFor($http_method, array $params = []){ |
30
|
|
|
switch ($http_method){ |
31
|
|
|
case HttpMethods::PropFind: |
32
|
|
|
case HttpMethods::Options: |
33
|
|
|
case HttpMethods::Report: |
34
|
|
|
return [ |
35
|
|
|
Headers::Depth => $params[0], |
36
|
|
|
Headers::Prefer => "return-minimal", |
37
|
|
|
Headers::ContentType => ContentTypes::Xml |
38
|
|
|
]; |
39
|
|
|
case HttpMethods::Delete: |
40
|
|
|
$etag = $params[0]; |
41
|
|
|
if(!empty($etag)) { |
42
|
|
|
return [ |
43
|
|
|
Headers::IfMatch => $etag, |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
return []; |
47
|
|
|
case HttpMethods::MakeCalendar: |
48
|
|
|
return [ |
49
|
|
|
Headers::ContentType => ContentTypes::Xml |
50
|
|
|
]; |
51
|
|
|
case HttpMethods::Put: |
52
|
|
|
|
53
|
|
|
$len = $params[0]; |
54
|
|
|
$etag = $params[1]; |
55
|
|
|
|
56
|
|
|
$headers = [ |
57
|
|
|
Headers::ContentLength => intval($len), |
58
|
|
|
Headers::ContentType => ContentTypes::Calendar, |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
if(!empty($etag)){ |
62
|
|
|
$headers[Headers::IfMatch] = $etag; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $headers; |
66
|
|
|
} |
67
|
|
|
return []; |
68
|
|
|
} |
69
|
|
|
/** |
70
|
|
|
* @param string $url |
71
|
|
|
* @param string $body |
72
|
|
|
* @param int $depth |
73
|
|
|
* @return Request |
74
|
|
|
*/ |
75
|
|
|
public static function createPropFindRequest($url , $body, $depth = 1){ |
76
|
|
|
return new Request |
77
|
|
|
( |
78
|
|
|
HttpMethods::PropFind, |
79
|
|
|
$url , |
80
|
|
|
self::createHeadersFor(HttpMethods::PropFind, [$depth]), |
81
|
|
|
$body |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $url |
87
|
|
|
* @param string $body |
88
|
|
|
* @return Request |
89
|
|
|
*/ |
90
|
|
|
public static function createMakeCalendarRequest($url , $body){ |
91
|
|
|
return new Request |
92
|
|
|
( |
93
|
|
|
HttpMethods::MakeCalendar, |
94
|
|
|
$url, |
95
|
|
|
self::createHeadersFor(HttpMethods::MakeCalendar), |
96
|
|
|
$body |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $url |
102
|
|
|
* @param int $depth |
103
|
|
|
* @return Request |
104
|
|
|
*/ |
105
|
|
|
public static function createOptionsRequest($url, $depth = 1){ |
106
|
|
|
return new Request |
107
|
|
|
( |
108
|
|
|
HttpMethods::Options, |
109
|
|
|
$url, |
110
|
|
|
self::createHeadersFor(HttpMethods::Options, [$depth]) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $url |
116
|
|
|
* @param string $body |
117
|
|
|
* @param int $depth |
118
|
|
|
* @return Request |
119
|
|
|
*/ |
120
|
|
|
public static function createReportRequest($url , $body, $depth = 1){ |
121
|
|
|
return new Request |
122
|
|
|
( |
123
|
|
|
HttpMethods::Report, |
124
|
|
|
$url, |
125
|
|
|
self::createHeadersFor(HttpMethods::Report, [$depth]), |
126
|
|
|
$body |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $url |
132
|
|
|
* @param string $etag |
133
|
|
|
* @return Request |
134
|
|
|
*/ |
135
|
|
|
public static function createDeleteRequest($url , $etag){ |
136
|
|
|
return new Request |
137
|
|
|
( |
138
|
|
|
HttpMethods::Delete, |
139
|
|
|
$url, |
140
|
|
|
self::createHeadersFor(HttpMethods::Delete, [$etag]) |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $url |
146
|
|
|
* @return Request |
147
|
|
|
*/ |
148
|
|
|
public static function createGetRequest($url){ |
149
|
|
|
return new Request |
150
|
|
|
( |
151
|
|
|
HttpMethods::Get, |
152
|
|
|
$url, |
153
|
|
|
self::createHeadersFor(HttpMethods::Get) |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $url |
159
|
|
|
* @param string $body |
160
|
|
|
* @param string $etag |
161
|
|
|
* @return Request |
162
|
|
|
*/ |
163
|
|
|
public static function createPutRequest($url, $body, $etag = null){ |
164
|
|
|
|
165
|
|
|
return new Request |
166
|
|
|
( |
167
|
|
|
HttpMethods::Put, |
168
|
|
|
$url, |
169
|
|
|
self::createHeadersFor(HttpMethods::Put, [strlen($body), $etag]), |
170
|
|
|
$body |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $url |
176
|
|
|
* @param string $body |
177
|
|
|
* @param string $etag |
178
|
|
|
* @return Request |
179
|
|
|
*/ |
180
|
|
|
public static function createPostRequest($url, $body, $etag = null){ |
181
|
|
|
return new Request |
182
|
|
|
( |
183
|
|
|
HttpMethods::Post, |
184
|
|
|
$url, |
185
|
|
|
self::createHeadersFor(HttpMethods::Post, [$etag]), |
186
|
|
|
$body |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
} |