Skip to content

Commit f1dc085

Browse files
committed
added Helpers
1 parent c133e18 commit f1dc085

6 files changed

Lines changed: 80 additions & 5 deletions

File tree

src/Utils/Arrays.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static function mergeTree(array $arr1, array $arr2): array
8383
public static function searchKey(array $arr, $key): ?int
8484
{
8585
$foo = [$key => null];
86-
return ($tmp = array_search(key($foo), array_keys($arr), true)) === false ? null : $tmp;
86+
return Helpers::falseToNull(array_search(key($foo), array_keys($arr), true));
8787
}
8888

8989

src/Utils/Helpers.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Utils;
11+
12+
13+
class Helpers
14+
{
15+
/**
16+
* Captures PHP output into a string.
17+
*/
18+
public static function capture(callable $func): string
19+
{
20+
ob_start(function () {});
21+
try {
22+
$func();
23+
return ob_get_clean();
24+
} catch (\Throwable $e) {
25+
ob_end_clean();
26+
throw $e;
27+
}
28+
}
29+
30+
31+
public static function falseToNull($val)
32+
{
33+
return $val === false ? null : $val;
34+
}
35+
}

src/Utils/Image.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,9 @@ public function save(string $file, int $quality = null, int $type = null): void
502502
*/
503503
public function toString(int $type = self::JPEG, int $quality = null): string
504504
{
505-
ob_start(function () {});
506-
$this->output($type, $quality);
507-
return ob_get_clean();
505+
return Helpers::capture(function () use ($type, $quality) {
506+
$this->output($type, $quality);
507+
});
508508
}
509509

510510

src/Utils/Strings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ private static function pos(string $haystack, string $needle, int $nth = 1): ?in
423423
$pos--;
424424
}
425425
}
426-
return $pos === false ? null : $pos;
426+
return Helpers::falseToNull($pos);
427427
}
428428

429429

tests/Utils/Helpers.capture().phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Utils\Helpers;
6+
use Tester\Assert;
7+
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
Assert::same('', Helpers::capture(function () {}));
13+
Assert::same('hello', Helpers::capture(function () { echo 'hello'; }));
14+
15+
16+
17+
$level = ob_get_level();
18+
19+
Assert::exception(function () {
20+
Helpers::capture(function () {
21+
undefined();
22+
});
23+
}, Error::class, 'Call to undefined function undefined()');
24+
25+
Assert::same($level, ob_get_level());
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Utils\Helpers;
6+
use Tester\Assert;
7+
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
Assert::same(null, Helpers::falseToNull(null));
13+
Assert::same(true, Helpers::falseToNull(true));
14+
Assert::same(null, Helpers::falseToNull(false));
15+
Assert::same([], Helpers::falseToNull([]));

0 commit comments

Comments
 (0)