Skip to content

Commit c68b588

Browse files
committed
reduce usage of Nette Utils + integration test for Strings util
1 parent 663c998 commit c68b588

3 files changed

Lines changed: 50 additions & 11 deletions

File tree

src/Model/Entity/Asset.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Cake\ORM\Entity;
1212
use Cake\Routing\Router;
1313
use League\Csv\Reader;
14-
use Nette\Utils\Arrays;
1514
use Nette\Utils\FileSystem;
1615
use Nette\Utils\Strings;
1716

@@ -125,14 +124,22 @@ public function isViewableInBrowser(): bool
125124
return false;
126125
}
127126

128-
return Arrays::contains([
129-
'image',
130-
'video',
131-
], Strings::before((string)$this->mimetype, '/'))
132-
|| Arrays::contains([
133-
'pdf',
134-
'json',
135-
], Strings::after((string)$this->mimetype, '/'));
127+
return in_array(
128+
Strings::before((string)$this->mimetype, '/'),
129+
[
130+
'image',
131+
'video',
132+
],
133+
true
134+
) ||
135+
in_array(
136+
Strings::after((string)$this->mimetype, '/'),
137+
[
138+
'pdf',
139+
'json',
140+
],
141+
true
142+
);
136143
}
137144

138145
/**
@@ -144,7 +151,7 @@ public function isImage(): bool
144151
{
145152
return $this->exists()
146153
&& Strings::before($this->mimetype ?? '', '/') === 'image'
147-
&& !Strings::contains((string)Strings::after($this->mimetype ?? '', '/'), 'svg');
154+
&& !str_contains((string)Strings::after($this->mimetype ?? '', '/'), 'svg');
148155
}
149156

150157
/**

src/Utilities/ImageAsset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ private function applyModifications(Image $image, ImageManager $manager): Image
433433
unset($modifications['noApi']);
434434

435435
foreach ($modifications as $method => $params) {
436-
if (Strings::contains($method, 'filter_')) {
436+
if (str_contains($method, 'filter_')) {
437437
$filterClassName = Strings::after($method, 'filter_') ?? '';
438438
if (!class_exists($filterClassName)) {
439439
throw new FilterNotFoundException("Filter {$filterClassName} does not exist. ");
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Assets\Test\integration\Nette;
5+
6+
use Cake\TestSuite\TestCase;
7+
use Nette\Utils\Strings;
8+
9+
class StringsTest extends TestCase
10+
{
11+
public function testStrings(): void
12+
{
13+
$string = 'some_string-with/random7stuff.here';
14+
15+
$some_string = Strings::before($string, '-');
16+
static::assertEquals('some_string', $some_string);
17+
18+
$here = Strings::after($string, '.', -1);
19+
static::assertEquals('here', $here);
20+
21+
$random = Strings::before(Strings::after($string, '/'), '7');
22+
static::assertEquals('random', $random);
23+
24+
$some_string = Strings::substring($string, 0, 11);
25+
static::assertEquals('some_string', $some_string);
26+
$ome_str = Strings::substring($string, 1, 7);
27+
static::assertEquals('ome_str', $ome_str);
28+
29+
$someDotDotDot = Strings::truncate($string, 5);
30+
static::assertEquals("some\u{2026}", $someDotDotDot);
31+
}
32+
}

0 commit comments

Comments
 (0)