Skip to content

Commit d9764e7

Browse files
authored
Merge pull request #43 from SimonFrings/functions
Improve documentation to use fully-qualified function names
2 parents c4c6aad + b7713d7 commit d9764e7

2 files changed

Lines changed: 45 additions & 45 deletions

File tree

README.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ A simple and modern approach to stream filtering in PHP
1010
* [Why?](#why)
1111
* [Support us](#support-us)
1212
* [Usage](#usage)
13-
* [append()](#append)
14-
* [prepend()](#prepend)
15-
* [fun()](#fun)
16-
* [remove()](#remove)
13+
* [append()](#append)
14+
* [prepend()](#prepend)
15+
* [fun()](#fun)
16+
* [remove()](#remove)
1717
* [Install](#install)
1818
* [Tests](#tests)
1919
* [License](#license)
@@ -58,26 +58,26 @@ Let's take these projects to the next level together! 🚀
5858
This lightweight library consists only of a few simple functions.
5959
All functions reside under the `Clue\StreamFilter` namespace.
6060

61-
The below examples assume you use an import statement similar to this:
61+
The below examples refer to all functions with their fully-qualified names like this:
6262

6363
```php
64-
use Clue\StreamFilter as Filter;
65-
66-
Filter\append(…);
64+
Clue\StreamFilter\append(…);
6765
```
6866

69-
Alternatively, you can also refer to them with their fully-qualified name:
67+
As of PHP 5.6+ you can also import each required function into your code like this:
7068

7169
```php
72-
\Clue\StreamFilter\append(…);
70+
use function Clue\StreamFilter\append;
71+
72+
append(…);
7373
```
7474

75-
As of PHP 5.6+ you can also import each required function into your code like this:
75+
Alternatively, you can also use an import statement similar to this:
7676

7777
```php
78-
use function Clue\StreamFilter\append;
78+
use Clue\StreamFilter as Filter;
7979

80-
append(…);
80+
Filter\append(…);
8181
```
8282

8383
### append()
@@ -100,7 +100,7 @@ The `$callback` should be a valid callable function which accepts
100100
an individual chunk of data and should return the updated chunk:
101101

102102
```php
103-
$filter = Filter\append($stream, function ($chunk) {
103+
$filter = Clue\StreamFilter\append($stream, function ($chunk) {
104104
// will be called each time you read or write a $chunk to/from the stream
105105
return $chunk;
106106
});
@@ -109,7 +109,7 @@ $filter = Filter\append($stream, function ($chunk) {
109109
As such, you can also use native PHP functions or any other `callable`:
110110

111111
```php
112-
Filter\append($stream, 'strtoupper');
112+
Clue\StreamFilter\append($stream, 'strtoupper');
113113

114114
// will write "HELLO" to the underlying stream
115115
fwrite($stream, 'hello');
@@ -119,7 +119,7 @@ If the `$callback` accepts invocation without parameters,
119119
then this signature will be invoked once ending (flushing) the filter:
120120

121121
```php
122-
Filter\append($stream, function ($chunk = null) {
122+
Clue\StreamFilter\append($stream, function ($chunk = null) {
123123
if ($chunk === null) {
124124
// will be called once ending the filter
125125
return 'end';
@@ -139,7 +139,7 @@ In order to play nice with PHP's stream handling,
139139
the `Exception` will be transformed to a PHP warning instead:
140140

141141
```php
142-
Filter\append($stream, function ($chunk) {
142+
Clue\StreamFilter\append($stream, function ($chunk) {
143143
throw new \RuntimeException('Unexpected chunk');
144144
});
145145

@@ -151,12 +151,12 @@ The optional `$read_write` parameter can be used to only invoke the `$callback`
151151
when either writing to the stream or only when reading from the stream:
152152

153153
```php
154-
Filter\append($stream, function ($chunk) {
154+
Clue\StreamFilter\append($stream, function ($chunk) {
155155
// will be called each time you write to the stream
156156
return $chunk;
157157
}, STREAM_FILTER_WRITE);
158158

159-
Filter\append($stream, function ($chunk) {
159+
Clue\StreamFilter\append($stream, function ($chunk) {
160160
// will be called each time you read from the stream
161161
return $chunk;
162162
}, STREAM_FILTER_READ);
@@ -186,7 +186,7 @@ This function prepends a filter to the start of this list.
186186
If the given filter can not be added, it throws an `Exception`.
187187

188188
```php
189-
$filter = Filter\prepend($stream, function ($chunk) {
189+
$filter = Clue\StreamFilter\prepend($stream, function ($chunk) {
190190
// will be called each time you read or write a $chunk to/from the stream
191191
return $chunk;
192192
});
@@ -208,7 +208,7 @@ Using `fun()` makes accessing these as easy as passing an input string to filter
208208
and getting the filtered output string.
209209

210210
```php
211-
$fun = Filter\fun('string.rot13');
211+
$fun = Clue\StreamFilter\fun('string.rot13');
212212

213213
assert('grfg' === $fun('test'));
214214
assert('test' === $fun($fun('test'));
@@ -221,7 +221,7 @@ or parameters as Zend PHP.
221221
Accessing an unknown filter function will result in a `RuntimeException`:
222222

223223
```php
224-
Filter\fun('unknown'); // throws RuntimeException
224+
Clue\StreamFilter\fun('unknown'); // throws RuntimeException
225225
```
226226

227227
Some filters may accept or require additional filter parameters – most
@@ -234,7 +234,7 @@ Please refer to the individual filter definition for more details.
234234
For example, the `string.strip_tags` filter can be invoked like this:
235235

236236
```php
237-
$fun = Filter\fun('string.strip_tags', '<a><b>');
237+
$fun = Clue\StreamFilter\fun('string.strip_tags', '<a><b>');
238238

239239
$ret = $fun('<b>h<br>i</b>');
240240
assert('<b>hi</b>' === $ret);
@@ -248,7 +248,7 @@ may use internal buffers and may emit a final data chunk on close.
248248
The filter function can be closed by invoking without any arguments:
249249

250250
```php
251-
$fun = Filter\fun('zlib.deflate');
251+
$fun = Clue\StreamFilter\fun('zlib.deflate');
252252

253253
$ret = $fun('hello') . $fun('world') . $fun();
254254
assert('helloworld' === gzinflate($ret));
@@ -258,7 +258,7 @@ The filter function must not be used anymore after it has been closed.
258258
Doing so will result in a `RuntimeException`:
259259

260260
```php
261-
$fun = Filter\fun('string.rot13');
261+
$fun = Clue\StreamFilter\fun('string.rot13');
262262
$fun();
263263

264264
$fun('test'); // throws RuntimeException
@@ -277,15 +277,15 @@ The `remove(resource<stream filter> $filter): bool` function can be used to
277277
remove a filter previously added via [`append()`](#append) or [`prepend()`](#prepend).
278278

279279
```php
280-
$filter = Filter\append($stream, function () {
280+
$filter = Clue\StreamFilter\append($stream, function () {
281281
// …
282282
});
283-
Filter\remove($filter);
283+
Clue\StreamFilter\remove($filter);
284284
```
285285

286286
## Install
287287

288-
The recommended way to install this library is [through Composer](https://getcomposer.org).
288+
The recommended way to install this library is [through Composer](https://getcomposer.org/).
289289
[New to Composer?](https://getcomposer.org/doc/00-intro.md)
290290

291291
This project follows [SemVer](https://semver.org/).
@@ -300,13 +300,13 @@ See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
300300
This project aims to run on any platform and thus does not require any PHP
301301
extensions and supports running on legacy PHP 5.3 through current PHP 8+ and
302302
HHVM.
303-
It's *highly recommended to use PHP 7+* for this project.
303+
It's *highly recommended to use the latest supported PHP version* for this project.
304304
Older PHP versions may suffer from a number of inconsistencies documented above.
305305

306306
## Tests
307307

308308
To run the test suite, you first need to clone this repo and then install all
309-
dependencies [through Composer](https://getcomposer.org):
309+
dependencies [through Composer](https://getcomposer.org/):
310310

311311
```bash
312312
$ composer install
@@ -315,7 +315,7 @@ $ composer install
315315
To run the test suite, go to the project root and run:
316316

317317
```bash
318-
$ php vendor/bin/phpunit
318+
$ vendor/bin/phpunit
319319
```
320320

321321
## License

src/functions.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* an individual chunk of data and should return the updated chunk:
2121
*
2222
* ```php
23-
* $filter = Filter\append($stream, function ($chunk) {
23+
* $filter = Clue\StreamFilter\append($stream, function ($chunk) {
2424
* // will be called each time you read or write a $chunk to/from the stream
2525
* return $chunk;
2626
* });
@@ -29,7 +29,7 @@
2929
* As such, you can also use native PHP functions or any other `callable`:
3030
*
3131
* ```php
32-
* Filter\append($stream, 'strtoupper');
32+
* Clue\StreamFilter\append($stream, 'strtoupper');
3333
*
3434
* // will write "HELLO" to the underlying stream
3535
* fwrite($stream, 'hello');
@@ -39,7 +39,7 @@
3939
* then this signature will be invoked once ending (flushing) the filter:
4040
*
4141
* ```php
42-
* Filter\append($stream, function ($chunk = null) {
42+
* Clue\StreamFilter\append($stream, function ($chunk = null) {
4343
* if ($chunk === null) {
4444
* // will be called once ending the filter
4545
* return 'end';
@@ -59,7 +59,7 @@
5959
* the `Exception` will be transformed to a PHP warning instead:
6060
*
6161
* ```php
62-
* Filter\append($stream, function ($chunk) {
62+
* Clue\StreamFilter\append($stream, function ($chunk) {
6363
* throw new \RuntimeException('Unexpected chunk');
6464
* });
6565
*
@@ -71,12 +71,12 @@
7171
* when either writing to the stream or only when reading from the stream:
7272
*
7373
* ```php
74-
* Filter\append($stream, function ($chunk) {
74+
* Clue\StreamFilter\append($stream, function ($chunk) {
7575
* // will be called each time you write to the stream
7676
* return $chunk;
7777
* }, STREAM_FILTER_WRITE);
7878
*
79-
* Filter\append($stream, function ($chunk) {
79+
* Clue\StreamFilter\append($stream, function ($chunk) {
8080
* // will be called each time you read from the stream
8181
* return $chunk;
8282
* }, STREAM_FILTER_READ);
@@ -126,7 +126,7 @@ function append($stream, $callback, $read_write = STREAM_FILTER_ALL)
126126
* If the given filter can not be added, it throws an `Exception`.
127127
*
128128
* ```php
129-
* $filter = Filter\prepend($stream, function ($chunk) {
129+
* $filter = Clue\StreamFilter\prepend($stream, function ($chunk) {
130130
* // will be called each time you read or write a $chunk to/from the stream
131131
* return $chunk;
132132
* });
@@ -168,7 +168,7 @@ function prepend($stream, $callback, $read_write = STREAM_FILTER_ALL)
168168
* and getting the filtered output string.
169169
*
170170
* ```php
171-
* $fun = Filter\fun('string.rot13');
171+
* $fun = Clue\StreamFilter\fun('string.rot13');
172172
*
173173
* assert('grfg' === $fun('test'));
174174
* assert('test' === $fun($fun('test'));
@@ -181,7 +181,7 @@ function prepend($stream, $callback, $read_write = STREAM_FILTER_ALL)
181181
* Accessing an unknown filter function will result in a `RuntimeException`:
182182
*
183183
* ```php
184-
* Filter\fun('unknown'); // throws RuntimeException
184+
* Clue\StreamFilter\fun('unknown'); // throws RuntimeException
185185
* ```
186186
*
187187
* Some filters may accept or require additional filter parameters – most
@@ -194,7 +194,7 @@ function prepend($stream, $callback, $read_write = STREAM_FILTER_ALL)
194194
* For example, the `string.strip_tags` filter can be invoked like this:
195195
*
196196
* ```php
197-
* $fun = Filter\fun('string.strip_tags', '<a><b>');
197+
* $fun = Clue\StreamFilter\fun('string.strip_tags', '<a><b>');
198198
*
199199
* $ret = $fun('<b>h<br>i</b>');
200200
* assert('<b>hi</b>' === $ret);
@@ -208,7 +208,7 @@ function prepend($stream, $callback, $read_write = STREAM_FILTER_ALL)
208208
* The filter function can be closed by invoking without any arguments:
209209
*
210210
* ```php
211-
* $fun = Filter\fun('zlib.deflate');
211+
* $fun = Clue\StreamFilter\fun('zlib.deflate');
212212
*
213213
* $ret = $fun('hello') . $fun('world') . $fun();
214214
* assert('helloworld' === gzinflate($ret));
@@ -218,7 +218,7 @@ function prepend($stream, $callback, $read_write = STREAM_FILTER_ALL)
218218
* Doing so will result in a `RuntimeException`:
219219
*
220220
* ```php
221-
* $fun = Filter\fun('string.rot13');
221+
* $fun = Clue\StreamFilter\fun('string.rot13');
222222
* $fun();
223223
*
224224
* $fun('test'); // throws RuntimeException
@@ -288,10 +288,10 @@ function fun($filter, $parameters = null)
288288
* Remove a filter previously added via `append()` or `prepend()`.
289289
*
290290
* ```php
291-
* $filter = Filter\append($stream, function () {
291+
* $filter = Clue\StreamFilter\append($stream, function () {
292292
* // …
293293
* });
294-
* Filter\remove($filter);
294+
* Clue\StreamFilter\remove($filter);
295295
* ```
296296
*
297297
* @param resource $filter

0 commit comments

Comments
 (0)