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 Jul 30, 2023
1 parent 228be73 commit 1d87ad8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ $response = $braze->users()->track($request, false);

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

### Custom user attributes

To set a custom User attribute use the ```setCustomAttribute``` method 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('specialUserCount', 47);
```

### Endpoints
Endpoints are organized by url prefix. The SDK supports all the Braze endpoints:
- users
Expand Down
16 changes: 16 additions & 0 deletions 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,20 @@ public function jsonSerialize(): mixed
$dataToSerialize['dob'] = $this->dob->format('Y-m-d');
}

foreach ($this->_customAttributes as $key => $value) {
$dataToSerialize[$key] = $value;
}

return $dataToSerialize;
}

/**
* To set a custom DateTime attribute directly use a string in the ISO 8601 format.
* Using a DateTime object directly 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;
}
}
14 changes: 14 additions & 0 deletions tests/Object/UserAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ 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 validProvider(): array
{
$userAttributes1 = new UserAttributes();
Expand Down

0 comments on commit 1d87ad8

Please sign in to comment.