aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/maennchen/zipstream-php/test/ResourceStream.php
blob: 752a1a35748526960b81e56f1dd5c0e08c78fe54 (plain) (blame)
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
<?php

declare(strict_types=1);

namespace ZipStream\Test;

use Psr\Http\Message\StreamInterface;
use RuntimeException;

/**
 * @internal
 */
class ResourceStream implements StreamInterface
{
    public function __construct(
        /**
         * @var resource
         */
        private $stream
    ) {}

    public function __toString(): string
    {
        if ($this->isSeekable()) {
            $this->seek(0);
        }
        return (string) stream_get_contents($this->stream);
    }

    public function close(): void
    {
        $stream = $this->detach();
        if ($stream) {
            fclose($stream);
        }
    }

    public function detach()
    {
        $result = $this->stream;
        // According to the interface, the stream is left in an unusable state;
        /** @psalm-suppress PossiblyNullPropertyAssignmentValue */
        $this->stream = null;
        return $result;
    }

    public function seek(int $offset, int $whence = SEEK_SET): void
    {
        if (!$this->isSeekable()) {
            throw new RuntimeException();
        }
        if (fseek($this->stream, $offset, $whence) !== 0) {
            // @codeCoverageIgnoreStart
            throw new RuntimeException();
            // @codeCoverageIgnoreEnd
        }
    }

    public function isSeekable(): bool
    {
        return (bool) $this->getMetadata('seekable');
    }

    public function getMetadata(?string $key = null)
    {
        $metadata = stream_get_meta_data($this->stream);
        return $key !== null ? @$metadata[$key] : $metadata;
    }

    public function getSize(): ?int
    {
        $stats = fstat($this->stream);
        return $stats['size'];
    }

    public function tell(): int
    {
        $position = ftell($this->stream);
        if ($position === false) {
            // @codeCoverageIgnoreStart
            throw new RuntimeException();
            // @codeCoverageIgnoreEnd
        }
        return $position;
    }

    public function eof(): bool
    {
        return feof($this->stream);
    }

    public function rewind(): void
    {
        $this->seek(0);
    }

    public function write(string $string): int
    {
        if (!$this->isWritable()) {
            throw new RuntimeException();
        }
        if (fwrite($this->stream, $string) === false) {
            // @codeCoverageIgnoreStart
            throw new RuntimeException();
            // @codeCoverageIgnoreEnd
        }
        return strlen($string);
    }

    public function isWritable(): bool
    {
        $mode = $this->getMetadata('mode');
        if (!is_string($mode)) {
            // @codeCoverageIgnoreStart
            throw new RuntimeException('Could not get stream mode from metadata!');
            // @codeCoverageIgnoreEnd
        }
        return preg_match('/[waxc+]/', $mode) === 1;
    }

    public function read(int $length): string
    {
        if (!$this->isReadable()) {
            throw new RuntimeException();
        }
        $result = fread($this->stream, $length);
        if ($result === false) {
            // @codeCoverageIgnoreStart
            throw new RuntimeException();
            // @codeCoverageIgnoreEnd
        }
        return $result;
    }

    public function isReadable(): bool
    {
        $mode = $this->getMetadata('mode');
        if (!is_string($mode)) {
            // @codeCoverageIgnoreStart
            throw new RuntimeException('Could not get stream mode from metadata!');
            // @codeCoverageIgnoreEnd
        }
        return preg_match('/[r+]/', $mode) === 1;
    }

    public function getContents(): string
    {
        if (!$this->isReadable()) {
            throw new RuntimeException();
        }
        $result = stream_get_contents($this->stream);
        if ($result === false) {
            // @codeCoverageIgnoreStart
            throw new RuntimeException();
            // @codeCoverageIgnoreEnd
        }
        return $result;
    }
}