<?php
declare(strict_types=1);
stream_wrapper_register('fizzbuzz', FizzBuzzStreamWrapper::class);
var_dump(fstat(fopen('fizzbuzz://22', 'r'))['size']);
echo file_get_contents('fizzbuzz://22');
final class FizzBuzzStreamWrapper
{
private int $counter = 1;
private int $length = 15;
/** @var resource */
public $context;
public function stream_open(string $path, $mode, $options, &$opened_path): bool
{
preg_match('#([\d]+$)#', $path, $matches);
$this->length = filter_var(
$matches[1] ?? '',
FILTER_VALIDATE_INT,
['options' => ['default' => 15]]
);
return true;
}
public function stream_read($count): string|false
{
if ($this->counter > $this->length) {
return false;
}
$fizzbuzz = match (true) {
$this->counter % 15 === 0 => 'FizzBuzz',
$this->counter % 5 === 0 => 'Buzz',
$this->counter % 3 === 0 => 'Fizz',
default => (string)$this->counter
};
$contents = '';
if ($this->counter === 1) {
$contents = '<?php ' . PHP_EOL;
}
$contents .= "echo '{$fizzbuzz}' . PHP_EOL;";
$this->counter++;
return $contents;
}
public function stream_close(): bool
{
return true;
}
public function stream_eof(): bool
{
return $this->counter ===$this->length;
}
public function stream_stat(): array|false
{
return ['size' => 0];
// return ['size' => $this->getSize()];
}
public function stream_set_option(int $option, int $arg1, int $arg2)
{
return false;
}
private function getSize()
{
$ret = strlen("<?php \n") + $this->length * 18;
$range = range(1, $this->length);
$numbers = array_filter($range, fn ($n) => ($n % 3 !== 0 && $n % 5 !== 0));
$ret += strlen(implode('', $numbers));
$fizzOrBuzzs = array_filter($range, fn ($n) => ($n % 15 !== 0) && ($n % 3 === 0 || $n % 5 === 0));
$ret += strlen('fizz') * count($fizzOrBuzzs);
$fizzbuzzs = array_filter($range, fn ($n) => ($n % 15 === 0));
$ret += strlen('fizzbuzz') * count($fizzbuzzs);
return $ret;
}
}
- Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- int(0)
<?php
echo '1' . PHP_EOL;echo '2' . PHP_EOL;echo 'Fizz' . PHP_EOL;echo '4' . PHP_EOL;echo 'Buzz' . PHP_EOL;echo 'Fizz' . PHP_EOL;echo '7' . PHP_EOL;echo '8' . PHP_EOL;echo 'Fizz' . PHP_EOL;echo 'Buzz' . PHP_EOL;echo '11' . PHP_EOL;echo 'Fizz' . PHP_EOL;echo '13' . PHP_EOL;echo '14' . PHP_EOL;echo 'FizzBuzz' . PHP_EOL;echo '16' . PHP_EOL;echo '17' . PHP_EOL;echo 'Fizz' . PHP_EOL;echo '19' . PHP_EOL;echo 'Buzz' . PHP_EOL;echo 'Fizz' . PHP_EOL;echo '22' . PHP_EOL;
preferences:
76.87 ms | 408 KiB | 5 Q