Skip to content

Commit e00ea9c

Browse files
committed
Strings, Arrays, Json, Image: $flags replaced with parameters
1 parent cdd74d1 commit e00ea9c

File tree

11 files changed

+164
-35
lines changed

11 files changed

+164
-35
lines changed

src/Utils/Arrays.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,9 @@ public static function renameKey(array &$array, string|int $oldKey, string|int $
196196
* @param string[] $array
197197
* @return string[]
198198
*/
199-
public static function grep(array $array, string $pattern, int $flags = 0): array
199+
public static function grep(array $array, string $pattern, bool|int $invert = false): array
200200
{
201+
$flags = $invert ? PREG_GREP_INVERT : 0;
201202
return Strings::pcre('preg_grep', [$pattern, $array, $flags]);
202203
}
203204

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 */
@@ -318,14 +318,19 @@ public function getImageResource(): \GdImage
318318

319319
/**
320320
* Scales an image. Width and height accept pixels or percent.
321+
* @param self::FIT|self::FILL|self::STRETCH|self::EXACT $mode
321322
*/
322-
public function resize(int|string|null $width, int|string|null $height, int $flags = self::FIT): static
323-
{
324-
if ($flags & self::EXACT) {
323+
public function resize(
324+
int|string|null $width,
325+
int|string|null $height,
326+
int $mode = self::FIT,
327+
bool $shrinkOnly = false,
328+
): static {
329+
if ($mode & self::EXACT) {
325330
return $this->resize($width, $height, self::FILL)->crop('50%', '50%', $width, $height);
326331
}
327332

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

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

355360
/**
356361
* Calculates dimensions of resized image. Width and height accept pixels or percent.
362+
* @param self::FIT|self::FILL|self::STRETCH $mode
357363
*/
358364
public static function calculateSize(
359365
int $srcWidth,
360366
int $srcHeight,
361367
$newWidth,
362368
$newHeight,
363-
int $flags = self::FIT,
369+
int $mode = self::FIT,
370+
bool $shrinkOnly = false,
364371
): array {
372+
$shrinkOnly = $shrinkOnly || ($mode & self::SHRINK_ONLY); // back compatibility
365373
if ($newWidth === null) {
366374
} elseif (self::isPercent($newWidth)) {
367375
$newWidth = (int) round($srcWidth / 100 * abs($newWidth));
@@ -373,17 +381,17 @@ public static function calculateSize(
373381
if ($newHeight === null) {
374382
} elseif (self::isPercent($newHeight)) {
375383
$newHeight = (int) round($srcHeight / 100 * abs($newHeight));
376-
$flags |= empty($percents) ? 0 : self::STRETCH;
384+
$mode |= empty($percents) ? 0 : self::STRETCH;
377385
} else {
378386
$newHeight = abs($newHeight);
379387
}
380388

381-
if ($flags & self::STRETCH) { // non-proportional
389+
if ($mode & self::STRETCH) { // non-proportional
382390
if (!$newWidth || !$newHeight) {
383391
throw new Nette\InvalidArgumentException('For stretching must be both width and height specified.');
384392
}
385393

386-
if ($flags & self::SHRINK_ONLY) {
394+
if ($shrinkOnly) {
387395
$newWidth = (int) round($srcWidth * min(1, $newWidth / $srcWidth));
388396
$newHeight = (int) round($srcHeight * min(1, $newHeight / $srcHeight));
389397
}
@@ -401,11 +409,11 @@ public static function calculateSize(
401409
$scale[] = $newHeight / $srcHeight;
402410
}
403411

404-
if ($flags & self::FILL) {
412+
if ($mode & self::FILL) {
405413
$scale = [max($scale)];
406414
}
407415

408-
if ($flags & self::SHRINK_ONLY) {
416+
if ($shrinkOnly) {
409417
$scale[] = 1;
410418
}
411419

src/Utils/Json.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,24 @@ final class Json
2525

2626

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

3948
$json = json_encode($value, $flags);
@@ -46,11 +55,15 @@ public static function encode(mixed $value, int $flags = 0): string
4655

4756

4857
/**
49-
* Parses JSON to PHP value. The flag can be Json::FORCE_ARRAY, which forces an array instead of an object as the return value.
58+
* Parses JSON to PHP value. Parameter $forceArray forces an array instead of an object as the return value.
5059
* @throws JsonException
5160
*/
52-
public static function decode(string $json, int $flags = 0): mixed
61+
public static function decode(string $json, bool|int $forceArray = false): mixed
5362
{
63+
$flags = is_int($forceArray) // back compatibility
64+
? $forceArray
65+
: ($forceArray ? JSON_OBJECT_AS_ARRAY : 0);
66+
5467
$value = json_decode($json, null, 512, $flags | JSON_BIGINT_AS_STRING);
5568
if ($error = json_last_error()) {
5669
throw new JsonException(json_last_error_msg(), $error);

src/Utils/Strings.php

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

484484
/**
485485
* Splits a string into array by the regular expression. Parenthesized expression in the delimiter are captured.
486-
* Parameter $flags can be any combination of PREG_SPLIT_NO_EMPTY and PREG_OFFSET_CAPTURE flags.
487486
*/
488-
public static function split(string $subject, string $pattern, int $flags = 0): array
489-
{
487+
public static function split(
488+
string $subject,
489+
string $pattern,
490+
bool|int $captureOffset = false,
491+
bool $skipEmpty = false,
492+
): array {
493+
$flags = is_int($captureOffset) && $captureOffset // back compatibility
494+
? $captureOffset
495+
: ($captureOffset ? PREG_SPLIT_OFFSET_CAPTURE : 0) | ($skipEmpty ? PREG_SPLIT_NO_EMPTY : 0);
490496
return self::pcre('preg_split', [$pattern, $subject, -1, $flags | PREG_SPLIT_DELIM_CAPTURE]);
491497
}
492498

493499

494500
/**
495501
* Checks if given string matches a regular expression pattern and returns an array with first found match and each subpattern.
496-
* Parameter $flags can be any combination of PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL flags.
497502
*/
498-
public static function match(string $subject, string $pattern, int $flags = 0, int $offset = 0): ?array
499-
{
503+
public static function match(
504+
string $subject,
505+
string $pattern,
506+
bool|int $captureOffset = false,
507+
int $offset = 0,
508+
bool $unmatchedAsNull = false,
509+
): ?array {
510+
$flags = is_int($captureOffset) && $captureOffset // back compatibility
511+
? $captureOffset
512+
: ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0);
500513
if ($offset > strlen($subject)) {
501514
return null;
502515
}
@@ -508,11 +521,20 @@ public static function match(string $subject, string $pattern, int $flags = 0, i
508521

509522

510523
/**
511-
* 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).
512-
* Parameter $flags can be any combination of PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL and PREG_PATTERN_ORDER flags.
524+
* Finds all occurrences matching regular expression pattern and returns a two-dimensional array.
525+
* Result is array of matches (ie uses by default PREG_SET_ORDER).
513526
*/
514-
public static function matchAll(string $subject, string $pattern, int $flags = 0, int $offset = 0): array
515-
{
527+
public static function matchAll(
528+
string $subject,
529+
string $pattern,
530+
bool|int $captureOffset = false,
531+
int $offset = 0,
532+
bool $unmatchedAsNull = false,
533+
bool $patternOrder = false,
534+
): array {
535+
$flags = is_int($captureOffset) && $captureOffset // back compatibility
536+
? $captureOffset
537+
: ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0) | ($patternOrder ? PREG_PATTERN_ORDER : 0);
516538
if ($offset > strlen($subject)) {
517539
return [];
518540
}

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)