Skip to content

Commit cd18759

Browse files
authored
Allow providing Gravity object directly (#8)
1 parent e02abca commit cd18759

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

src/Options/Crop.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ final class Crop extends AbstractOption
1010
private Height $height;
1111
private ?Gravity $gravity = null;
1212

13-
public function __construct(int $width, int $height, ?string $gravity = null)
13+
public function __construct(int $width, int $height, Gravity|string|null $gravity = null)
1414
{
1515
$this->width = new Width($width);
1616
$this->height = new Height($height);
17-
18-
if ($gravity !== null) {
19-
$this->gravity = Gravity::fromString($gravity);
20-
}
17+
$this->gravity = is_string($gravity) ? Gravity::fromString($gravity) : $gravity;
2118
}
2219

2320
/**

src/Options/Extend.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ final class Extend extends AbstractOption
99
private bool $extend;
1010
private ?Gravity $gravity = null;
1111

12-
public function __construct(bool $extend = true, ?string $gravity = null)
12+
public function __construct(bool $extend = true, Gravity|string|null $gravity = null)
1313
{
1414
$this->extend = $extend;
15-
16-
if ($gravity !== null) {
17-
$this->gravity = Gravity::fromString($gravity);
18-
}
15+
$this->gravity = is_string($gravity) ? Gravity::fromString($gravity) : $gravity;
1916
}
2017

2118
/**

tests/Options/CropTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
namespace Onliner\ImgProxy\Options;
66

7+
use Onliner\ImgProxy\Support\GravityType;
78
use PHPUnit\Framework\TestCase;
89

910
class CropTest extends TestCase
1011
{
1112
/**
1213
* @dataProvider validData
1314
*/
14-
public function testCreate(int $width, int $height, ?string $gravity, string $expected): void
15+
public function testCreate(int $width, int $height, Gravity|string|null $gravity, string $expected): void
1516
{
1617
$opt = new Crop($width, $height, $gravity);
1718
$this->assertSame($expected, (string) $opt);
@@ -61,8 +62,10 @@ public function validData(): array
6162
[100, 200, null, 'c:100:200'],
6263
[100, 0, 'ce', 'c:100:0:ce'],
6364
[100, 200, 'ce', 'c:100:200:ce'],
65+
[100, 200, new Gravity(GravityType::CENTER), 'c:100:200:ce'],
6466
[100, 200, 'ce:0:0', 'c:100:200:ce:0:0'],
6567
[100, 200, 'ce:200:250', 'c:100:200:ce:200:250'],
68+
[100, 200, new Gravity(GravityType::CENTER, 200, 250), 'c:100:200:ce:200:250'],
6669
];
6770
}
6871

tests/Options/ExtendTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
namespace Onliner\ImgProxy\Options;
66

7+
use Onliner\ImgProxy\Support\GravityType;
78
use PHPUnit\Framework\TestCase;
89

910
class ExtendTest extends TestCase
1011
{
1112
/**
1213
* @dataProvider exampleData
1314
*/
14-
public function testCreate(bool $extend, ?string $gravity, string $expected): void
15+
public function testCreate(bool $extend, Gravity|string|null $gravity, string $expected): void
1516
{
1617
$opt = new Extend($extend, $gravity);
1718

@@ -45,8 +46,10 @@ public function exampleData(): array
4546
[true, null, 'ex:1'],
4647
[false, null, 'ex:0'],
4748
[true, 'ce', 'ex:1:ce'],
49+
[true, new Gravity(GravityType::CENTER), 'ex:1:ce'],
4850
[false, 'ce:0:0', 'ex:0:ce:0:0'],
4951
[false, 'ce:200:250', 'ex:0:ce:200:250'],
52+
[false, new Gravity(GravityType::CENTER, 200, 250), 'ex:0:ce:200:250'],
5053
];
5154
}
5255

0 commit comments

Comments
 (0)