|
4 | 4 |
|
5 | 5 | use JsonSerializable; |
6 | 6 | use NotificationChannels\Facebook\Enums\AttachmentType; |
| 7 | +use NotificationChannels\Facebook\Enums\ImageAspectRatioType; |
7 | 8 | use NotificationChannels\Facebook\Enums\MessagingType; |
8 | 9 | use NotificationChannels\Facebook\Enums\NotificationType; |
9 | 10 | use NotificationChannels\Facebook\Enums\RecipientType; |
@@ -47,9 +48,15 @@ class FacebookMessage implements JsonSerializable |
47 | 48 | /** @var bool */ |
48 | 49 | protected $hasText = false; |
49 | 50 |
|
| 51 | + /** @var bool There is card with 'image_url' in attachment */ |
| 52 | + protected $hasImageUrl = false; |
| 53 | + |
50 | 54 | /** @var string Message tag used with messaging type MESSAGE_TAG */ |
51 | 55 | protected $messageTag; |
52 | 56 |
|
| 57 | + /** @var string The aspect ratio for */ |
| 58 | + protected $imageAspectRatio = ImageAspectRatioType::HORIZONTAL; |
| 59 | + |
53 | 60 | /** |
54 | 61 | * @param string $text |
55 | 62 | * |
@@ -177,6 +184,33 @@ public function notificationType(string $notificationType): self |
177 | 184 | return $this; |
178 | 185 | } |
179 | 186 |
|
| 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 | + |
180 | 214 | /** |
181 | 215 | * Helper to set notification type as REGULAR. |
182 | 216 | * |
@@ -376,6 +410,10 @@ protected function genericMessageToArray(): array |
376 | 410 | $message['message']['attachment']['payload']['elements'] = $this->cards; |
377 | 411 | $message['messaging_type'] = $this->messagingType; |
378 | 412 |
|
| 413 | + if ($this->hasImageUrl) { |
| 414 | + $message['message']['attachment']['payload']['image_aspect_ratio'] = $this->imageAspectRatio; |
| 415 | + } |
| 416 | + |
379 | 417 | if (filled($this->messageTag)) { |
380 | 418 | $message['tag'] = $this->messageTag; |
381 | 419 | } |
|
0 commit comments