-
Notifications
You must be signed in to change notification settings - Fork 4
get a data set of contact tags through a mailing list #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
b3b4838
8e887ed
7e406c0
25774d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -112,6 +113,48 @@ public function isValidStatus(string $status): bool | |
| ], true); | ||
| } | ||
|
|
||
| public function getTags(string $callerInfo="createContact"): array | ||
| { | ||
| $newTags = []; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't you return the |
||
| 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; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'])) | ||
| ; | ||
|
|
||
|
|
@@ -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']), | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's that? |
||
| ]; | ||
|
|
||
| if ($object->getStatus()) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,10 @@ protected function setUp(): void | |
| 'FirstName' => 'John', | ||
| 'LastName' => 'Doe', | ||
| ], | ||
| 'tags' => [ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
| ], | ||
|
|
@@ -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); | ||
|
|
@@ -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']); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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 | ||
|
|
@@ -82,4 +96,4 @@ public function testJsonArrayDeserialization(): void | |
| $this->assertCount(1, $contacts); | ||
| $this->assertContainsOnlyInstancesOf(Contact::class, $contacts); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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 😄
There was a problem hiding this comment.
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.