File tree Expand file tree Collapse file tree 4 files changed +78
-1
lines changed Expand file tree Collapse file tree 4 files changed +78
-1
lines changed Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff line change 1717
1818class 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 */
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1919
2020class 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 ' );
You can’t perform that action at this time.
0 commit comments