Skip to content

Commit 567557f

Browse files
authored
Merge pull request #2 from Codeinwp/feat/dpr
add dpr compatibility
2 parents 72d96fe + 0fd2c65 commit 567557f

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

src/Optimole.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class Optimole
3333
/**
3434
* The Optimole SDK version.
3535
*/
36-
public const VERSION = '1.2.1';
36+
public const VERSION = '1.2.2';
3737

3838
/**
3939
* The Optimole dashboard API URL.

src/Resource/Image.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717

1818
class Image extends AbstractResource
1919
{
20+
/**
21+
* Set the dpr of the optimized image.
22+
*/
23+
public function dpr($dpr = 1): self
24+
{
25+
$this->addProperty(new ImageProperty\DprProperty($dpr));
26+
27+
return $this;
28+
}
29+
2030
/**
2131
* Convert the optimized image to the given format.
2232
*/
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Optimole PHP SDK.
7+
*
8+
* (c) Optimole Team <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Optimole\Sdk\Resource\ImageProperty;
15+
16+
use Optimole\Sdk\Exception\InvalidArgumentException;
17+
use Optimole\Sdk\Resource\PropertyInterface;
18+
19+
class DprProperty implements PropertyInterface
20+
{
21+
/**
22+
* The dpr of the image.
23+
*/
24+
private $dpr;
25+
26+
/**
27+
* Constructor.
28+
*/
29+
public function __construct($dpr = 1)
30+
{
31+
if (!is_int($dpr) || $dpr < 1) {
32+
throw new InvalidArgumentException('Image dpr must be an integer greater or equal than 1.');
33+
}
34+
$dpr = max(1, min(5, $dpr));
35+
36+
$this->dpr = $dpr;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function __toString(): string
43+
{
44+
return sprintf('dpr:%s', $this->dpr);
45+
}
46+
}

tests/Unit/Resource/ImageTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@
1919

2020
class ImageTest extends TestCase
2121
{
22+
public function testDprAddsDprPropriety(): void
23+
{
24+
$image = (new Image('domain', 'source'))->dpr(2);
25+
26+
$this->assertSame('https://domain/dpr:2/source', (string) $image);
27+
}
28+
29+
public function testDprAddsDprProprietyDefault(): void
30+
{
31+
$image = (new Image('domain', 'source'))->dpr(1);
32+
33+
$this->assertSame('https://domain/dpr:1/source', (string) $image);
34+
}
35+
36+
public function testDprAddsDprProprietyHigh(): void
37+
{
38+
$image = (new Image('domain', 'source'))->dpr(10);
39+
40+
$this->assertSame('https://domain/dpr:5/source', (string) $image);
41+
}
42+
2243
public function testFormatAddsFormatProperty(): void
2344
{
2445
$image = (new Image('domain', 'source'))->format('webp');

0 commit comments

Comments
 (0)