/**
  * Function always use provided $filename. In cases when it's empty, for string content or when name cannot be
  * automatically discovered the $filename will be set to attachment name.
  * If attachment name is not provided, it will be randomly generated.
  * @param resource|string|StreamInterface $content  StreamInterface/resource/string to send
  * @param string                          $filename Optional. Filename of attachment, can't be empty if content is string
  * @param array                           $headers  Optional. Associative array of custom headers
  * @param string                          $name     Optional. Form field name
  * @return $this
  */
 public function addAttachment($content, $filename = '', array $headers = array(), $name = '')
 {
     $uri = null;
     if (!empty($filename)) {
         $uri = $filename;
     } elseif ($content instanceof StreamInterface) {
         $meta = $content->getMetadata('uri');
         if (substr($meta, 0, 6) !== 'php://') {
             $uri = $meta;
         }
     } elseif (is_resource($content)) {
         $meta = stream_get_meta_data($content);
         $uri = $meta['uri'];
     }
     $basename = basename($uri);
     if (empty($basename)) {
         throw new \InvalidArgumentException('File name was not provided and cannot be auto-discovered');
     }
     $name = !empty($name) ? $name : $basename;
     $element = array('contents' => $content, 'name' => $name);
     // always set as defined or else it will be auto-discovered by Guzzle
     if (!empty($filename)) {
         $element['filename'] = $filename;
     }
     if (!empty($headers)) {
         $element['headers'] = $headers;
     }
     $contentKey = null;
     foreach ($headers as $k => $v) {
         if (strtolower($k) == 'content-type') {
             $contentKey = $k;
         }
     }
     if (empty($contentKey)) {
         if (is_string($content)) {
             // Automatically set
             $element['headers']['Content-Type'] = 'application/octet-stream';
         } elseif ($content instanceof StreamInterface) {
             $type = \GuzzleHttp\Psr7\mimetype_from_filename($basename);
             if (!$type) {
                 throw new \InvalidArgumentException('Content-Type header was not provided and cannot be auto-discovered');
             }
         }
     }
     $this->elements[] = $element;
     return $this;
 }
Example #2
0
 public function testGetMetadata()
 {
     $this->assertTrue(is_array($this->stream->getMetadata()));
     $this->assertNull($this->stream->getMetadata('unknown.........'));
 }
 /**
  * {@inheritdoc}
  */
 public function getMetadata($key = null)
 {
     return $this->stream->getMetadata($key);
 }
 /**
  * {@inheritdoc}
  */
 public function getMetadata($key = null)
 {
     return $this->decoratedStream->getMetadata($key);
 }
 public function testWrapsMetadata()
 {
     $this->assertSame($this->b->getMetadata(), $this->a->getMetadata());
     $this->assertSame($this->b->getMetadata('uri'), $this->a->getMetadata('uri'));
 }