Chris@0: boundary = $boundary ?: sha1(uniqid('', true)); Chris@0: $this->stream = $this->createStream($elements); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the boundary Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getBoundary() Chris@0: { Chris@0: return $this->boundary; Chris@0: } Chris@0: Chris@0: public function isWritable() Chris@0: { Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the headers needed before transferring the content of a POST file Chris@0: */ Chris@0: private function getHeaders(array $headers) Chris@0: { Chris@0: $str = ''; Chris@0: foreach ($headers as $key => $value) { Chris@0: $str .= "{$key}: {$value}\r\n"; Chris@0: } Chris@0: Chris@0: return "--{$this->boundary}\r\n" . trim($str) . "\r\n\r\n"; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Create the aggregate stream that will be used to upload the POST data Chris@0: */ Chris@0: protected function createStream(array $elements) Chris@0: { Chris@0: $stream = new AppendStream(); Chris@0: Chris@0: foreach ($elements as $element) { Chris@0: $this->addElement($stream, $element); Chris@0: } Chris@0: Chris@0: // Add the trailing boundary with CRLF Chris@0: $stream->addStream(stream_for("--{$this->boundary}--\r\n")); Chris@0: Chris@0: return $stream; Chris@0: } Chris@0: Chris@0: private function addElement(AppendStream $stream, array $element) Chris@0: { Chris@0: foreach (['contents', 'name'] as $key) { Chris@0: if (!array_key_exists($key, $element)) { Chris@0: throw new \InvalidArgumentException("A '{$key}' key is required"); Chris@0: } Chris@0: } Chris@0: Chris@0: $element['contents'] = stream_for($element['contents']); Chris@0: Chris@0: if (empty($element['filename'])) { Chris@0: $uri = $element['contents']->getMetadata('uri'); Chris@0: if (substr($uri, 0, 6) !== 'php://') { Chris@0: $element['filename'] = $uri; Chris@0: } Chris@0: } Chris@0: Chris@0: list($body, $headers) = $this->createElement( Chris@0: $element['name'], Chris@0: $element['contents'], Chris@0: isset($element['filename']) ? $element['filename'] : null, Chris@0: isset($element['headers']) ? $element['headers'] : [] Chris@0: ); Chris@0: Chris@0: $stream->addStream(stream_for($this->getHeaders($headers))); Chris@0: $stream->addStream($body); Chris@0: $stream->addStream(stream_for("\r\n")); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return array Chris@0: */ Chris@0: private function createElement($name, StreamInterface $stream, $filename, array $headers) Chris@0: { Chris@0: // Set a default content-disposition header if one was no provided Chris@0: $disposition = $this->getHeader($headers, 'content-disposition'); Chris@0: if (!$disposition) { Chris@0: $headers['Content-Disposition'] = ($filename === '0' || $filename) Chris@0: ? sprintf('form-data; name="%s"; filename="%s"', Chris@0: $name, Chris@0: basename($filename)) Chris@0: : "form-data; name=\"{$name}\""; Chris@0: } Chris@0: Chris@0: // Set a default content-length header if one was no provided Chris@0: $length = $this->getHeader($headers, 'content-length'); Chris@0: if (!$length) { Chris@0: if ($length = $stream->getSize()) { Chris@0: $headers['Content-Length'] = (string) $length; Chris@0: } Chris@0: } Chris@0: Chris@0: // Set a default Content-Type if one was not supplied Chris@0: $type = $this->getHeader($headers, 'content-type'); Chris@0: if (!$type && ($filename === '0' || $filename)) { Chris@0: if ($type = mimetype_from_filename($filename)) { Chris@0: $headers['Content-Type'] = $type; Chris@0: } Chris@0: } Chris@0: Chris@0: return [$stream, $headers]; Chris@0: } Chris@0: Chris@0: private function getHeader(array $headers, $key) Chris@0: { Chris@0: $lowercaseHeader = strtolower($key); Chris@0: foreach ($headers as $k => $v) { Chris@0: if (strtolower($k) === $lowercaseHeader) { Chris@0: return $v; Chris@0: } Chris@0: } Chris@0: Chris@0: return null; Chris@0: } Chris@0: }