Skip to content

Commit

Permalink
Merge branch 'develop-patch' into develop-minor
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Jan 31, 2024
2 parents 7cf9069 + bd06e83 commit 5c46047
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config/sections/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
'props' => [
/**
* Array or query string for reports. Each report needs a `label` and `value` and can have additional `info`, `link` and `theme` settings.
* Array or query string for reports. Each report needs a `label` and `value` and can have additional `info`, `link`, `icon` and `theme` settings.
*/
'reports' => function ($reports = null) {
if ($reports === null) {
Expand Down
8 changes: 8 additions & 0 deletions src/Cms/FileModifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ public function resize(
]);
}

/**
* Sharpens the image
*/
public function sharpen(int $amount = 50): FileVersion|File|Asset
{
return $this->thumb(['sharpen' => $amount]);
}

/**
* Create a srcset definition for the given sizes
* Sizes can be defined as a simple array. They can
Expand Down
9 changes: 9 additions & 0 deletions src/Filesystem/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,15 @@ public function match(array $rules): bool
if (is_array($rules['mime'] ?? null) === true) {
$mime = $this->mime();

// the MIME type could not be determined, but matching
// to it was requested explicitly
if ($mime === null) {
throw new Exception([
'key' => 'file.mime.missing',
'data' => ['filename' => $this->filename()]
]);
}

// determine if any pattern matches the MIME type;
// once any pattern matches, `$carry` is `true` and the rest is skipped
$matches = array_reduce(
Expand Down
11 changes: 10 additions & 1 deletion src/Toolkit/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,16 @@ public static function excerpt(
return $string;
}

return static::substr($string, 0, mb_strrpos(static::substr($string, 0, $chars), ' ')) . $rep;
// shorten the string to the specified number of characters,
// but make sure to not cut off in the middle of a word
$excerpt = static::substr($string, 0, $chars);
$cutoff = mb_strrpos($excerpt, ' ');

if ($cutoff !== false) {
$excerpt = static::substr($string, 0, $cutoff);
}

return $excerpt . $rep;
}

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/Cms/Files/FileModificationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,34 @@ public function testResize()
$file = $app->file('test.jpg');
$file->resize(100, 200, 10);
}

public function testSharpen()
{
$app = $this->app->clone([
'components' => [
'file::version' => function ($kirby, $file, $options = []) {
$this->assertSame(['sharpen' => 50], $options);
return $file;
}
]
]);

$file = $app->file('test.jpg');
$file->sharpen();
}

public function testSharpenWithCustomValue()
{
$app = $this->app->clone([
'components' => [
'file::version' => function ($kirby, $file, $options = []) {
$this->assertSame(['sharpen' => 20], $options);
return $file;
}
]
]);

$file = $app->file('test.jpg');
$file->sharpen(20);
}
}
19 changes: 15 additions & 4 deletions tests/Filesystem/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,20 @@ public function testMatch()
}

/**
* @covers \Kirby\Filesystem\File::match
* @covers ::match
*/
public function testMatchMimeException()
public function testMatchMimeMissing()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('The media type for "doesnotexist.invalid" cannot be detected');

$this->_file('doesnotexist.invalid')->match(['mime' => ['image/png', 'application/pdf']]);
}

/**
* @covers ::match
*/
public function testMatchMimeInvalid()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid mime type: text/plain');
Expand All @@ -392,7 +403,7 @@ public function testMatchMimeException()
}

/**
* @covers \Kirby\Filesystem\File::match
* @covers ::match
*/
public function testMatchExtensionException()
{
Expand All @@ -406,7 +417,7 @@ public function testMatchExtensionException()
}

/**
* @covers \Kirby\Filesystem\File::match
* @covers ::match
*/
public function testMatchTypeException()
{
Expand Down
12 changes: 12 additions & 0 deletions tests/Toolkit/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,18 @@ public function testExcerptWithSpaces()
$this->assertSame($expected, $result);
}

/**
* @covers ::excerpt
*/
public function testExcerptWithoutSpaces()
{
$string = 'ThisIsALongTextWithSomeHtml';
$expected = 'ThisIsALongText …';
$result = Str::excerpt($string, 15);

$this->assertSame($expected, $result);
}

/**
* @covers ::excerpt
*/
Expand Down

0 comments on commit 5c46047

Please sign in to comment.