Created
November 23, 2016 20:50
-
-
Save sergiors/33db93f18d53b74d34f8f7299f45f7d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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