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/Entity/SmsMessage.php \Drupal\sms\Entity\SmsMessage::chunkByRecipients()
  2. 2.1.x src/Entity/SmsMessage.php \Drupal\sms\Entity\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/Entity/SmsMessage.php, line 446

Class

SmsMessage
Defines the SMS message entity.

Namespace

Drupal\sms\Entity

Code

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

  // Save processing by returning early.
  if ($size < 1 || count($recipients_all) <= $size) {
    return [
      $this,
    ];
  }

  // Create a baseline SMS message with recipients cleaned out.
  $base = $this
    ->createDuplicate();
  $base
    ->removeRecipients($recipients_all);
  $messages = [];
  foreach (array_chunk($recipients_all, $size) as $recipients) {
    $messages[] = $base
      ->createDuplicate()
      ->addRecipients($recipients);
  }
  return $messages;
}