Api
3 years ago
Connection
3 years ago
Constants
7 years ago
Exception
3 years ago
HttpClient
3 years ago
Hydrator
3 years ago
Lists
7 years ago
Message
7 years ago
Messages
7 years ago
Model
7 years ago
Assert.php
7 years ago
Exception.php
7 years ago
HttpClientConfigurator.php
3 years ago
Mailgun.php
3 years ago
RequestBuilder.php
3 years ago
RequestBuilder.php
122 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * Copyright (C) 2013 Mailgun |
| 5 | * |
| 6 | * This software may be modified and distributed under the terms |
| 7 | * of the MIT license. See the LICENSE file for details. |
| 8 | */ |
| 9 | |
| 10 | namespace Mailgun; |
| 11 | |
| 12 | use AmeliaHttp\Discovery\MessageFactoryDiscovery; |
| 13 | use AmeliaHttp\Message\MultipartStream\MultipartStreamBuilder; |
| 14 | use AmeliaHttp\Message\RequestFactory; |
| 15 | use AmeliaPsr\Http\Message\RequestInterface; |
| 16 | |
| 17 | /** |
| 18 | * @author Tobias Nyholm <tobias.nyholm@gmail.com> |
| 19 | */ |
| 20 | class RequestBuilder |
| 21 | { |
| 22 | /** |
| 23 | * @var RequestFactory |
| 24 | */ |
| 25 | private $requestFactory; |
| 26 | |
| 27 | /** |
| 28 | * @var MultipartStreamBuilder |
| 29 | */ |
| 30 | private $multipartStreamBuilder; |
| 31 | |
| 32 | /** |
| 33 | * Creates a new PSR-7 request. |
| 34 | * |
| 35 | * @param string $method |
| 36 | * @param string $uri |
| 37 | * @param array $headers |
| 38 | * @param array|string|null $body Request body. If body is an array we will send a as multipart stream request. |
| 39 | * If array, each array *item* MUST look like: |
| 40 | * array ( |
| 41 | * 'content' => string|resource|StreamInterface, |
| 42 | * 'name' => string, |
| 43 | * 'filename'=> string (optional) |
| 44 | * 'headers' => array (optinal) ['header-name' => 'header-value'] |
| 45 | * ) |
| 46 | * |
| 47 | * @return RequestInterface |
| 48 | */ |
| 49 | public function create($method, $uri, array $headers = [], $body = null) |
| 50 | { |
| 51 | if (!is_array($body)) { |
| 52 | return $this->getRequestFactory()->createRequest($method, $uri, $headers, $body); |
| 53 | } |
| 54 | |
| 55 | $builder = $this->getMultipartStreamBuilder(); |
| 56 | foreach ($body as $item) { |
| 57 | $name = $item['name']; |
| 58 | $content = $item['content']; |
| 59 | unset($item['name']); |
| 60 | unset($item['content']); |
| 61 | |
| 62 | $builder->addResource($name, $content, $item); |
| 63 | } |
| 64 | |
| 65 | $multipartStream = $builder->build(); |
| 66 | $boundary = $builder->getBoundary(); |
| 67 | $builder->reset(); |
| 68 | |
| 69 | $headers['Content-Type'] = 'multipart/form-data; boundary="'.$boundary.'"'; |
| 70 | |
| 71 | return $this->getRequestFactory()->createRequest($method, $uri, $headers, $multipartStream); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return RequestFactory |
| 76 | */ |
| 77 | private function getRequestFactory() |
| 78 | { |
| 79 | if (null === $this->requestFactory) { |
| 80 | $this->requestFactory = MessageFactoryDiscovery::find(); |
| 81 | } |
| 82 | |
| 83 | return $this->requestFactory; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param RequestFactory $requestFactory |
| 88 | * |
| 89 | * @return RequestBuilder |
| 90 | */ |
| 91 | public function setRequestFactory($requestFactory) |
| 92 | { |
| 93 | $this->requestFactory = $requestFactory; |
| 94 | |
| 95 | return $this; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @return MultipartStreamBuilder |
| 100 | */ |
| 101 | private function getMultipartStreamBuilder() |
| 102 | { |
| 103 | if (null === $this->multipartStreamBuilder) { |
| 104 | $this->multipartStreamBuilder = new MultipartStreamBuilder(); |
| 105 | } |
| 106 | |
| 107 | return $this->multipartStreamBuilder; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param MultipartStreamBuilder $multipartStreamBuilder |
| 112 | * |
| 113 | * @return RequestBuilder |
| 114 | */ |
| 115 | public function setMultipartStreamBuilder($multipartStreamBuilder) |
| 116 | { |
| 117 | $this->multipartStreamBuilder = $multipartStreamBuilder; |
| 118 | |
| 119 | return $this; |
| 120 | } |
| 121 | } |
| 122 |