You are here

public function SmsMessage::chunkByRecipients in SMS Framework 8

Same name in this branch
  1. 8 src/Message/SmsMessage.php \Drupal\sms\Message\SmsMessage::chunkByRecipients()
  2. 8 src/Entity/SmsMessage.php \Drupal\sms\Entity\SmsMessage::chunkByRecipients()
Same name and namespace in other branches
  1. 2.x src/Message/SmsMessage.php \Drupal\sms\Message\SmsMessage::chunkByRecipients()
  2. 2.1.x src/Message/SmsMessage.php \Drupal\sms\Message\SmsMessage::chunkByRecipients()

Split this SMS message into new messages by chunks of recipients.

Parameters

int $size: The quantity of recipients to chunk by.

Return value

static[] An array of SMS messages split by recipient chunks.

Overrides SmsMessageInterface::chunkByRecipients

File

src/Message/SmsMessage.php, line 332

Class

SmsMessage
Basic implementation of an SMS message.

Namespace

Drupal\sms\Message

Code

public function chunkByRecipients($size) {
  $recipients_all = $this
    ->getRecipients();

  // Save processing by returning early.
  if ($size < 1 || count($recipients_all) <= $size) {
    return [
      $this,
    ];
  }
  $base = clone $this;
  $base
    ->removeRecipients($recipients_all);
  $messages = [];
  foreach (array_chunk($recipients_all, $size) as $recipients) {
    $message = clone $base;
    $messages[] = $message
      ->addRecipients($recipients);
  }
  return $messages;
}