-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDirectory.php
More file actions
247 lines (226 loc) · 5.78 KB
/
Directory.php
File metadata and controls
247 lines (226 loc) · 5.78 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
declare(strict_types = 1);
namespace Innmind\Filesystem;
use Innmind\Immutable\{
Set,
Sequence,
Maybe,
SideEffect,
};
/**
* @psalm-immutable
*/
final class Directory
{
/**
* @param Sequence<File|self> $files
* @param Set<Name> $removed
*/
private function __construct(
private Name $name,
private Sequence $files,
private Set $removed,
) {
}
/**
* @psalm-pure
*
* @param Sequence<File|self>|null $files
*/
#[\NoDiscard]
public static function of(Name $name, ?Sequence $files = null): self
{
return new self(
$name,
self::safeguard($files ?? Sequence::of()),
Set::of(),
);
}
/**
* @psalm-pure
*
* @param non-empty-string $name
* @param Sequence<File|self>|null $files
*/
#[\NoDiscard]
public static function named(string $name, ?Sequence $files = null): self
{
return new self(
Name::of($name),
self::safeguard($files ?? Sequence::of()),
Set::of(),
);
}
/**
* @internal
* @psalm-pure
*
* @param Sequence<File|self> $files
*/
#[\NoDiscard]
public static function lazy(Name $name, Sequence $files): self
{
// we prevent the contrusctor from checking for duplicates when
// using a lazy set of files as it will trigger to load the whole
// directory tree, it's kinda safe to do this as this method should
// only be used within the filesystem adapter and there should be no
// duplicates on a concrete filesystem
return new self($name, $files, Set::of());
}
#[\NoDiscard]
public function name(): Name
{
return $this->name;
}
#[\NoDiscard]
public function rename(Name $name): self
{
return new self(
$name,
$this->files,
$this->removed,
);
}
#[\NoDiscard]
public function add(File|self $file): self
{
return new self(
$this->name,
$this
->files
->exclude(static fn(File|self $known): bool => $known->name()->equals($file->name()))
->add($file),
$this->removed,
);
}
/**
* @return Maybe<File|self>
*/
#[\NoDiscard]
public function get(Name $name): Maybe
{
return $this->files->find(static fn($file) => $file->name()->equals($name));
}
#[\NoDiscard]
public function contains(Name $name): bool
{
return $this->get($name)->match(
static fn() => true,
static fn() => false,
);
}
#[\NoDiscard]
public function remove(Name $name): self
{
return new self(
$this->name,
$this->files->exclude(static fn(File|self $file) => $file->name()->equals($name)),
($this->removed)($name),
);
}
/**
* @param callable(File|self): void $function
*/
#[\NoDiscard]
public function foreach(callable $function): SideEffect
{
return $this->files->foreach($function);
}
/**
* @param callable(File|self): bool $predicate
*/
#[\NoDiscard]
public function filter(callable $predicate): self
{
// it is safe to not check for duplicates here as either the current
// directory comes from the filesystem thus can't have duplicates or
// comes from a user and must have used the standard constructor that
// validates for duplicates, so they're can't be any duplicates after
// a filter
return new self(
$this->name,
$this->files->filter($predicate),
$this->removed,
);
}
/**
* @param callable(File|self): File $map
*/
#[\NoDiscard]
public function map(callable $map): self
{
return new self(
$this->name,
self::safeguard($this->files->map($map)),
$this->removed,
);
}
/**
* @param callable(File|self): self $map
*/
#[\NoDiscard]
public function flatMap(callable $map): self
{
/** @var callable(File|self): Sequence<File|self> */
$map = static fn(File|self $file): Sequence => $map($file)->all();
return new self(
$this->name,
self::safeguard($this->files->flatMap($map)),
$this->removed,
);
}
/**
* @template R
*
* @param R $carry
* @param callable(R, File|self): R $reducer
*
* @return R
*/
#[\NoDiscard]
public function reduce($carry, callable $reducer)
{
return $this->files->reduce($carry, $reducer);
}
/**
* This method should only be used for implementations of the Adapter
* interface, normal users should never have to use this method
*
* @internal
*
* @return Set<Name>
*/
#[\NoDiscard]
public function removed(): Set
{
return $this->removed;
}
/**
* @return Sequence<File|self>
*/
#[\NoDiscard]
public function all(): Sequence
{
return $this->files;
}
/**
* @psalm-pure
*
* @param Sequence<File|self> $files
*
* @return Sequence<File|self>
*/
private static function safeguard(Sequence $files): Sequence
{
return $files->safeguard(
Set::strings(),
static fn(Set $names, $file) => match ($names->contains($file->name()->toString())) {
true => throw new \LogicException(\sprintf(
"Same file '%s' found multiple times",
$file->name()->toString(),
)),
false => ($names)($file->name()->toString()),
},
);
}
}