Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gappa committed Jun 26, 2023
1 parent 87d4d31 commit 2a8f6be
Showing 1 changed file with 60 additions and 15 deletions.
75 changes: 60 additions & 15 deletions src/ResizerParamsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,44 @@ class ResizerParamsParser
{
use SmartObject;

private const PATTERN = '~
^(?:auto| # use the image as-is, only compress/convert
(?:(ifresize)-)? # ifresize modifier
([lcr]?)([1-9][0-9]*)? # width modifer and dimension
x # divider
([tcb]?)([1-9][0-9]*)? # height modifier and dimension
(!?) # force dimensions, disregard aspect ratio
(?:-hm([+-]?[0-9]*))? # horizontal margin, unused
(?:-vm([+-]?[0-9]*))? # vertical margin, unused
(?:-q([0-9][0-9]?|100))? # quality, 0-100
)$
~x';
public const PATTERN_IR = 'ir/';
public const PATTERN_AUTO = '?:auto'; # use the image as-is, only compress/convert
public const PATTERN_IFRESIZE = '(?:(ifresize)-)?'; # ifresize modifier
public const PATTERN_WIDTH_MOD = '([lcr]?)([1-9][0-9]*)?'; # width modifer and dimension
public const PATTERN_DIVIDER = 'x'; # divider
public const PATTERN_HEIGHT_MOD = '([tcb]?)([1-9][0-9]*)?'; # height modifier and dimension
public const PATTERN_FORCE_DIMS = '(!?)'; # force dimensions, disregard aspect ratio
public const PATTERN_MARGIN_H = '(?:-hm([+-]?[0-9]*))?'; # horizontal margin, unused
public const PATTERN_MARGIN_V = '(?:-vm([+-]?[0-9]*))?'; # vertical margin, unused
public const PATTERN_MARGIN_Q = '(?:-q([0-9][0-9]?|100))?'; # quality, 0-100

public const PATTERN_PARAMS = '(' .
self::PATTERN_AUTO . '|' .
self::PATTERN_IFRESIZE .
self::PATTERN_WIDTH_MOD .
self::PATTERN_DIVIDER .
self::PATTERN_HEIGHT_MOD .
self::PATTERN_FORCE_DIMS .
self::PATTERN_MARGIN_H .
self::PATTERN_MARGIN_V .
self::PATTERN_MARGIN_Q .
')';

public const PATTERN = '~^' . self::PATTERN_PARAMS . '$~';
public const PATTERN_JS = self::PATTERN_PARAMS;

// public const PATTERN = '~
// ^(?:auto| # use the image as-is, only compress/convert
// (?:(ifresize)-)? # ifresize modifier
// ([lcr]?)([1-9][0-9]*)? # width modifer and dimension
// x # divider
// ([tcb]?)([1-9][0-9]*)? # height modifier and dimension
// (!?) # force dimensions, disregard aspect ratio
// (?:-hm([+-]?[0-9]*))? # horizontal margin, unused
// (?:-vm([+-]?[0-9]*))? # vertical margin, unused
// (?:-q([0-9][0-9]?|100))? # quality, 0-100
// )$
// ~x';

private ResizerParams $params;

Expand All @@ -45,8 +71,8 @@ public function parseParams(string $rawParams): ResizerParams
$ifresize = (bool) ($matches[1] ?? '');
$horizontal = $matches[2] ?? '';
$vertical = $matches[4] ?? '';
$width = $this->parseNumericValueToIntOrNull($matches[3] ?? '');
$height = $this->parseNumericValueToIntOrNull($matches[5] ?? '');
$width = $this->parseNumericValueToPositiveIntOrNull($matches[3] ?? '');
$height = $this->parseNumericValueToPositiveIntOrNull($matches[5] ?? '');
$suffix = $matches[6] ?? '';
$horizontalMargin = $matches[7] ?? '';
$verticalMargin = $matches[8] ?? '';
Expand All @@ -72,13 +98,32 @@ public function getParams(): ResizerParams
}


/** @return int|null */
/** @return int<0,100>|null */
private function parseNumericValueToIntOrNull(string $value): ?int
{
if (strlen($value) === 0) {
return null;
}

if (is_numeric($value)) {
$int = (int) $value;

if ($int >= 0 && $int <= 100) {
return $int;
}
}

return null;
}


/** @return positive-int|null */
private function parseNumericValueToPositiveIntOrNull(string $value): ?int
{
if (strlen($value) === 0) {
return null;
}

if (is_numeric($value)) {
$int = (int) $value;

Expand Down

0 comments on commit 2a8f6be

Please sign in to comment.