2 Commits
0.2 ... 0.4

Author SHA1 Message Date
6ebae6201c global test mode + fixes 2025-02-05 19:42:54 +01:00
9df8151682 add custom locale per recipient 2025-01-23 15:40:48 +01:00
2 changed files with 29 additions and 17 deletions

View File

@@ -4,6 +4,7 @@ namespace Bluesquare\Push;
use Bluesquare\Push\PushTemplate; use Bluesquare\Push\PushTemplate;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Illuminate\Support\Arr;
use Psr\Http\Message\StreamInterface; use Psr\Http\Message\StreamInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -73,25 +74,24 @@ class PushApi
continue; continue;
} }
foreach ($value as $subkey => $subvalue) { $values = Arr::dot($value);
foreach ($values as $subkey => $subvalue) {
$subkey = str_replace('.', '][', $subkey);
$multipart_form[] = [ $multipart_form[] = [
'name' => "{$key}[$subkey]" . (is_array($subvalue) ? '[' . key($subvalue) . ']' : '' ), 'name' => "{$key}[$subkey]",
'contents' => (is_array($subvalue) ? reset($subvalue) : $subvalue) 'contents' => is_array($subvalue) ? json_encode($subvalue) : $subvalue
]; ];
} }
} }
try { return $client->request('post', $url, [
return $client->request('post', $url, [ 'multipart' => $multipart_form,
'multipart' => $multipart_form, 'headers' => [
'headers' => [ 'Authorization' => 'Bearer ' . $this->api_key,
'Authorization' => 'Bearer ' . $this->api_key, 'Accept' => 'application/json'
'Accept' => 'application/json' ]
] ])->getBody();
])->getBody();
} catch(\Exception $e) {
throw new PushException($e->getMessage());
}
} }
} }

View File

@@ -78,6 +78,10 @@ class PushTemplate implements \JsonSerializable
public function __construct(string $template_id) public function __construct(string $template_id)
{ {
$this->template_id = $template_id; $this->template_id = $template_id;
if (env('PUSH_TEST_MODE', false)) {
$this->testMode();
}
} }
/** /**
@@ -138,6 +142,10 @@ class PushTemplate implements \JsonSerializable
$data['unsubscribe_url'] = $recipient['unsubscribe_url']; $data['unsubscribe_url'] = $recipient['unsubscribe_url'];
} }
if (! empty($recipient['locale'])) {
$data['locale'] = $recipient['locale'];
}
$this->recipients[] = $data; $this->recipients[] = $data;
} }
} }
@@ -152,7 +160,7 @@ class PushTemplate implements \JsonSerializable
* @param string|null $unsubscribe_url * @param string|null $unsubscribe_url
* @return $this * @return $this
*/ */
public function addRecipient(string $email, string $name = null, array $parameters = [], string $unsubscribe_url = null): PushTemplate public function addRecipient(string $email, string $name = null, array $parameters = [], string $unsubscribe_url = null, string $locale = null): PushTemplate
{ {
$data = [ $data = [
'email' => $email 'email' => $email
@@ -170,6 +178,10 @@ class PushTemplate implements \JsonSerializable
$data['unsubscribe_url'] = $unsubscribe_url; $data['unsubscribe_url'] = $unsubscribe_url;
} }
if (! is_null($locale)) {
$data['locale'] = $locale;
}
$this->recipients[] = $data; $this->recipients[] = $data;
return $this; return $this;