<?php
namespace Psy\Readline\Hoa;
class FileLinkRead extends FileLink implements StreamIn
{
public function __construct(
string $streamName,
string $mode = parent::MODE_READ,
?string $context = null,
bool $wait = false
) {
parent::__construct($streamName, $mode, $context, $wait);
return;
}
protected function &_open(string $streamName, ?StreamContext $context = null)
{
static $createModes = [
parent::MODE_READ,
];
if (!\in_array($this->getMode(), $createModes)) {
throw new FileException('Open mode are not supported; given %d. Only %s are supported.', 0, [$this->getMode(), \implode(', ', $createModes)]);
}
\preg_match('#^(\w+)://#', $streamName, $match);
if (((isset($match[1]) && $match[1] === 'file') || !isset($match[1])) &&
!\file_exists($streamName)) {
throw new FileDoesNotExistException('File %s does not exist.', 1, $streamName);
}
$out = parent::_open($streamName, $context);
return $out;
}
public function eof(): bool
{
return \feof($this->getStream());
}
public function read(int $length)
{
if (0 > $length) {
throw new FileException('Length must be greater than 0, given %d.', 2, $length);
}
return \fread($this->getStream(), $length);
}
public function readString(int $length)
{
return $this->read($length);
}
public function readCharacter()
{
return \fgetc($this->getStream());
}
public function readBoolean()
{
return (bool) $this->read(1);
}
public function readInteger(int $length = 1)
{
return (int) $this->read($length);
}
public function readFloat(int $length = 1)
{
return (float) $this->read($length);
}
public function readArray(?string $format = null)
{
return $this->scanf($format);
}
public function readLine()
{
return \fgets($this->getStream());
}
public function readAll(int $offset = 0)
{
return \stream_get_contents($this->getStream(), -1, $offset);
}
public function scanf(string $format): array
{
return \fscanf($this->getStream(), $format);
}
}