Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: validate custom srcsets #61

Merged
merged 3 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Imgix/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Imgix;

use Imgix\Validator;

class UrlBuilder {

private $currentVersion = "3.2.0";
Expand Down Expand Up @@ -82,6 +84,7 @@ public function createSrcSet($path, $params=array(), $options=array()) {
$widthsArray = isset($options['widths']) ? $options['widths'] : NULL;

if (isset($widthsArray)) {
Validator::validateWidths($widthsArray);
return $this->createSrcSetPairs($path, $params=$params, $widthsArray);
}

Expand Down Expand Up @@ -130,6 +133,7 @@ public function targetWidths(
return array((int) $start);
}

Validator::validateMinMaxTol($start, $stop, $tol);
$resolutions = array();

while ($start < $stop && $start < self::MAX_WIDTH) {
Expand Down
60 changes: 60 additions & 0 deletions src/Imgix/Validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Imgix;

class Validator {
const ONE_PERCENT = 0.01;

public static function validateMinWidth($start) {
if ($start < 0) {
throw new \InvalidArgumentException("`start` width value must be greater than zero");
}
}

public static function validateMaxWidth($end) {
if ($end < 0) {
throw new \InvalidArgumentException("`stop` width value must be greater than zero");
}
}

public static function validateRange($start, $stop) {
// Validate the minimum width, `begin`.
Validator::validateMinWidth($start);
// Validate the maximum width, `end`.
Validator::validateMaxWidth($stop);

// Ensure that the range is valid, ie. `begin <= end`.
if ($start > $stop) {
throw new \InvalidArgumentException("`start` width value must be less than `stop` width value");
}
}

public static function validateTolerance($tol) {
$msg = "`tol`erance value must be greater than, or equal to one percent, ie. >= 0.01";

if ($tol < self::ONE_PERCENT) {
throw new \InvalidArgumentException($msg);
}
}

public static function validateMinMaxTol($begin, $end, $tol) {
Validator::validateRange($begin, $end);
Validator::validateTolerance($tol);
}

public static function validateWidths($widths) {
if ($widths == NULL) {
throw new \InvalidArgumentException("`widths` array cannot be `null`");
}

if (count($widths) == 0) {
throw new \InvalidArgumentException("`widths` array cannot be empty");
}
foreach ($widths as &$w) {
if ($w < 0) {
throw new \InvalidArgumentException("width values in `widths` cannot be negative");
}
}
}
}
?>
63 changes: 63 additions & 0 deletions tests/Imgix/Tests/ValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

use Imgix\Validator;

class ValidatorTest extends \PHPUnit\Framework\TestCase {
const LESS_THAN_ZERO = -1;
/**
* Test `validateMinWidth` throws if passed a value less than
* zero.
*/
public function testValidateMinWidth() {
$this->expectException(InvalidArgumentException::class);
Validator::validateMinWidth(self::LESS_THAN_ZERO);
}

/**
* Test `validateMaxWidth` throws if passed a value less than
* zero.
*/
public function testValidateMaxWidth() {
$this->expectException(InvalidArgumentException::class);
Validator::validateMaxWidth(self::LESS_THAN_ZERO);
}

/**
* Test `validateRange` throws if passed an invalid range,
* ie. if `BEGIN > END`.
*/
public function testValidateRange() {
$this->expectException(InvalidArgumentException::class);
$begin = 400;
$end = 100;
Validator::validateRange($begin, $end);
}

/**
* Test `validateTolerance` throws if passed a `tol`erance
* that is less than one percent.
*/
public function testValidateTolerance() {
$this->expectException(InvalidArgumentException::class);
$lessThanOnePercent = 0.001;
Validator::validateTolerance($lessThanOnePercent);
}


/**
* Test `validateWidths` throws if passed a `null` array.
*/
public function testValidateWidthsNullArray() {
$this->expectException(InvalidArgumentException::class);
Validator::validateWidths(NULL);
}

/**
* Test `validateWidths` throws if passed an empty array.
*/
public function testValidateWidthsEmptyArray() {
$this->expectException(InvalidArgumentException::class);
Validator::validateWidths([]);
}
}
?>