Skip to content

Commit ea005a0

Browse files
authored
Merge pull request #21 from leolll/main
Fix Contact 'email' property filtered due to 'Emails' HubSpot type
2 parents bdd50b6 + b0ca9cf commit ea005a0

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/Api/Model.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function fill(array $properties): static
8282

8383
$properties = array_filter(
8484
$properties,
85-
static fn (string $key): bool => !(HubSpot::isType($key) || HubSpot::isType(Str::plural($key))),
85+
fn (string $key): bool => $this->isAllowedProperty($key),
8686
ARRAY_FILTER_USE_KEY
8787
);
8888

@@ -91,6 +91,12 @@ public function fill(array $properties): static
9191
return $this;
9292
}
9393

94+
private function isAllowedProperty(string $key): bool
95+
{
96+
return $key === 'email' ||
97+
!(HubSpot::isType($key) || HubSpot::isType(Str::plural($key)));
98+
}
99+
94100
public function type(): string
95101
{
96102
return $this->type;
@@ -250,10 +256,9 @@ public function __get($key)
250256

251257
public function __set($key, $value)
252258
{
253-
if (HubSpot::isType($key) || HubSpot::isType(Str::plural($key))) {
254-
return;
259+
if ($this->isAllowedProperty($key)) {
260+
$this->properties[$key] = $value;
255261
}
256-
$this->properties[$key] = $value;
257262
}
258263

259264
public function __isset($key)

tests/Unit/Api/ModelTest.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,20 @@ public function fill(array $properties): static
4242
$this->addToAssertionCount(1);
4343
});
4444

45-
test('fill does not fill hubspot types', function (string $type) {
45+
test('fill does not fill hubspot types except email', function (string $type) {
4646
$baseData = ['test_name' => $this->getName()];
4747
$model = (new class extends AbstractApiModel {
4848
})->fill([$type => sha1(random_bytes(11)), ...$baseData]);
4949

5050
$properties = new ReflectionProperty($model, 'properties');
51-
expect($properties->getValue($model))->toBe($baseData);
51+
if ($type === 'email') {
52+
expect($properties->getValue($model))
53+
->toBeArray()
54+
->toHaveKey('email')
55+
->toHaveKey('test_name');
56+
} else {
57+
expect($properties->getValue($model))->toBe($baseData);
58+
}
5259
})->with('SdkTypes-both');
5360

5461
test('update calls fill & save', function () {
@@ -104,11 +111,16 @@ protected function assertBacktraceIsUpdate(): void
104111

105112
$model->$propName = $propValue;
106113

107-
expect($attributes->getValue($model))
108-
->toBeArray()
109-
->toBeEmpty();
110-
})
111-
->with('SdkTypes-both');
114+
if ($propName === 'email') {
115+
expect($attributes->getValue($model))
116+
->toBeArray()
117+
->toHaveKey('email', $propValue);
118+
} else {
119+
expect($attributes->getValue($model))
120+
->toBeArray()
121+
->toBeEmpty();
122+
}
123+
})->with('SdkTypes-both');
112124

113125
test('builder returns api builder', function () {
114126
$model = new class extends AbstractApiModel {

0 commit comments

Comments
 (0)