[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/vendor/guzzlehttp/guzzle/src/Post/ -> PostBody.php (source)

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


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1