forked from xp-framework/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlob.class.php
More file actions
executable file
·148 lines (128 loc) · 3.7 KB
/
Blob.class.php
File metadata and controls
executable file
·148 lines (128 loc) · 3.7 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
<?php namespace io;
use IteratorAggregate, Traversable;
use io\streams\{InputStream, IterableInputStream, Streams};
use lang\{Value, IllegalArgumentException};
use util\{Bytes, Objects};
/** @test io.unittest.BlobTest */
class Blob implements IteratorAggregate, Value {
private $parts;
private $iterator= null;
public $meta= [];
/**
* Creates a new blob from parts
*
* @param iterable|string|util.Bytes|io.streams.InputStream $parts
* @param [:var] $meta
* @throws lang.IllegalArgumentException
*/
public function __construct($parts= [], array $meta= []) {
if ($parts instanceof InputStream) {
$this->iterator= function() {
static $started= false;
return (function() use(&$started) {
$started ? Streams::seek($this->parts, 0) : $started= true;
while ($this->parts->available()) {
yield $this->parts->read();
}
})();
};
} else if ($parts instanceof Bytes || is_string($parts)) {
$this->iterator= fn() => (function() { yield (string)$this->parts; })();
} else if (is_iterable($parts)) {
$this->iterator= fn() => (function() {
foreach ($this->parts as $part) {
yield (string)$part;
}
})();
} else {
throw new IllegalArgumentException(sprintf(
'Expected iterable|string|util.Bytes|io.streams.InputStream, have %s',
typeof($parts)
));
}
$this->parts= $parts;
$this->meta= $meta;
}
/** @return iterable */
public function getIterator(): Traversable { return ($this->iterator)(); }
/** @return util.Bytes */
public function bytes() {
return $this->parts instanceof Bytes
? $this->parts
: new Bytes(...($this->iterator)())
;
}
/** @return io.streams.InputStream */
public function stream() {
return $this->parts instanceof InputStream
? $this->parts
: new IterableInputStream(($this->iterator)())
;
}
/** Creates a new blob with the given filter applied */
public function encoded(string $filter): self {
$it= function() use($filter) {
$fd= Streams::readableFd($this->stream());
if (!stream_filter_append($fd, $filter, STREAM_FILTER_READ)) {
fclose($fd);
throw new OperationNotSupportedException('Cannot stream '.$filter);
}
try {
do {
yield fread($fd, 8192);
} while (!feof($fd));
} finally {
fclose($fd);
}
};
$meta= $this->meta;
$meta['encoding']??= [];
$meta['encoding'][]= $filter;
return new self($it(), $meta);
}
/** @return iterable */
public function slices(int $size= 8192) {
$it= ($this->iterator)();
$it->rewind();
while ($it->valid()) {
$slice= $it->current();
$length= strlen($slice);
$offset= 0;
while ($length < $size) {
$it->next();
$slice.= $it->current();
if (!$it->valid()) break;
}
while ($length - $offset > $size) {
yield substr($slice, $offset, $size);
$offset+= $size;
}
yield $offset ? substr($slice, $offset) : $slice;
$it->next();
}
}
/** @return string */
public function hashCode() { return 'B'.Objects::hashOf($this->parts); }
/** @return string */
public function toString() { return nameof($this).'('.Objects::stringOf($this->parts).')'; }
/**
* Comparison
*
* @param var $value
* @return int
*/
public function compareTo($value) {
return $value instanceof self
? Objects::compare($this->parts, $value->parts)
: 1
;
}
/** @return string */
public function __toString() {
$bytes= '';
foreach (($this->iterator)() as $chunk) {
$bytes.= $chunk;
}
return $bytes;
}
}