Skip to content

Commit e9aa157

Browse files
authored
Merge pull request dekalee#13 from danielfh/unset-fields
Allow to unset fields
2 parents 19e38fc + d2a292c commit e9aa157

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

src/AutopilotContact.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,6 @@ public function toRequest($prependKey = true)
200200

201201
/** @var AutopilotField $field */
202202
foreach($this->fields as $field) {
203-
//TODO: how do you "unset" fields with Autopilot? Their API doesn't say anything about it
204-
if ($field->getValue() === null) {
205-
continue;
206-
}
207-
208203
if (! $field->isReserved()) {
209204
$result['custom'][$field->formatName()] = $field->getValue();
210205
} else {
@@ -246,4 +241,4 @@ function jsonSerialize()
246241
{
247242
return $this->toArray();
248243
}
249-
}
244+
}

src/AutopilotField.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function setValue($value)
229229
}
230230

231231
// type of field is set in the constructor
232-
if ($this->getType() !== $type) {
232+
if (($this->getType() !== $type) && !is_null($value)) {
233233
throw AutopilotException::typeMismatch($this->getType(), $type);
234234
}
235235

tests/unit/AutopilotContactCest.php

+49
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,55 @@ public function testContactToRequest(UnitTester $I)
163163
$I->assertEquals($expected['contact'], $contact->toRequest($prependKey = false));
164164
}
165165

166+
public function testContactFieldsCanBeCleared(UnitTester $I)
167+
{
168+
$I->wantTo('clear contact fields');
169+
170+
$values = [
171+
'FirstName' => null,
172+
'LastName' => null,
173+
'custom_fields' => [
174+
[
175+
'kind' => 'Age',
176+
'value' => null,
177+
'fieldType' => 'integer',
178+
],
179+
[
180+
'kind' => 'Birthday',
181+
'value' => null,
182+
'fieldType' => 'date',
183+
],
184+
[
185+
'kind' => 'Category',
186+
'value' => null,
187+
'fieldType' => 'string',
188+
],
189+
[
190+
'kind' => 'Completed Status',
191+
'value' => null,
192+
'fieldType' => 'float',
193+
],
194+
],
195+
];
196+
197+
$contact = new AutopilotContact($values);
198+
199+
$expected = [
200+
'contact' => [
201+
'FirstName' => null,
202+
'LastName' => null,
203+
'custom' => [
204+
'integer--Age' => null,
205+
'date--Birthday' => null,
206+
'string--Category' => null,
207+
'float--Completed--Status' => null,
208+
],
209+
],
210+
];
211+
212+
$I->assertEquals($expected, $contact->toRequest());
213+
}
214+
166215
public function testContactMagicGettersAndSetters(UnitTester $I)
167216
{
168217
$I->wantTo('set and get values using magic methods');

0 commit comments

Comments
 (0)