Skip to content

Commit

Permalink
Scrutinizer bugs (#13)
Browse files Browse the repository at this point in the history
* fix bugs and complexity

* Fix styling

---------

Co-authored-by: jeremytubbs <jeremytubbs@users.noreply.github.com>
  • Loading branch information
jeremytubbs and jeremytubbs authored Mar 13, 2023
1 parent 24e4f59 commit ef81326
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[![Latest Version on Packagist](https://img.shields.io/packagist/v/conlect/image-iiif.svg?style=flat-square)](https://packagist.org/packages/conlect/image-iiif)
[![Tests](https://github.com/conlect/image-iiif/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/conlect/image-iiif/actions/workflows/run-tests.yml)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/conlect/image-iiif/badges/quality-score.png?b=main)](https://scrutinizer-ci.com/g/conlect/image-iiif/?branch=main)
[![Total Downloads](https://img.shields.io/packagist/dt/conlect/image-iiif.svg?style=flat-square)](https://packagist.org/packages/conlect/image-iiif)

# Image IIIF
Expand Down
8 changes: 4 additions & 4 deletions src/Filters/RegionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public function applyFilter(Image $image)

if (strpos($this->options[0], 'pct:') === false) {
// iiif - x,y,w,h
$x = $this->options[0];
$y = $this->options[1];
$w = $this->options[2];
$h = $this->options[3];
$x = (int) $this->options[0];
$y = (int) $this->options[1];
$w = (int) $this->options[2];
$h = (int) $this->options[3];

if (($x + $w) > $this->width) {
$w = $w - (($x + $w) - $this->width);
Expand Down
15 changes: 11 additions & 4 deletions src/ImageIIIF.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ public function hasValidParameters(array $parameters)

if (! (new $validators[$parameter]($this->config, $this->image))->valid($value)) {
return false;

break;
}
}

Expand All @@ -101,7 +99,7 @@ public function info($prefix = 'iiif', $identifier)
'scaleFactors' => $this->getScaleFactors(),
],
],
'extraFormats' => array_keys($this->config['mime']),
'extraFormats' => $this->getExtraFormats(),
'extraQualities' => $this->config['qualities'],
'extraFeatures' => $this->config['supports'],
];
Expand All @@ -111,7 +109,7 @@ protected function getScaleFactors()
{
$scaleFactors = [];
$maxSize = max($this->image->width(), $this->image->height());
$total = (int) ceil($maxSize / $this->config->get('tile_width'));
$total = (int) ceil($maxSize / $this->config['tile_width']);
$factor = 1;
while ($factor / 2 <= $total) {
$scaleFactors[] = $factor;
Expand All @@ -123,4 +121,13 @@ protected function getScaleFactors()

return $scaleFactors;
}

protected function getExtraFormats()
{
if (is_array($this->config['mime'])) {
return array_keys($this->config['mime']);
}

return [];
}
}
55 changes: 36 additions & 19 deletions src/Validators/SizeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,69 @@

class SizeValidator extends ValidatorAbstract implements ValidatorInterface
{
protected $allow_upscaling;
protected $upscale;
protected $regex = [
'^max$',
'^\^max$',
',[0-9]+',
'\^,[0-9]+',
'[0-9]+,',
'\^[0-9]+,',
'{!}?[0-9]+,[0-9]+',
];

public function __construct($config)
{
$this->allow_upscaling = $config['allow_upscaling'];
}

public function valid($value)
{
$startValue = $value;
if ($value === 'max') {
return true;
}

if (str_starts_with($value, '^') && $this->config['allow_upscaling'] === false) {
if ($value === '^max') {
throw new NotImplementedException("Maximum size is not implemented.");
}

if (! $this->allow_upscaling && $this->upscale) {
throw new NotImplementedException("Upscaling is not allowed.");
}

if (strpos($value, '^pct:') !== false) {
$value = preg_replace('/^\^pct:/', '', $value);
$this->upscale = str_starts_with($value, '^');
$isPercent = str_contains($value, 'pct:');
$percent_value = (int)$this->getPercentValue($value);

if ((int)$value == 0) {
throw new BadRequestException("Size $startValue is invalid.");
if ($isPercent) {
if ($percent_value == 0 || $percent_value < 1) {
throw new BadRequestException("Size $value is invalid.");
}
if ((int)$value >= 1) {
if ($this->upscale) {
return true;
}
}

if (strpos($value, 'pct:') !== false) {
$value = preg_replace('/pct:/', '', $value);

if ((int)$value == 0 || (int)$value > 100) {
throw new BadRequestException("Size $startValue is invalid.");
if ($percent_value > 100) {
throw new BadRequestException("Size $value is invalid.");
}
if ((int)$value >= 1 && (int)$value <= 100) {

if ($percent_value <= 100) {
return true;
}
}

$all_regex = implode('|', $this->regex);

if (preg_match('/' . $all_regex . '/', $value)) {
return true;
}

throw new BadRequestException("Size $startValue is invalid.");
throw new BadRequestException("Size $value is invalid.");
}

protected function getPercentValue($value)
{
if ($this->upscale) {
return preg_replace('/^\^pct:/', '', $value);
}

return preg_replace('/pct:/', '', $value);
}
}
15 changes: 13 additions & 2 deletions tests/SizeValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Conlect\ImageIIIF\Tests;

use Conlect\ImageIIIF\Exceptions\BadRequestException;
use Conlect\ImageIIIF\Exceptions\NotImplementedException;
use Conlect\ImageIIIF\Validators\SizeValidator;
use Noodlehaus\Config;
use PHPUnit\Framework\TestCase;
Expand All @@ -17,8 +18,6 @@ public function it_validates_size()

// max
$this->assertTrue($sizeValidator->valid('max'));
// ^max
$this->assertTrue($sizeValidator->valid('^max'));
// w,
$this->assertTrue($sizeValidator->valid('20,'));
// ^w,
Expand Down Expand Up @@ -103,4 +102,16 @@ public function it_throws_exception_full()
$this->expectExceptionMessage("Size $size is invalid.");
$sizeValidator->valid($size);
}

/** @test */
public function it_throws_exception_max_size()
{
$size = '^max';
$config = new Config(__DIR__ . '/../config');
$sizeValidator = new SizeValidator($config);
$this->expectException(NotImplementedException::class);
$this->expectExceptionCode(501);
$this->expectExceptionMessage("Maximum size is not implemented.");
$sizeValidator->valid($size);
}
}

0 comments on commit ef81326

Please sign in to comment.