Skip to content

Instantly share code, notes, and snippets.

@sergiors
Created November 23, 2016 20:50
Show Gist options
  • Save sergiors/33db93f18d53b74d34f8f7299f45f7d7 to your computer and use it in GitHub Desktop.
Save sergiors/33db93f18d53b74d34f8f7299f45f7d7 to your computer and use it in GitHub Desktop.
<?php
use const Prelude\{_, id, isResource};
use function Prelude\{always, ifElse, apply, placeholder};
class Stream
{
protected $stream;
public function __construct($stream, $options = [])
{
$fn = ifElse(isResource, id, function () {
throw new \InvalidArgumentException();
});
$this->stream = $fn($stream);
}
public function eof()
{
}
public function close()
{
return $this->do('fclose');
}
public function write()
{
}
public function tell()
{
return $this->do('ftell');
}
public function seek($offset, $whence = SEEK_SET)
{
return $this->do(placeholder('fseek', _, $offset, $whence));
}
public function rewind()
{
return $this->do('rewind');
}
public function readLine()
{
return $this->do(placeholder('stream_get_line', _, 1024, "\n"));
}
public function __destruct()
{
$this->close();
}
private function do(callable $callback)
{
return apply(ifElse(isResource, apply($callback), always(false)), $this->stream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment