Skip to content

Commit

Permalink
fix(variables): merging sms options with variables not working
Browse files Browse the repository at this point in the history
  • Loading branch information
sudkumar committed Sep 30, 2020
1 parent d763b14 commit c96f2e0
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 7 deletions.
67 changes: 60 additions & 7 deletions src/SMS/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class Options extends Msg91Options
*/
public $receiver_key = "mobiles";

/**
* Variable mapping for all the recipients
* @var array
*/
protected $variables_mapping = [];

/**
* Set the recipient(s) of the sms
* @param int|array|null $mobile - recipient's mobile number
Expand Down Expand Up @@ -82,31 +88,57 @@ public function receiverKey(string $receiver_key = "mobiles")

/**
* Set value for a variable (used in the Flow message template) for all recipients
* NOTE: Please call this method after recipients have been set
* @param string $name - name of the variable in the template
* @param string|array $name - name of the variable in the template
* @param string|number|null $value - value for the variable to be placed in template
* @return $this
*/
public function variable(string $name, $value = null)
public function variable($name, $value = null)
{
$variables = $this->variables_mapping;
if (is_array($name)) {
$variables = array_merge($variables, $name);
} else {
$variables[$name] = $value;
}
// update the existing recipients
$existing_recipients = $this->getPayloadForKey('recipients', []);
if ($existing_recipients && count($existing_recipients) > 0) {
$recipients = array_map(function ($recipient) use ($name, $value) {
return array_merge($recipient, [
$name => $value
]);
$recipients = array_map(function ($recipient) use ($variables) {
return array_merge($recipient, $variables);
}, $existing_recipients);
$this->recipients($recipients);
}
// also store the mapping for future recipients
$this->variables_mapping = $variables;
return $this;
}

/**
* Get the variables mapping which was set using `variable` method
* @return array
*/
public function getVariableMapping()
{
return $this->variables_mapping;
}

/**
* Set recipients with mobile numbers and any variables to pass to the template
* @param array $recipients This should be an array of recipients with variables
* e.g. [['mobiles' => '919999999999', '<var>' => '<value>']]
*/
public function recipients($recipients = [])
{
if (!is_array($recipients) || (count($recipients) > 0 && !is_array($recipients[0]))) {
// these are mobile number(s)
// transform them into recipients
$recipients = $this->transformMobileNumbersToRecipients($recipients);
}
// put variables to all new recipients
$recipients = array_map(function ($recipient) {
return array_merge($recipient, $this->variables_mapping);
}, $recipients);
// set the payload
$this->setPayloadFor('recipients', $recipients);
return $this;
}
Expand Down Expand Up @@ -170,4 +202,25 @@ public function resolveConfig(Config $config): self
}
});
}

public function mergeWith($options = null)
{
if ($options instanceof self) {
// merge the payloads
$existing_recipients = $this->getPayloadForKey('recipients', []);
$this->payload = array_merge($this->toArray(), $options->toArray());

// merge the array elements which are replaced by array_merge
// - recipients
$this->recipients(array_merge($options->getPayloadForKey('recipients', []), $existing_recipients));

// set the new receiver key
$this->receiverKey($options->receiver_key);

// set the variables
$this->variable($options->getVariableMapping());
return $this;
}
return parent::mergeWith($options);
}
}
56 changes: 56 additions & 0 deletions tests/SMS/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,60 @@ public function test_variable_for_multiple_recipients()
$this->assertEquals('Craft Sys', $recipient['name']);
}
}


public function test_merge_will_merge_recipients()
{
$this->options = $this->options
->recipients([[
'mobiles' => '91123123123'
]])->mergeWith((new Options)->recipients([[
'mobiles' => '91123123124'
]]));
$recipients = $this->options->getPayloadForKey('recipients');
$this->assertCount(2, $recipients);
}

public function test_merging_with_another_options_keeps_variables()
{
$this->options = $this->options
->recipients([[
'mobiles' => '91123123123'
]])
->variable('name', 'Craft Sys')
->mergeWith((new Options())
->recipients([
[
'mobiles' => '91123123124'
]
])
->variable('short_name', 'Craft'));
$recipients = $this->options->getPayloadForKey('recipients');
$this->assertCount(2, $recipients);
foreach ($recipients as $recipient) {
$this->assertArrayHasKey('name', $recipient);
$this->assertArrayHasKey('short_name', $recipient);
$this->assertEquals('Craft Sys', $recipient['name']);
$this->assertEquals('Craft', $recipient['short_name']);
}
}

public function test_variable_applied_to_future_recipients()
{
$this->options = $this->options
->variable('short_name', 'Craft')
->recipients([[
'mobiles' => '91123123123'
], [
'mobiles' => '91123123124'
]])
->variable('name', 'Craft Sys');
$recipients = $this->options->getPayloadForKey('recipients');
foreach ($recipients as $recipient) {
$this->assertArrayHasKey('name', $recipient);
$this->assertArrayHasKey('short_name', $recipient);
$this->assertEquals('Craft Sys', $recipient['name']);
$this->assertEquals('Craft', $recipient['short_name']);
}
}
}

0 comments on commit c96f2e0

Please sign in to comment.