Security update for Core, with self-updated composer
[yaffs-website] / vendor / zendframework / zend-diactoros / src / PhpInputStream.php
1 <?php
2 /**
3  * @see       https://github.com/zendframework/zend-diactoros for the canonical source repository
4  * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
5  * @license   https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
6  */
7
8 namespace Zend\Diactoros;
9
10 /**
11  * Caching version of php://input
12  */
13 class PhpInputStream extends Stream
14 {
15     /**
16      * @var string
17      */
18     private $cache = '';
19
20     /**
21      * @var bool
22      */
23     private $reachedEof = false;
24
25     /**
26      * @param  string|resource $stream
27      */
28     public function __construct($stream = 'php://input')
29     {
30         parent::__construct($stream, 'r');
31     }
32
33     /**
34      * {@inheritdoc}
35      */
36     public function __toString()
37     {
38         if ($this->reachedEof) {
39             return $this->cache;
40         }
41
42         $this->getContents();
43         return $this->cache;
44     }
45
46     /**
47      * {@inheritdoc}
48      */
49     public function isWritable()
50     {
51         return false;
52     }
53
54     /**
55      * {@inheritdoc}
56      */
57     public function read($length)
58     {
59         $content = parent::read($length);
60         if (! $this->reachedEof) {
61             $this->cache .= $content;
62         }
63
64         if ($this->eof()) {
65             $this->reachedEof = true;
66         }
67
68         return $content;
69     }
70
71     /**
72      * {@inheritdoc}
73      */
74     public function getContents($maxLength = -1)
75     {
76         if ($this->reachedEof) {
77             return $this->cache;
78         }
79
80         $contents     = stream_get_contents($this->resource, $maxLength);
81         $this->cache .= $contents;
82
83         if ($maxLength === -1 || $this->eof()) {
84             $this->reachedEof = true;
85         }
86
87         return $contents;
88     }
89 }