Skip to content

Commit 2fe8d7f

Browse files
authored
Add Image aspect ratio helper (#51)
* Add image_aspect_ratio to generic template [square] * Add image_aspect_ratio param * Add ImageAspectRatio Enum * Add description of imageAspectRatio to README * Fix styling
1 parent cea6b4a commit 2fe8d7f

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public function routeNotificationForFacebook()
165165
- `buttons($buttons = [])`: (array) An array of "Call to Action" buttons (Created using `NotificationChannels\Facebook\Components\Button::create()`). You can add up to 3 buttons of one of the following types: `web_url`, `postback` or `phone_number`. See Button methods below for more details.
166166
- `cards($cards = [])`: (array) An array of item cards to be displayed in a carousel (Created using `NotificationChannels\Facebook\Components\Card::create()`). You can add up to 10 cards. See Card methods below for more details.
167167
- `notificationType('')`: (string) Push Notification type: `REGULAR` will emit a sound/vibration and a phone notification; `SILENT_PUSH` will just emit a phone notification, `NO_PUSH` will not emit either. You can make use of `NotificationType::REGULAR`, `NotificationType::SILENT_PUSH` and `NotificationType::NO_PUSH` to make it easier to work with the type. This is an optional method, defaults to `REGULAR` type.
168+
- `imageAspectRatio('')`: (string) Image Aspect Ratio if Card with `image_url` present. You can use of `ImageAspectRatioType::SQUARE` or `ImageAspectRatioType::HORIZONTAL`. This is an optional method, defaults to `ImageAspectRatioType::HORIZONTAL` aspect ratio (image should be 1.91:1).
168169
- `isTypeRegular()`: Helper method to create a notification type: `REGULAR`.
169170
- `isTypeSilentPush()`: Helper method to create a notification type: `SILENT_PUSH`.
170171
- `isTypeNoPush()`: Helper method to create a notification type: `NO_PUSH`.

src/Components/Card.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function url(string $itemUrl): self
7979
/**
8080
* Set Card Image Url.
8181
*
82-
* @param string $imageUrl Image ratio should be 1.91:1
82+
* @param string $imageUrl Default image ratio is 1.91:1
8383
*
8484
* @return $this
8585
*/

src/Enums/ImageAspectRatioType.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace NotificationChannels\Facebook\Enums;
4+
5+
/**
6+
* Class ImageAspectRatioType.
7+
*
8+
* The aspect ratio used to render images specified by element.image_url
9+
*
10+
* @see https://developers.facebook.com/docs/messenger-platform/reference/templates/generic#payload
11+
*/
12+
class ImageAspectRatioType
13+
{
14+
/**
15+
* Aspect ratio of image should be 1.91:1.
16+
*/
17+
public const HORIZONTAL = 'horizontal';
18+
/**
19+
* Aspect ratio of image should be 1:1.
20+
*/
21+
public const SQUARE = 'square';
22+
}

src/Exceptions/CouldNotCreateMessage.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ public static function textTooLong(): self
1919
return new static('Message text is too long, A 320 character limited string should be provided.');
2020
}
2121

22+
/**
23+
* Thrown when invalid image aspect ratio provided.
24+
*
25+
* @return static
26+
*/
27+
public static function invalidImageAspectRatio(): self
28+
{
29+
return new static('Image Aspect Ratio provided is invalid.');
30+
}
31+
2232
/**
2333
* Thrown when invalid notification type provided.
2434
*

src/FacebookMessage.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use JsonSerializable;
66
use NotificationChannels\Facebook\Enums\AttachmentType;
7+
use NotificationChannels\Facebook\Enums\ImageAspectRatioType;
78
use NotificationChannels\Facebook\Enums\MessagingType;
89
use NotificationChannels\Facebook\Enums\NotificationType;
910
use NotificationChannels\Facebook\Enums\RecipientType;
@@ -47,9 +48,15 @@ class FacebookMessage implements JsonSerializable
4748
/** @var bool */
4849
protected $hasText = false;
4950

51+
/** @var bool There is card with 'image_url' in attachment */
52+
protected $hasImageUrl = false;
53+
5054
/** @var string Message tag used with messaging type MESSAGE_TAG */
5155
protected $messageTag;
5256

57+
/** @var string The aspect ratio for */
58+
protected $imageAspectRatio = ImageAspectRatioType::HORIZONTAL;
59+
5360
/**
5461
* @param string $text
5562
*
@@ -177,6 +184,33 @@ public function notificationType(string $notificationType): self
177184
return $this;
178185
}
179186

187+
public function imageAspectRatio(string $imageAspectRatio): self
188+
{
189+
$imageAspectRatios = [
190+
ImageAspectRatioType::SQUARE,
191+
ImageAspectRatioType::HORIZONTAL,
192+
];
193+
194+
if (! in_array($imageAspectRatio, $imageAspectRatios, false)) {
195+
throw CouldNotCreateMessage::invalidImageAspectRatio();
196+
}
197+
198+
foreach ($this->cards as $card) {
199+
if (array_key_exists('image_url', $card->toArray())) {
200+
$this->hasImageUrl = true;
201+
break;
202+
}
203+
}
204+
205+
if (! $this->hasImageUrl) {
206+
return $this;
207+
}
208+
209+
$this->imageAspectRatio = $imageAspectRatio;
210+
211+
return $this;
212+
}
213+
180214
/**
181215
* Helper to set notification type as REGULAR.
182216
*
@@ -376,6 +410,10 @@ protected function genericMessageToArray(): array
376410
$message['message']['attachment']['payload']['elements'] = $this->cards;
377411
$message['messaging_type'] = $this->messagingType;
378412

413+
if ($this->hasImageUrl) {
414+
$message['message']['attachment']['payload']['image_aspect_ratio'] = $this->imageAspectRatio;
415+
}
416+
379417
if (filled($this->messageTag)) {
380418
$message['tag'] = $this->messageTag;
381419
}

0 commit comments

Comments
 (0)