Skip to content

Commit

Permalink
add Content::io()
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Oct 22, 2023
1 parent 3ae963f commit b9ed2a8
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog

## 7.1.0 - 2023-10-22

### Added

- `Innmind\Filesystem\File\Content::io()`

## 7.0.0 - 2023-10-21

### Added
Expand Down
10 changes: 10 additions & 0 deletions proofs/file/content.php
Expand Up @@ -37,6 +37,16 @@
$path,
)),
],
[
'Content::io()',
Set\Elements::of('LICENSE', 'CHANGELOG.md', 'composer.json')
->map(Path::of(...))
->map(static fn($path) => Model::io(
$io->wrap(
$capabilities->readable()->open($path),
),
)),
],
[
'Content::none()',
Set\Elements::of(Model::none()),
Expand Down
8 changes: 8 additions & 0 deletions src/File/Content.php
Expand Up @@ -44,6 +44,14 @@ public static function atPath(
return new self(Content\AtPath::of($capabilities, $io, $path));
}

/**
* @psalm-pure
*/
public static function io(IO\Readable\Stream $io): self
{
return new self(Content\IO::of($io));
}

/**
* @psalm-pure
*
Expand Down
96 changes: 96 additions & 0 deletions src/File/Content/IO.php
@@ -0,0 +1,96 @@
<?php
declare(strict_types = 1);

namespace Innmind\Filesystem\File\Content;

use Innmind\IO\Readable\Stream;
use Innmind\Immutable\{
Sequence,
SideEffect,
Maybe,
Monoid\Concat,
};

/**
* @internal
* @psalm-immutable
*/
final class IO implements Implementation
{
private Stream $io;

private function __construct(Stream $io)
{
$this->io = $io;
}

/**
* @psalm-pure
*/
public static function of(Stream $io): self
{
return new self($io);
}

public function foreach(callable $function): SideEffect
{
return $this->lines()->foreach($function);
}

public function map(callable $map): Implementation
{
return Lines::of($this->lines()->map($map));
}

public function flatMap(callable $map): Implementation
{
return Lines::of($this->lines())->flatMap($map);
}

public function filter(callable $filter): Implementation
{
return Lines::of($this->lines()->filter($filter));
}

public function lines(): Sequence
{
/** @psalm-suppress ImpureMethodCall */
return $this
->io
->lines()
->lazy()
->rewindable()
->sequence()
->map(static fn($line) => Line::fromStream($line));
}

public function reduce($carry, callable $reducer)
{
return $this->lines()->reduce($carry, $reducer);
}

public function size(): Maybe
{
/** @psalm-suppress ImpureMethodCall */
return $this->io->size();
}

public function toString(): string
{
return $this
->chunks()
->fold(new Concat)
->toString();
}

public function chunks(): Sequence
{
/** @psalm-suppress ImpureMethodCall */
return $this
->io
->chunks(8192)
->lazy()
->rewindable()
->sequence();
}
}

0 comments on commit b9ed2a8

Please sign in to comment.