A classe `BulkGate\Sms\BulkMessage` representa o objeto que junta mensagens do tipo [`BulkGate\Sms\Message`](https://www.bulkgate.com/pt/documentacao/php-sdk/php-sdk-mensagem/2.0/) numa mensagem em massa (campanha).

```php
use BulkGate\Sms\BulkMessage;
```

O construtor tem um parâmetro opcional do tipo [`array`](http://php.net/manual/en/language.types.array.php), que tem de conter itens [`BulkGate\Sms\Message`](https://www.bulkgate.com/pt/documentacao/php-sdk/php-sdk-mensagem/2.0/).

```php
$array = [
    new BulkGate\Sms\Message("420777777777", "test1"),
    new BulkGate\Sms\Message("420777777778", "test2"),
    new BulkGate\Sms\Message("420777777779", "test3"),
    new BulkGate\Sms\Message("420777777780", "test4"),
];

$bulk_message = new BulkMessage($array);
```

As mensagens podem ser adicionadas uma de cada vez:

```php
$message = new BulkGate\Sms\Message(
    new BulkGate\Sms\Message\PhoneNumber("777777781", BulkGate\Sms\Country::CZECH_REPUBLIC),
    new BulkGate\Sms\Message\Text("test<number>", [
        'number' => '5'
    ])
);

/** @var BulkGate\Sms\BulkMessage $bulk_message */
$bulk_message->addMessage($message);
```

## Iterador

Pode percorrer as mensagens com o ciclo [`foreach`](http://php.net/manual/en/control-structures.foreach.php) 

```php
/** 
 * @var BulkGate\Sms\BulkMessage $bulk_message 
 * @var BulkGate\Sms\Message $message
 */
foreach($bulk_message as $message)
{
    echo $message;
}
```

O número de mensagens obtém-se com `count()`

```php
/** @var BulkGate\Sms\BulkMessage $bulk_message */
$bulk_message->count();
```

## Suporte de JSON

O objeto `BulkGate\Sms\BulkMessage` implementa a interface [`\JsonSerializable`](http://php.net/manual/en/class.jsonserializable.php), que permite convertê-lo para o formato JSON através da função [`json_encode()`](http://php.net/manual/en/function.json-encode.php).

```php
/** @var BulkGate\Sms\BulkMessage $bulk_message */
echo json_encode($bulk_message);
```

O resultado é:

```json
[
  {
    "number": {
      "number": "420777777777",
      "iso": null
    },
    "text": "test1",
    "status": "preparation",
    "price": 0.0,
    "credit": 0.0,
    "id": null,
    "scheduled": null
  },
  {
    "number": {
      "number": "420777777778",
      "iso": null
    },
    "text": "test2",
    "status": "preparation",
    "price": 0.0,
    "credit": 0.0,
    "id": null,
    "scheduled": null
  },
  {
    "number": {
      "number": "420777777779",
      "iso": null
    },
    "text": "test3",
    "status": "preparation",
    "price": 0.0,
    "credit": 0.0,
    "id": null,
    "scheduled": null
  },
  {
    "number": {
      "number": "420777777780",
      "iso": null
    },
    "text": "test4",
    "status": "preparation",
    "price": 0.0,
    "credit": 0.0,
    "id": null,
    "scheduled": null
  },
  {
    "number": {
      "number": "777777781",
      "iso": "cz"
    },
    "text": "test5",
    "status": "preparation",
    "price": 0.0,
    "credit": 0.0,
    "id": null,
    "scheduled": null
  }
]
```

## Conversão para string

O objeto `BulkGate\Sms\BulkMessage` implementa o método mágico [`__string()`](http://php.net/manual/en/language.oop5.magic.php#object.tostring).

```php
/** @var BulkGate\Sms\BulkMessage $bulk_message */
$message = (string) $bulk_message; // para a variável
echo $bulk_message; // para a saída
```

O resultado é:
```
420777777777: test1
420777777778: test2
420777777779: test3
420777777780: test4
777777781: test5
```

## Conversão para array

```php
/** @var BulkGate\Sms\BulkMessage $bulk_message */
$array = $message->toArray();
```
