scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2 namespace GuzzleHttp\Post;
3  
4 use GuzzleHttp\Message\RequestInterface;
5 use GuzzleHttp\Query;
6 use GuzzleHttp\Stream\Stream;
7 use GuzzleHttp\Stream\StreamInterface;
8  
9 /**
10 * Holds POST fields and files and creates a streaming body when read methods
11 * are called on the object.
12 */
13 class PostBody implements PostBodyInterface
14 {
15 /** @var StreamInterface */
16 private $body;
17  
18 /** @var callable */
19 private $aggregator;
20  
21 private $fields = [];
22  
23 /** @var PostFileInterface[] */
24 private $files = [];
25 private $forceMultipart = false;
26  
27 /**
28 * Applies request headers to a request based on the POST state
29 *
30 * @param RequestInterface $request Request to update
31 */
32 public function applyRequestHeaders(RequestInterface $request)
33 {
34 if ($this->files || $this->forceMultipart) {
35 $request->setHeader(
36 'Content-Type',
37 'multipart/form-data; boundary=' . $this->getBody()->getBoundary()
38 );
39 } elseif ($this->fields) {
40 $request->setHeader('Content-Type', 'application/x-www-form-urlencoded');
41 }
42  
43 if ($size = $this->getSize()) {
44 $request->setHeader('Content-Length', $size);
45 }
46 }
47  
48 public function forceMultipartUpload($force)
49 {
50 $this->forceMultipart = $force;
51  
52 return $this;
53 }
54  
55 public function setAggregator(callable $aggregator)
56 {
57 $this->aggregator = $aggregator;
58 }
59  
60 public function setField($name, $value)
61 {
62 $this->fields[$name] = $value;
63 $this->mutate();
64  
65 return $this;
66 }
67  
68 public function replaceFields(array $fields)
69 {
70 $this->fields = $fields;
71 $this->mutate();
72  
73 return $this;
74 }
75  
76 public function getField($name)
77 {
78 return isset($this->fields[$name]) ? $this->fields[$name] : null;
79 }
80  
81 public function removeField($name)
82 {
83 unset($this->fields[$name]);
84 $this->mutate();
85  
86 return $this;
87 }
88  
89 public function getFields($asString = false)
90 {
91 if (!$asString) {
92 return $this->fields;
93 }
94  
95 return (string) (new Query($this->fields))
96 ->setEncodingType(Query::RFC1738)
97 ->setAggregator($this->getAggregator());
98 }
99  
100 public function hasField($name)
101 {
102 return isset($this->fields[$name]);
103 }
104  
105 public function getFile($name)
106 {
107 foreach ($this->files as $file) {
108 if ($file->getName() == $name) {
109 return $file;
110 }
111 }
112  
113 return null;
114 }
115  
116 public function getFiles()
117 {
118 return $this->files;
119 }
120  
121 public function addFile(PostFileInterface $file)
122 {
123 $this->files[] = $file;
124 $this->mutate();
125  
126 return $this;
127 }
128  
129 public function clearFiles()
130 {
131 $this->files = [];
132 $this->mutate();
133  
134 return $this;
135 }
136  
137 /**
138 * Returns the numbers of fields + files
139 *
140 * @return int
141 */
142 public function count()
143 {
144 return count($this->files) + count($this->fields);
145 }
146  
147 public function __toString()
148 {
149 return (string) $this->getBody();
150 }
151  
152 public function getContents($maxLength = -1)
153 {
154 return $this->getBody()->getContents();
155 }
156  
157 public function close()
158 {
159 return $this->body ? $this->body->close() : true;
160 }
161  
162 public function detach()
163 {
164 $this->body = null;
165 $this->fields = $this->files = [];
166  
167 return $this;
168 }
169  
170 public function eof()
171 {
172 return $this->getBody()->eof();
173 }
174  
175 public function tell()
176 {
177 return $this->body ? $this->body->tell() : 0;
178 }
179  
180 public function isSeekable()
181 {
182 return true;
183 }
184  
185 public function isReadable()
186 {
187 return true;
188 }
189  
190 public function isWritable()
191 {
192 return false;
193 }
194  
195 public function getSize()
196 {
197 return $this->getBody()->getSize();
198 }
199  
200 public function seek($offset, $whence = SEEK_SET)
201 {
202 return $this->getBody()->seek($offset, $whence);
203 }
204  
205 public function read($length)
206 {
207 return $this->getBody()->read($length);
208 }
209  
210 public function write($string)
211 {
212 return false;
213 }
214  
215 public function flush()
216 {
217 return false;
218 }
219  
220 /**
221 * Return a stream object that is built from the POST fields and files.
222 *
223 * If one has already been created, the previously created stream will be
224 * returned.
225 */
226 private function getBody()
227 {
228 if ($this->body) {
229 return $this->body;
230 } elseif ($this->files || $this->forceMultipart) {
231 return $this->body = $this->createMultipart();
232 } elseif ($this->fields) {
233 return $this->body = $this->createUrlEncoded();
234 } else {
235 return $this->body = Stream::factory();
236 }
237 }
238  
239 /**
240 * Get the aggregator used to join multi-valued field parameters
241 *
242 * @return callable
243 */
244 final protected function getAggregator()
245 {
246 if (!$this->aggregator) {
247 $this->aggregator = Query::phpAggregator();
248 }
249  
250 return $this->aggregator;
251 }
252  
253 /**
254 * Creates a multipart/form-data body stream
255 *
256 * @return MultipartBody
257 */
258 private function createMultipart()
259 {
260 // Flatten the nested query string values using the correct aggregator
261 return new MultipartBody(
262 call_user_func($this->getAggregator(), $this->fields),
263 $this->files
264 );
265 }
266  
267 /**
268 * Creates an application/x-www-form-urlencoded stream body
269 *
270 * @return StreamInterface
271 */
272 private function createUrlEncoded()
273 {
274 return Stream::factory($this->getFields(true));
275 }
276  
277 /**
278 * Get rid of any cached data
279 */
280 private function mutate()
281 {
282 $this->body = null;
283 }
284 }