Open Journal Systems  3.3.0
MultipartStream.php
1 <?php
2 namespace GuzzleHttp\Psr7;
3 
5 
11 {
12  use StreamDecoratorTrait;
13 
14  private $boundary;
15 
28  public function __construct(array $elements = [], $boundary = null)
29  {
30  $this->boundary = $boundary ?: sha1(uniqid('', true));
31  $this->stream = $this->createStream($elements);
32  }
33 
39  public function getBoundary()
40  {
41  return $this->boundary;
42  }
43 
44  public function isWritable()
45  {
46  return false;
47  }
48 
52  private function getHeaders(array $headers)
53  {
54  $str = '';
55  foreach ($headers as $key => $value) {
56  $str .= "{$key}: {$value}\r\n";
57  }
58 
59  return "--{$this->boundary}\r\n" . trim($str) . "\r\n\r\n";
60  }
61 
65  protected function createStream(array $elements)
66  {
67  $stream = new AppendStream();
68 
69  foreach ($elements as $element) {
70  $this->addElement($stream, $element);
71  }
72 
73  // Add the trailing boundary with CRLF
74  $stream->addStream(stream_for("--{$this->boundary}--\r\n"));
75 
76  return $stream;
77  }
78 
79  private function addElement(AppendStream $stream, array $element)
80  {
81  foreach (['contents', 'name'] as $key) {
82  if (!array_key_exists($key, $element)) {
83  throw new \InvalidArgumentException("A '{$key}' key is required");
84  }
85  }
86 
87  $element['contents'] = stream_for($element['contents']);
88 
89  if (empty($element['filename'])) {
90  $uri = $element['contents']->getMetadata('uri');
91  if (substr($uri, 0, 6) !== 'php://') {
92  $element['filename'] = $uri;
93  }
94  }
95 
96  list($body, $headers) = $this->createElement(
97  $element['name'],
98  $element['contents'],
99  isset($element['filename']) ? $element['filename'] : null,
100  isset($element['headers']) ? $element['headers'] : []
101  );
102 
103  $stream->addStream(stream_for($this->getHeaders($headers)));
104  $stream->addStream($body);
105  $stream->addStream(stream_for("\r\n"));
106  }
107 
111  private function createElement($name, StreamInterface $stream, $filename, array $headers)
112  {
113  // Set a default content-disposition header if one was no provided
114  $disposition = $this->getHeader($headers, 'content-disposition');
115  if (!$disposition) {
116  $headers['Content-Disposition'] = ($filename === '0' || $filename)
117  ? sprintf('form-data; name="%s"; filename="%s"',
118  $name,
119  basename($filename))
120  : "form-data; name=\"{$name}\"";
121  }
122 
123  // Set a default content-length header if one was no provided
124  $length = $this->getHeader($headers, 'content-length');
125  if (!$length) {
126  if ($length = $stream->getSize()) {
127  $headers['Content-Length'] = (string) $length;
128  }
129  }
130 
131  // Set a default Content-Type if one was not supplied
132  $type = $this->getHeader($headers, 'content-type');
133  if (!$type && ($filename === '0' || $filename)) {
134  if ($type = mimetype_from_filename($filename)) {
135  $headers['Content-Type'] = $type;
136  }
137  }
138 
139  return [$stream, $headers];
140  }
141 
142  private function getHeader(array $headers, $key)
143  {
144  $lowercaseHeader = strtolower($key);
145  foreach ($headers as $k => $v) {
146  if (strtolower($k) === $lowercaseHeader) {
147  return $v;
148  }
149  }
150 
151  return null;
152  }
153 }
GuzzleHttp\Psr7\MultipartStream\createStream
createStream(array $elements)
Definition: MultipartStream.php:65
GuzzleHttp\Psr7\MultipartStream\isWritable
isWritable()
Definition: MultipartStream.php:44
Psr\Http\Message\StreamInterface
Definition: vendor/psr/http-message/src/StreamInterface.php:12
GuzzleHttp\Psr7\MultipartStream\__construct
__construct(array $elements=[], $boundary=null)
Definition: MultipartStream.php:28
GuzzleHttp\Psr7\mimetype_from_filename
mimetype_from_filename($filename)
Definition: guzzlehttp/psr7/src/functions.php:624
GuzzleHttp\Psr7
Definition: AppendStream.php:2
GuzzleHttp\Psr7\MultipartStream\getBoundary
getBoundary()
Definition: MultipartStream.php:39
GuzzleHttp\Psr7\AppendStream
Definition: AppendStream.php:11
GuzzleHttp\Psr7\MultipartStream
Definition: MultipartStream.php:10
GuzzleHttp\Psr7\stream_for
stream_for($resource='', array $options=[])
Definition: guzzlehttp/psr7/src/functions.php:78