-
Notifications
You must be signed in to change notification settings - Fork 0
/
Content.php
177 lines (157 loc) · 3.48 KB
/
Content.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
declare(strict_types = 1);
namespace Innmind\Filesystem\File;
use Innmind\Filesystem\File\Content\{
Implementation,
Line,
};
use Innmind\IO;
use Innmind\Stream\{
Stream\Size,
Capabilities,
};
use Innmind\Url\Path;
use Innmind\Immutable\{
Str,
Sequence,
SideEffect,
Maybe,
};
/**
* @psalm-immutable
*/
final class Content
{
private Implementation $implementation;
private function __construct(Implementation $implementation)
{
$this->implementation = $implementation;
}
/**
* @psalm-pure
*/
public static function atPath(
Capabilities\Readable $capabilities,
IO\Readable $io,
Path $path,
): self {
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
*
* This method is to be used with sockets that can't be read twice
*/
public static function oneShot(IO\Readable\Stream $io): self
{
return new self(Content\OneShot::of($io));
}
/**
* @psalm-pure
*
* @param Sequence<Str> $chunks
*/
public static function ofChunks(Sequence $chunks): self
{
return new self(Content\Chunks::of($chunks));
}
/**
* @psalm-pure
*
* @param Sequence<Line> $lines
*/
public static function ofLines(Sequence $lines): self
{
return new self(Content\Lines::of($lines));
}
/**
* @psalm-pure
*/
public static function ofString(string $content): self
{
return self::ofLines(
Str::of($content)
->split("\n")
->map(static fn($line) => Line::fromStream($line)),
);
}
/**
* @psalm-pure
*/
public static function none(): self
{
return new self(Content\Lines::of(Sequence::of(Line::of(Str::of('')))));
}
/**
* @param callable(Line): void $function
*/
public function foreach(callable $function): SideEffect
{
return $this->implementation->foreach($function);
}
/**
* @param callable(Line): Line $map
*/
public function map(callable $map): self
{
return new self($this->implementation->map($map));
}
/**
* @param callable(Line): self $map
*/
public function flatMap(callable $map): self
{
return new self($this->implementation->flatMap($map));
}
/**
* @param callable(Line): bool $filter
*/
public function filter(callable $filter): self
{
return new self($this->implementation->filter($filter));
}
/**
* @return Sequence<Line>
*/
public function lines(): Sequence
{
return $this->implementation->lines();
}
/**
* @return Sequence<Str>
*/
public function chunks(): Sequence
{
return $this->implementation->chunks();
}
/**
* @template T
*
* @param T $carry
* @param callable(T, Line): T $reducer
*
* @return T
*/
public function reduce($carry, callable $reducer)
{
return $this->implementation->reduce($carry, $reducer);
}
/**
* @return Maybe<Size>
*/
public function size(): Maybe
{
return $this->implementation->size();
}
public function toString(): string
{
return $this->implementation->toString();
}
}