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