Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/Entity/Contact.php
100644 → 100755
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be grateful to add a method to delete a tag, and related test cases 😄

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thank you for your time reviewing my pull request.
As requested, I have added the method removeTag and also updated the ContactSerializerTest.php.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Contact
private string $email;
private array $fields = [];
private ?string $status = null;
private array $tags = [];
private \DateTimeInterface $createdAt;

public function getId(): string
Expand Down Expand Up @@ -112,6 +113,48 @@ public function isValidStatus(string $status): bool
], true);
}

public function getTags(string $callerInfo="createContact"): array
{
$newTags = [];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you return the tags property?

array_walk(
$this->tags,
function($val, $key) use (&$newTags)
{
if(is_numeric($key)) {
$newTags[$val] = true;
return;
}

$newTags[$key] = $val;
}
);
return (in_array($callerInfo, ["createContact","testSerialization"]))?array_keys($newTags,true):$newTags;
}

public function setTags(array $tags): self
{
$this->tags = $tags;
return $this;
}

public function addTag(string $key): self
{
if( !in_array($key, $this->tags) ){
$this->tags[] = $key;
}
return $this;
}

public function removeTag(string $key): self
{
while (($index = array_search($key, $this->tags)) !== false)
{
unset($this->tags[$index]);
}
$this->tags[$key] = false;
return $this;
}

public function getCreatedAt(): \DateTimeInterface
{
return $this->createdAt;
Expand Down
4 changes: 3 additions & 1 deletion src/Serializer/ContactSerializer.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static function deserializeSingle(array $json): Contact
->setEmail($json['email_address'])
->setFields($json['fields'])
->setStatus($json['status'])
->setTags($json['tags'])
->setCreatedAt(new \DateTime($json['created_at']))
;

Expand All @@ -50,10 +51,11 @@ public static function serialize($object): array
if (!$object instanceof Contact) {
throw new \InvalidArgumentException('Invalid object type. Expected Contact.');
}

$json = [
'email_address' => $object->getEmail(),
'fields' => $object->getFields(),
'tags' => $object->getTags(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's that?

];

if ($object->getStatus()) {
Expand Down
16 changes: 15 additions & 1 deletion tests/Serializer/ContactSerializerTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ protected function setUp(): void
'FirstName' => 'John',
'LastName' => 'Doe',
],
'tags' => [
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the json tags should be like that (key: string => value: boolean)?

'FreeTrialUser' => true,
'AccountConnected' => false,
],
'status' => 'SUBSCRIBED',
'created_at' => '2020-07-04T14:55:32+00:00',
],
Expand All @@ -46,6 +50,9 @@ public function testSerialization(): void
->setFirstName('John')
->setLastName('Doe')
->setStatus(Contact::STATUS_SUBSCRIBED)
->addTag('FreeTrialUser')
->addTag('AccountConnected')
->removeTag('AccountConnected')
;

$json = ContactSerializer::serialize($contact);
Expand All @@ -58,6 +65,11 @@ public function testSerialization(): void
$this->assertArrayHasKey('FirstName', $json['fields']);
$this->assertArrayHasKey('LastName', $json['fields']);
$this->assertSame('john.doe@mail.com', $json['email_address']);
$this->assertIsArray($json['tags']);
$this->assertCount(1, $json['tags']);
$this->assertContains('FreeTrialUser', $json['tags']);
$this->assertNotContains('AccountConnected', $json['tags']);

}

/**
Expand All @@ -72,6 +84,8 @@ public function testJsonObjectDeserialization(): void
$this->assertInstanceOf(\DateTimeInterface::class, $contact->getCreatedAt());
$this->assertIsArray($contact->getFields());
$this->assertCount(2, $contact->getFields());
$this->assertIsArray($contact->getTags());
$this->assertCount(1, $contact->getTags());
}

public function testJsonArrayDeserialization(): void
Expand All @@ -82,4 +96,4 @@ public function testJsonArrayDeserialization(): void
$this->assertCount(1, $contacts);
$this->assertContainsOnlyInstancesOf(Contact::class, $contacts);
}
}
}