Skip to content

Commit

Permalink
feat: add support for user custom attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioturdo committed Aug 3, 2023
1 parent f7c30ad commit d10c351
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ $response = $braze->users()->track($request, false);

You can see a few complete [examples](./examples) in the repository.

### Custom user attributes

To set custom User attributes use the ```setCustomAttribute``` or ```setCustomAttributes``` methods available in the ```UserAttributes``` class.

```php
use ImmobiliareLabs\BrazeSDK\Object\UserAttributes;

$userAttributes = new UserAttributes();
$userAttributes->external_id = 'user-id';
$userAttributes->first_name = 'Name';

$userAttributes->setCustomAttribute('custom_int_property', 47);

$userAttributes->setCustomAttributes([
'custom_string_property' => 'properyValue',
'custom_bool_property' => false,
]);

$request = new TrackRequest();
$request->attributes = [$userAttributes];

$response = $braze->users()->track($request, false);
```

### Endpoints
Endpoints are organized by url prefix. The SDK supports all the Braze endpoints:
- users
Expand Down
21 changes: 20 additions & 1 deletion src/Object/UserAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class UserAttributes extends BaseObject

public ?string $twitter = null;

private array $_customAttributes = [];

public function validate(bool $strict): void
{
parent::validate($strict);
Expand All @@ -84,6 +86,23 @@ public function jsonSerialize(): mixed
$dataToSerialize['dob'] = $this->dob->format('Y-m-d');
}

return $dataToSerialize;
return array_merge($dataToSerialize, $this->_customAttributes);
}

/**
* To set a custom DateTime attribute use a string in the ISO 8601 format.
* Using a DateTime object it would not be possible to know if you want
* to send a date that includes time or not.
*/
public function setCustomAttribute(string $key, array|bool|float|int|string $value): void
{
$this->_customAttributes[$key] = $value;
}

public function setCustomAttributes(array $customAttributes): void
{
foreach ($customAttributes as $key => $value) {
$this->setCustomAttribute($key, $value);
}
}
}
31 changes: 31 additions & 0 deletions tests/Object/UserAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,37 @@ public function testJsonSerialize(): void
$this->assertSame($userAttributes->dob->format('Y-m-d'), $serialized['dob']);
}

public function testCustomAttribute(): void
{
$customAttributeKey = 'key';
$customAttributeValue = 'value';

$userAttributes = new UserAttributes();
$userAttributes->setCustomAttribute($customAttributeKey, $customAttributeValue);

$serialized = $userAttributes->jsonSerialize();

$this->assertSame($customAttributeValue, $serialized[$customAttributeKey]);
$this->assertArrayNotHasKey('_customAttributes', $serialized);
}

public function testCustomAttributes(): void
{
$customAttributes = [
'custom1' => 'value1',
'custom2' => 2,
];

$userAttributes = new UserAttributes();
$userAttributes->setCustomAttributes($customAttributes);

$serialized = $userAttributes->jsonSerialize();

foreach ($customAttributes as $key => $value) {
$this->assertSame($value, $serialized[$key]);
}
}

public function validProvider(): array
{
$userAttributes1 = new UserAttributes();
Expand Down

0 comments on commit d10c351

Please sign in to comment.