Skip to content

Commit 5c2c477

Browse files
committed
Strings, Arrays, Json, Image: $flags replaced with parameters
1 parent 694130e commit 5c2c477

File tree

11 files changed

+161
-37
lines changed

11 files changed

+161
-37
lines changed

src/Utils/Arrays.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ public static function renameKey(array &$array, string|int $oldKey, string|int $
190190
* @param string[] $array
191191
* @return string[]
192192
*/
193-
public static function grep(array $array, string $pattern, int $flags = 0): array
193+
public static function grep(array $array, string $pattern, bool|int $invert = false): array
194194
{
195+
$flags = $invert ? PREG_GREP_INVERT : 0;
195196
return Strings::pcre('preg_grep', [$pattern, $array, $flags]);
196197
}
197198

src/Utils/Image.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class Image
9898
{
9999
use Nette\SmartObject;
100100

101-
/** {@link resize()} only shrinks images */
101+
/** @deprecated */
102102
public const SHRINK_ONLY = 0b0001;
103103

104104
/** {@link resize()} will ignore aspect ratio */
@@ -303,14 +303,19 @@ public function getImageResource(): \GdImage
303303

304304
/**
305305
* Scales an image. Width and height accept pixels or percent.
306+
* @param self::FIT|self::FILL|self::STRETCH|self::EXACT $mode
306307
*/
307-
public function resize(int|string|null $width, int|string|null $height, int $flags = self::FIT): static
308-
{
309-
if ($flags & self::EXACT) {
308+
public function resize(
309+
int|string|null $width,
310+
int|string|null $height,
311+
int $mode = self::FIT,
312+
bool $shrinkOnly = false,
313+
): static {
314+
if ($mode & self::EXACT) {
310315
return $this->resize($width, $height, self::FILL)->crop('50%', '50%', $width, $height);
311316
}
312317

313-
[$newWidth, $newHeight] = static::calculateSize($this->getWidth(), $this->getHeight(), $width, $height, $flags);
318+
[$newWidth, $newHeight] = static::calculateSize($this->getWidth(), $this->getHeight(), $width, $height, $mode, $shrinkOnly);
314319

315320
if ($newWidth !== $this->getWidth() || $newHeight !== $this->getHeight()) { // resize
316321
$newImage = static::fromBlank($newWidth, $newHeight, self::rgb(0, 0, 0, 127))->getImageResource();
@@ -338,14 +343,17 @@ public function resize(int|string|null $width, int|string|null $height, int $fla
338343

339344
/**
340345
* Calculates dimensions of resized image. Width and height accept pixels or percent.
346+
* @param self::FIT|self::FILL|self::STRETCH $mode
341347
*/
342348
public static function calculateSize(
343349
int $srcWidth,
344350
int $srcHeight,
345351
$newWidth,
346352
$newHeight,
347-
int $flags = self::FIT,
353+
int $mode = self::FIT,
354+
bool $shrinkOnly = false,
348355
): array {
356+
$shrinkOnly = $shrinkOnly || ($mode & self::SHRINK_ONLY); // back compatibility
349357
if ($newWidth === null) {
350358
} elseif (self::isPercent($newWidth)) {
351359
$newWidth = (int) round($srcWidth / 100 * abs($newWidth));
@@ -357,17 +365,17 @@ public static function calculateSize(
357365
if ($newHeight === null) {
358366
} elseif (self::isPercent($newHeight)) {
359367
$newHeight = (int) round($srcHeight / 100 * abs($newHeight));
360-
$flags |= empty($percents) ? 0 : self::STRETCH;
368+
$mode |= empty($percents) ? 0 : self::STRETCH;
361369
} else {
362370
$newHeight = abs($newHeight);
363371
}
364372

365-
if ($flags & self::STRETCH) { // non-proportional
373+
if ($mode & self::STRETCH) { // non-proportional
366374
if (!$newWidth || !$newHeight) {
367375
throw new Nette\InvalidArgumentException('For stretching must be both width and height specified.');
368376
}
369377

370-
if ($flags & self::SHRINK_ONLY) {
378+
if ($shrinkOnly) {
371379
$newWidth = (int) round($srcWidth * min(1, $newWidth / $srcWidth));
372380
$newHeight = (int) round($srcHeight * min(1, $newHeight / $srcHeight));
373381
}
@@ -386,11 +394,11 @@ public static function calculateSize(
386394
$scale[] = $newHeight / $srcHeight;
387395
}
388396

389-
if ($flags & self::FILL) {
397+
if ($mode & self::FILL) {
390398
$scale = [max($scale)];
391399
}
392400

393-
if ($flags & self::SHRINK_ONLY) {
401+
if ($shrinkOnly) {
394402
$scale[] = 1;
395403
}
396404

src/Utils/Json.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,32 @@ final class Json
1919
{
2020
use Nette\StaticClass;
2121

22+
/** @deprecated */
2223
public const FORCE_ARRAY = 0b0001;
2324

25+
/** @deprecated */
2426
public const PRETTY = 0b0010;
2527

28+
/** @deprecated */
2629
public const ESCAPE_UNICODE = 0b0100;
2730

2831

2932
/**
30-
* Converts value to JSON format. The flag can be Json::PRETTY, which formats JSON for easier reading and clarity,
31-
* and Json::ESCAPE_UNICODE for ASCII output.
33+
* Converts value to JSON format. Use $pretty for easier reading and clarity and $escapeUnicode for ASCII output.
3234
* @throws JsonException
3335
*/
34-
public static function encode(mixed $value, int $flags = 0): string
35-
{
36-
$flags = ($flags & self::ESCAPE_UNICODE ? 0 : JSON_UNESCAPED_UNICODE)
36+
public static function encode(
37+
mixed $value,
38+
bool|int $pretty = false,
39+
bool $escapeUnicode = false,
40+
): string {
41+
if (is_int($pretty)) { // back compatibility
42+
$escapeUnicode = $pretty & self::ESCAPE_UNICODE;
43+
$pretty &= self::PRETTY;
44+
}
45+
$flags = ($escapeUnicode ? 0 : JSON_UNESCAPED_UNICODE)
3746
| JSON_UNESCAPED_SLASHES
38-
| ($flags & self::PRETTY ? JSON_PRETTY_PRINT : 0)
47+
| ($pretty ? JSON_PRETTY_PRINT : 0)
3948
| (defined('JSON_PRESERVE_ZERO_FRACTION') ? JSON_PRESERVE_ZERO_FRACTION : 0); // since PHP 5.6.6 & PECL JSON-C 1.3.7
4049

4150
$json = json_encode($value, $flags);
@@ -47,13 +56,12 @@ public static function encode(mixed $value, int $flags = 0): string
4756

4857

4958
/**
50-
* Parses JSON to PHP value. The flag can be Json::FORCE_ARRAY, which forces an array instead of an object as the return value.
59+
* Parses JSON to PHP value. Parameter $forceArray forces an array instead of an object as the return value.
5160
* @throws JsonException
5261
*/
53-
public static function decode(string $json, int $flags = 0): mixed
62+
public static function decode(string $json, bool|int $forceArray = false): mixed
5463
{
55-
$forceArray = (bool) ($flags & self::FORCE_ARRAY);
56-
$value = json_decode($json, $forceArray, 512, JSON_BIGINT_AS_STRING);
64+
$value = json_decode($json, (bool) $forceArray, 512, JSON_BIGINT_AS_STRING);
5765
if ($error = json_last_error()) {
5866
throw new JsonException(json_last_error_msg(), $error);
5967
}

src/Utils/Strings.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -468,20 +468,33 @@ private static function pos(string $haystack, string $needle, int $nth = 1): ?in
468468

469469
/**
470470
* Splits a string into array by the regular expression. Parenthesized expression in the delimiter are captured.
471-
* Parameter $flags can be any combination of PREG_SPLIT_NO_EMPTY and PREG_OFFSET_CAPTURE flags.
472471
*/
473-
public static function split(string $subject, string $pattern, int $flags = 0): array
474-
{
472+
public static function split(
473+
string $subject,
474+
string $pattern,
475+
bool|int $captureOffset = false,
476+
bool $skipEmpty = false,
477+
): array {
478+
$flags = is_int($captureOffset) && $captureOffset // back compatibility
479+
? $captureOffset
480+
: ($captureOffset ? PREG_SPLIT_OFFSET_CAPTURE : 0) | ($skipEmpty ? PREG_SPLIT_NO_EMPTY : 0);
475481
return self::pcre('preg_split', [$pattern, $subject, -1, $flags | PREG_SPLIT_DELIM_CAPTURE]);
476482
}
477483

478484

479485
/**
480486
* Checks if given string matches a regular expression pattern and returns an array with first found match and each subpattern.
481-
* Parameter $flags can be any combination of PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL flags.
482487
*/
483-
public static function match(string $subject, string $pattern, int $flags = 0, int $offset = 0): ?array
484-
{
488+
public static function match(
489+
string $subject,
490+
string $pattern,
491+
bool|int $captureOffset = false,
492+
int $offset = 0,
493+
bool $unmatchedAsNull = false,
494+
): ?array {
495+
$flags = is_int($captureOffset) && $captureOffset // back compatibility
496+
? $captureOffset
497+
: ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0);
485498
if ($offset > strlen($subject)) {
486499
return null;
487500
}
@@ -492,11 +505,20 @@ public static function match(string $subject, string $pattern, int $flags = 0, i
492505

493506

494507
/**
495-
* Finds all occurrences matching regular expression pattern and returns a two-dimensional array. Result is array of matches (ie uses by default PREG_SET_ORDER).
496-
* Parameter $flags can be any combination of PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL and PREG_PATTERN_ORDER flags.
508+
* Finds all occurrences matching regular expression pattern and returns a two-dimensional array.
509+
* Result is array of matches (ie uses by default PREG_SET_ORDER).
497510
*/
498-
public static function matchAll(string $subject, string $pattern, int $flags = 0, int $offset = 0): array
499-
{
511+
public static function matchAll(
512+
string $subject,
513+
string $pattern,
514+
bool|int $captureOffset = false,
515+
int $offset = 0,
516+
bool $unmatchedAsNull = false,
517+
bool $patternOrder = false,
518+
): array {
519+
$flags = is_int($captureOffset) && $captureOffset // back compatibility
520+
? $captureOffset
521+
: ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0) | ($patternOrder ? PREG_PATTERN_ORDER : 0);
500522
if ($offset > strlen($subject)) {
501523
return [];
502524
}

tests/Utils/Arrays.grep().phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ Assert::same([
2121
0 => 'a',
2222
2 => 'c',
2323
], Arrays::grep(['a', '1', 'c'], '#\d#', PREG_GREP_INVERT));
24+
25+
Assert::same([
26+
0 => 'a',
27+
2 => 'c',
28+
], Arrays::grep(['a', '1', 'c'], '#\d#', invert: true));

tests/Utils/Image.resize.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ test('resizing Y shrink', function () use ($main) {
5252
});
5353

5454

55+
test('resizing Y shrink', function () use ($main) {
56+
$image = clone $main;
57+
$image->resize(null, 150, shrinkOnly: true);
58+
Assert::same(176, $image->width);
59+
Assert::same(104, $image->height);
60+
});
61+
62+
5563
test('resizing X Y shrink', function () use ($main) {
5664
$image = clone $main;
5765
$image->resize(300, 150, Image::SHRINK_ONLY);
@@ -60,6 +68,14 @@ test('resizing X Y shrink', function () use ($main) {
6068
});
6169

6270

71+
test('resizing X Y shrink', function () use ($main) {
72+
$image = clone $main;
73+
$image->resize(300, 150, shrinkOnly: true);
74+
Assert::same(176, $image->width);
75+
Assert::same(104, $image->height);
76+
});
77+
78+
6379
test('resizing X Y', function () use ($main) {
6480
$image = clone $main;
6581
$image->resize(300, 150);
@@ -84,6 +100,14 @@ test('resizing X Y shrink stretch', function () use ($main) {
84100
});
85101

86102

103+
test('resizing X Y shrink stretch', function () use ($main) {
104+
$image = clone $main;
105+
$image->resize(300, 100, Image::STRETCH, shrinkOnly: true);
106+
Assert::same(176, $image->width);
107+
Assert::same(100, $image->height);
108+
});
109+
110+
87111
test('resizing X%', function () use ($main) {
88112
$image = clone $main;
89113
$image->resize('110%', null);
@@ -116,6 +140,14 @@ test('flipping Y shrink', function () use ($main) {
116140
});
117141

118142

143+
test('flipping Y shrink', function () use ($main) {
144+
$image = clone $main;
145+
$image->resize(null, -150, shrinkOnly: true);
146+
Assert::same(176, $image->width);
147+
Assert::same(104, $image->height);
148+
});
149+
150+
119151
test('flipping X Y shrink', function () use ($main) {
120152
$image = clone $main;
121153
$image->resize(-300, -150, Image::SHRINK_ONLY);
@@ -124,6 +156,14 @@ test('flipping X Y shrink', function () use ($main) {
124156
});
125157

126158

159+
test('flipping X Y shrink', function () use ($main) {
160+
$image = clone $main;
161+
$image->resize(-300, -150, shrinkOnly: true);
162+
Assert::same(176, $image->width);
163+
Assert::same(104, $image->height);
164+
});
165+
166+
127167
test('exact resize', function () use ($main) {
128168
$image = clone $main;
129169
$image->resize(300, 150, Image::EXACT);

tests/Utils/Json.decode().phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Assert::null(Json::decode(' null'));
2020

2121
Assert::equal((object) ['a' => 1], Json::decode('{"a":1}'));
2222
Assert::same(['a' => 1], Json::decode('{"a":1}', Json::FORCE_ARRAY));
23+
Assert::same(['a' => 1], Json::decode('{"a":1}', forceArray: true));
2324

2425

2526
Assert::exception(function () {

tests/Utils/Json.encode().phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ Assert::same('"\u2028\u2029"', Json::encode("\u{2028}\u{2029}"));
3636
// ESCAPE_UNICODE
3737
Assert::same('"/I\u00f1t\u00ebrn\u00e2ti\u00f4n\u00e0liz\u00e6ti\u00f8n"', Json::encode("/I\u{F1}t\u{EB}rn\u{E2}ti\u{F4}n\u{E0}liz\u{E6}ti\u{F8}n", Json::ESCAPE_UNICODE));
3838
Assert::same('"\u2028\u2029"', Json::encode("\u{2028}\u{2029}", Json::ESCAPE_UNICODE));
39+
Assert::same('"\u2028\u2029"', Json::encode("\u{2028}\u{2029}", escapeUnicode: true));
3940

4041

4142
// JSON_PRETTY_PRINT
4243
Assert::same("[\n 1,\n 2,\n 3\n]", Json::encode([1, 2, 3], Json::PRETTY));
44+
Assert::same("[\n 1,\n 2,\n 3\n]", Json::encode([1, 2, 3], pretty: true));
4345

4446

4547
Assert::exception(function () {

tests/Utils/Strings.match().phpt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ Assert::same(['hell', 'l'], Strings::match('hello world!', '#([e-l])+#'));
2020
Assert::same(['hell'], Strings::match('hello world!', '#[e-l]+#'));
2121

2222
Assert::same([['hell', 0]], Strings::match('hello world!', '#[e-l]+#', PREG_OFFSET_CAPTURE));
23+
Assert::same([['hell', 0]], Strings::match('hello world!', '#[e-l]+#', captureOffset: true));
2324

24-
Assert::same(['ll'], Strings::match('hello world!', '#[e-l]+#', 0, 2));
25+
Assert::same(['e', null], Strings::match('hello world!', '#e(x)*#', unmatchedAsNull: true));
26+
Assert::same(['e', null], Strings::match('hello world!', '#e(x)*#', 0, 0, unmatchedAsNull: true)); // $flags = 0
2527

26-
Assert::null(Strings::match('hello world!', '', 0, 50));
27-
Assert::null(Strings::match('', '', 0, 1));
28+
Assert::same(['ll'], Strings::match('hello world!', '#[e-l]+#', offset: 2));
29+
30+
Assert::null(Strings::match('hello world!', '', offset: 50));
31+
Assert::null(Strings::match('', '', offset: 1));

tests/Utils/Strings.matchAll().phpt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,31 @@ Assert::same([
3232
[['k', 14], ['k', 14], ['', 15]],
3333
], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', PREG_OFFSET_CAPTURE));
3434

35+
Assert::same([
36+
[['lu', 2], ['l', 2], ['u', 3]],
37+
[['ou', 6], ['o', 6], ['u', 7]],
38+
[['k', 10], ['k', 10], ['', 11]],
39+
[['k', 14], ['k', 14], ['', 15]],
40+
], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', captureOffset: true));
41+
3542
Assert::same([
3643
[['lu', 2], ['ou', 6], ['k', 10], ['k', 14]],
3744
[['l', 2], ['o', 6], ['k', 10], ['k', 14]],
3845
[['u', 3], ['u', 7], ['', 11], ['', 15]],
3946
], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER));
4047

41-
Assert::same([['l'], ['k'], ['k']], Strings::matchAll('žluťoučký kůň', '#[e-l]+#u', 0, 2));
48+
Assert::same([
49+
[['lu', 2], ['ou', 6], ['k', 10], ['k', 14]],
50+
[['l', 2], ['o', 6], ['k', 10], ['k', 14]],
51+
[['u', 3], ['u', 7], ['', 11], ['', 15]],
52+
], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', captureOffset: true, patternOrder: true));
53+
54+
Assert::same([['l'], ['k'], ['k']], Strings::matchAll('žluťoučký kůň', '#[e-l]+#u', offset: 2));
4255

4356
Assert::same([['ll', 'l']], Strings::matchAll('hello world!', '#[e-l]+#', PREG_PATTERN_ORDER, 2));
57+
Assert::same([['ll', 'l']], Strings::matchAll('hello world!', '#[e-l]+#', offset: 2, patternOrder: true));
58+
59+
Assert::same([['e', null]], Strings::matchAll('hello world!', '#e(x)*#', unmatchedAsNull: true));
60+
Assert::same([['e', null]], Strings::matchAll('hello world!', '#e(x)*#', 0, 0, unmatchedAsNull: true)); // $flags = 0
4461

45-
Assert::same([], Strings::matchAll('hello world!', '', 0, 50));
62+
Assert::same([], Strings::matchAll('hello world!', '', offset: 50));

0 commit comments

Comments
 (0)