Skip to content

Commit 7309e5e

Browse files
committed
feat: Add image aspect ratio to generic template
1 parent 15babbd commit 7309e5e

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

src/Messages/GenericTemplate.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@
1717
*/
1818
class GenericTemplate extends Template
1919
{
20+
/**
21+
* Horizontal image
22+
*/
23+
const IMAGE_HORIZONTAL = 'horizontal';
24+
/**
25+
* Square image
26+
*/
27+
const IMAGE_SQUARE = 'square';
28+
29+
/**
30+
* @var string
31+
*/
32+
private $imageRatio = self::IMAGE_HORIZONTAL;
33+
2034
/**
2135
* Generic constructor.
2236
*
@@ -42,6 +56,28 @@ public function toData()
4256
return parent::toData();
4357
}
4458

59+
/**
60+
* Set image aspect ratio
61+
*
62+
* @param $value
63+
* @return $this
64+
*/
65+
public function setImageRatio($value)
66+
{
67+
$this->imageRatio = $value;
68+
69+
return $this;
70+
}
71+
72+
/**
73+
* Get image ratio
74+
* @return string
75+
*/
76+
public function getImageRatio()
77+
{
78+
return $this->imageRatio;
79+
}
80+
4581
/**
4682
* @return mixed
4783
*/

src/Transformers/GenericTransformer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Casperlaitw\LaravelFbMessenger\Transformers;
99

1010
use Casperlaitw\LaravelFbMessenger\Contracts\Messages\Template;
11+
use Casperlaitw\LaravelFbMessenger\Messages\GenericTemplate;
1112

1213
/**
1314
* Class GenericTransformer
@@ -18,14 +19,15 @@ class GenericTransformer implements StructuredTransformer
1819
/**
1920
* Transform payload
2021
*
21-
* @param Template $message
22+
* @param Template|GenericTemplate $message
2223
*
2324
* @return array
2425
*/
2526
public function transform(Template $message)
2627
{
2728
return [
2829
'template_type' => 'generic',
30+
'image_aspect_ratio' => $message->getImageRatio(),
2931
'elements' => $message->getCollections()->toData()
3032
];
3133
}

tests/Messages/GenericTemplateTest.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ public function test_to_data()
3939
'type' => 'template',
4040
'payload' => [
4141
'template_type' => 'generic',
42-
'elements' => $elementExpected
42+
'elements' => $elementExpected,
43+
'image_aspect_ratio' => 'horizontal',
4344
],
44-
]
45+
],
4546
],
4647
];
4748

@@ -67,8 +68,9 @@ public function test_disable_share()
6768
'template_type' => 'generic',
6869
'elements' => $elementExpected,
6970
'sharable' => false,
71+
'image_aspect_ratio' => 'horizontal',
7072
],
71-
]
73+
],
7274
],
7375
];
7476

@@ -77,4 +79,32 @@ public function test_disable_share()
7779

7880
$this->assertEquals($expected, $actual->toData());
7981
}
82+
83+
public function test_image_ratio()
84+
{
85+
$elementExpected = [];
86+
foreach ($this->case as $case) {
87+
$elementExpected[] = $case->toData();
88+
}
89+
$expected = [
90+
'recipient' => [
91+
'id' => $this->sender,
92+
],
93+
'message' => [
94+
'attachment' => [
95+
'type' => 'template',
96+
'payload' => [
97+
'template_type' => 'generic',
98+
'elements' => $elementExpected,
99+
'image_aspect_ratio' => 'square',
100+
],
101+
],
102+
],
103+
];
104+
105+
$actual = new GenericTemplate($this->sender, $this->case);
106+
$actual->setImageRatio(GenericTemplate::IMAGE_SQUARE);
107+
108+
$this->assertEquals($expected, $actual->toData());
109+
}
80110
}

tests/Transformers/GenericTransformerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function test_transform()
3131

3232
$expected = [
3333
'template_type' => 'generic',
34+
'image_aspect_ratio' => 'horizontal',
3435
'elements' => $expectedCase,
3536
];
3637

@@ -46,6 +47,7 @@ private function createMessageMock($testCase, $testSender)
4647
$message = m::mock(Template::class)
4748
->shouldReceive('getSender')->andReturn($testSender)
4849
->shouldReceive('getCollections')->andReturn($elements)
50+
->shouldReceive('getImageRatio')->andReturn('horizontal')
4951
->getMock();
5052

5153
return $message;

0 commit comments

Comments
 (0)