Hoa central
Read.php
Go to the documentation of this file.
1 <?php
2 
38 
39 use Hoa\File;
40 use Hoa\Stream;
41 
50 class Read extends Temporary implements Stream\IStream\In
51 {
62  public function __construct(
63  $streamName,
64  $mode = parent::MODE_READ,
65  $context = null,
66  $wait = false
67  ) {
68  parent::__construct($streamName, $mode, $context, $wait);
69 
70  return;
71  }
72 
82  protected function &_open($streamName, Stream\Context $context = null)
83  {
84  static $createModes = [
85  parent::MODE_READ
86  ];
87 
88  if (!in_array($this->getMode(), $createModes)) {
89  throw new File\Exception(
90  'Open mode are not supported; given %d. Only %s are supported.',
91  0,
92  [$this->getMode(), implode(', ', $createModes)]
93  );
94  }
95 
96  preg_match('#^(\w+)://#', $streamName, $match);
97 
98  if (((isset($match[1]) && $match[1] == 'file') || !isset($match[1])) &&
99  !file_exists($streamName)) {
101  'File %s does not exist.',
102  1,
103  $streamName
104  );
105  }
106 
107  $out = parent::_open($streamName, $context);
108 
109  return $out;
110  }
111 
117  public function eof()
118  {
119  return feof($this->getStream());
120  }
121 
129  public function read($length)
130  {
131  if (0 > $length) {
132  throw new File\Exception(
133  'Length must be greater than 0, given %d.',
134  2,
135  $length
136  );
137  }
138 
139  return fread($this->getStream(), $length);
140  }
141 
148  public function readString($length)
149  {
150  return $this->read($length);
151  }
152 
158  public function readCharacter()
159  {
160  return fgetc($this->getStream());
161  }
162 
168  public function readBoolean()
169  {
170  return (bool) $this->read(1);
171  }
172 
179  public function readInteger($length = 1)
180  {
181  return (int) $this->read($length);
182  }
183 
190  public function readFloat($length = 1)
191  {
192  return (float) $this->read($length);
193  }
194 
202  public function readArray($format = null)
203  {
204  return $this->scanf($format);
205  }
206 
212  public function readLine()
213  {
214  return fgets($this->getStream());
215  }
216 
223  public function readAll($offset = 0)
224  {
225  return stream_get_contents($this->getStream(), -1, $offset);
226  }
227 
234  public function scanf($format)
235  {
236  return fscanf($this->getStream(), $format);
237  }
238 }
read($length)
Definition: Read.php:128
& _open($streamName, Stream\Context $context=null)
Definition: Read.php:82
readInteger($length=1)
Definition: Read.php:179
scanf($format)
Definition: Read.php:233
readAll($offset=0)
Definition: Read.php:223
readFloat($length=1)
Definition: Read.php:190
__construct($streamName, $mode=parent::MODE_READ, $context=null, $wait=false)
Definition: Read.php:62
readArray($format=null)
Definition: Read.php:202
readString($length)
Definition: Read.php:148