diff --git a/src/Optimole.php b/src/Optimole.php index a7665b1..1ef8403 100644 --- a/src/Optimole.php +++ b/src/Optimole.php @@ -33,7 +33,7 @@ final class Optimole /** * The Optimole SDK version. */ - public const VERSION = '1.2.1'; + public const VERSION = '1.2.2'; /** * The Optimole dashboard API URL. diff --git a/src/Resource/Image.php b/src/Resource/Image.php index 9618ed9..c509b12 100644 --- a/src/Resource/Image.php +++ b/src/Resource/Image.php @@ -17,6 +17,16 @@ class Image extends AbstractResource { + /** + * Set the dpr of the optimized image. + */ + public function dpr($dpr = 1): self + { + $this->addProperty(new ImageProperty\DprProperty($dpr)); + + return $this; + } + /** * Convert the optimized image to the given format. */ diff --git a/src/Resource/ImageProperty/DprProperty.php b/src/Resource/ImageProperty/DprProperty.php new file mode 100644 index 0000000..f65f149 --- /dev/null +++ b/src/Resource/ImageProperty/DprProperty.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Optimole\Sdk\Resource\ImageProperty; + +use Optimole\Sdk\Exception\InvalidArgumentException; +use Optimole\Sdk\Resource\PropertyInterface; + +class DprProperty implements PropertyInterface +{ + /** + * The dpr of the image. + */ + private $dpr; + + /** + * Constructor. + */ + public function __construct($dpr = 1) + { + if (!is_int($dpr) || $dpr < 1) { + throw new InvalidArgumentException('Image dpr must be an integer greater or equal than 1.'); + } + $dpr = max(1, min(5, $dpr)); + + $this->dpr = $dpr; + } + + /** + * {@inheritdoc} + */ + public function __toString(): string + { + return sprintf('dpr:%s', $this->dpr); + } +} diff --git a/tests/Unit/Resource/ImageTest.php b/tests/Unit/Resource/ImageTest.php index 8bbc453..82c7d09 100644 --- a/tests/Unit/Resource/ImageTest.php +++ b/tests/Unit/Resource/ImageTest.php @@ -19,6 +19,27 @@ class ImageTest extends TestCase { + public function testDprAddsDprPropriety(): void + { + $image = (new Image('domain', 'source'))->dpr(2); + + $this->assertSame('https://domain/dpr:2/source', (string) $image); + } + + public function testDprAddsDprProprietyDefault(): void + { + $image = (new Image('domain', 'source'))->dpr(1); + + $this->assertSame('https://domain/dpr:1/source', (string) $image); + } + + public function testDprAddsDprProprietyHigh(): void + { + $image = (new Image('domain', 'source'))->dpr(10); + + $this->assertSame('https://domain/dpr:5/source', (string) $image); + } + public function testFormatAddsFormatProperty(): void { $image = (new Image('domain', 'source'))->format('webp');