From ab9964bf926a3ef876ec26885c2c8e03093f078a Mon Sep 17 00:00:00 2001 From: selul Date: Fri, 28 Mar 2025 20:29:33 +0200 Subject: [PATCH 1/2] add dpr compatibility --- src/Resource/Image.php | 10 +++++ src/Resource/ImageProperty/DprProperty.php | 46 ++++++++++++++++++++++ tests/Unit/Resource/ImageTest.php | 21 ++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/Resource/ImageProperty/DprProperty.php 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'); From 0fd2c65c9538b1db40c21f71518e48d71670d683 Mon Sep 17 00:00:00 2001 From: selul Date: Fri, 28 Mar 2025 20:30:27 +0200 Subject: [PATCH 2/2] bump version --- src/Optimole.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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.