Skip to content

Commit f8ca66a

Browse files
committed
fix method chaining of convenience methods
1 parent 7065619 commit f8ca66a

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
All Notable changes to `laravel-medialibrary` will be documented in this file
44

5-
##3.1.3
5+
##3.1.4
6+
- Fixed a bug where chaining the conversion convenience methods would not give the right result
7+
8+
##3.1.3gti i
69
- Fixed a bug where getByModelType would return null
710

811
##3.1.2

src/Conversion/Conversion.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,17 @@ public function setRectangle($width, $height, $x, $y)
321321
*/
322322
public function setManipulationParameter($name, $value)
323323
{
324-
$this->manipulations[count($this->manipulations)][$name] = $value;
324+
if (count($this->manipulations) == 0) {
325+
$this->manipulations[0] = [];
326+
};
327+
328+
$lastIndex = count($this->manipulations) - 1;
329+
330+
if (!isset($this->manipulations[$lastIndex])) {
331+
$this->manipulations[$lastIndex] = [];
332+
}
333+
334+
$this->manipulations[$lastIndex] = array_merge($this->manipulations[$lastIndex], [$name => $value]);
325335

326336
return $this;
327337
}

src/MediaRepository.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public function all()
122122
* Get all media for the given type.
123123
*
124124
* @param string $modelType
125+
*
125126
* @return \Illuminate\Database\Eloquent\Collection
126127
*/
127128
public function getByModelType($modelType)

tests/Conversion/ConversionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,16 @@ public function it_can_add_a_parameter_to_a_manipulation()
248248
$this->assertEquals('value', $this->conversion->getManipulations()[0]['name']);
249249
$this->assertInstanceOf(\Spatie\MediaLibrary\Conversion\Conversion::class, $conversion);
250250
}
251+
252+
/**
253+
* @test
254+
*/
255+
public function it_can_chain_the_convenience_methods()
256+
{
257+
$conversion = $this->conversion->setWidth(75)->setHeight(75)->setFit('crop')->setFormat('jpg');
258+
259+
$otherConversions = (new Conversion('other'))->setManipulations(['w' => 75, 'h' => 75, 'fit' => 'crop', 'fm' => 'jpg']);
260+
261+
$this->assertEquals($conversion->getManipulations(), $otherConversions->getManipulations());
262+
}
251263
}

0 commit comments

Comments
 (0)