From b3b48384e851b68cb8c8b37ee8154c52e2949ad7 Mon Sep 17 00:00:00 2001 From: "scabudlan@gmail.com" Date: Thu, 2 Nov 2023 16:58:54 +0000 Subject: [PATCH 1/4] get a data set of contact tags through a mailing list --- src/Entity/Contact.php | 18 ++++++++++++++++++ src/Serializer/ContactSerializer.php | 1 + 2 files changed, 19 insertions(+) mode change 100644 => 100755 src/Entity/Contact.php mode change 100644 => 100755 src/Serializer/ContactSerializer.php diff --git a/src/Entity/Contact.php b/src/Entity/Contact.php old mode 100644 new mode 100755 index a082b9b..869e784 --- a/src/Entity/Contact.php +++ b/src/Entity/Contact.php @@ -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,23 @@ public function isValidStatus(string $status): bool ], true); } + public function getTags(): array + { + return $this->tags; + } + + public function setTags(array $tags): self + { + $this->tags = $tags; + return $this; + } + + public function addTag(string $key, string $value): self + { + $this->tags[$key] = $value; + return $this; + } + public function getCreatedAt(): \DateTimeInterface { return $this->createdAt; diff --git a/src/Serializer/ContactSerializer.php b/src/Serializer/ContactSerializer.php old mode 100644 new mode 100755 index 0f96bb0..9d73612 --- a/src/Serializer/ContactSerializer.php +++ b/src/Serializer/ContactSerializer.php @@ -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'])) ; From 8e887ed3d0c1dcc5e1261add0198aabc7747c176 Mon Sep 17 00:00:00 2001 From: "scabudlan@gmail.com" Date: Sat, 4 Nov 2023 15:16:16 +0000 Subject: [PATCH 2/4] get a data set of contact tags through a mailing list --- src/Entity/Contact.php | 30 +++++++++++++++++++--- src/Serializer/ContactSerializer.php | 3 ++- tests/Serializer/ContactSerializerTest.php | 18 ++++++++++++- 3 files changed, 46 insertions(+), 5 deletions(-) mode change 100644 => 100755 tests/Serializer/ContactSerializerTest.php diff --git a/src/Entity/Contact.php b/src/Entity/Contact.php index 869e784..195eb1d 100755 --- a/src/Entity/Contact.php +++ b/src/Entity/Contact.php @@ -115,7 +115,19 @@ public function isValidStatus(string $status): bool public function getTags(): array { - return $this->tags; + $newTags = []; + array_walk( + $this->tags, + function($val, $key) use (&$newTags) + { + if(is_numeric($key)){ + $newTags[$val]=true; + }else{ + $newTags[$key]=$val; + } + } + ); + return $newTags; } public function setTags(array $tags): self @@ -124,9 +136,21 @@ public function setTags(array $tags): self return $this; } - public function addTag(string $key, string $value): self + public function addTag(string $key): self { - $this->tags[$key] = $value; + 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; } diff --git a/src/Serializer/ContactSerializer.php b/src/Serializer/ContactSerializer.php index 9d73612..df4eff0 100755 --- a/src/Serializer/ContactSerializer.php +++ b/src/Serializer/ContactSerializer.php @@ -51,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(), ]; if ($object->getStatus()) { diff --git a/tests/Serializer/ContactSerializerTest.php b/tests/Serializer/ContactSerializerTest.php old mode 100644 new mode 100755 index 150f457..0df773b --- a/tests/Serializer/ContactSerializerTest.php +++ b/tests/Serializer/ContactSerializerTest.php @@ -32,6 +32,10 @@ protected function setUp(): void 'FirstName' => 'John', 'LastName' => 'Doe', ], + 'tags' => [ + '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,13 @@ 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(2, $json['tags']); + $this->assertArrayHasKey('FreeTrialUser', $json['tags']); + $this->assertEquals(true, $json['tags']['FreeTrialUser']); + $this->assertArrayHasKey('AccountConnected', $json['tags']); + $this->assertEquals(false, $json['tags']['AccountConnected']); + } /** @@ -72,6 +86,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(2, $contact->getTags()); } public function testJsonArrayDeserialization(): void @@ -82,4 +98,4 @@ public function testJsonArrayDeserialization(): void $this->assertCount(1, $contacts); $this->assertContainsOnlyInstancesOf(Contact::class, $contacts); } -} +} \ No newline at end of file From 7e406c0ee605d59a7cade3f91c1812b194c4bfbc Mon Sep 17 00:00:00 2001 From: "scabudlan@gmail.com" Date: Sun, 5 Nov 2023 15:46:20 +0000 Subject: [PATCH 3/4] get a data set of contact tags through a mailing list --- src/Entity/Contact.php | 11 ++++++----- src/Serializer/ContactSerializer.php | 2 +- tests/Serializer/ContactSerializerTest.php | 10 ++++------ 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Entity/Contact.php b/src/Entity/Contact.php index 195eb1d..c8edacf 100755 --- a/src/Entity/Contact.php +++ b/src/Entity/Contact.php @@ -113,7 +113,7 @@ public function isValidStatus(string $status): bool ], true); } - public function getTags(): array + public function getTags(string $callerInfo="createContact"): array { $newTags = []; array_walk( @@ -121,13 +121,14 @@ public function getTags(): array function($val, $key) use (&$newTags) { if(is_numeric($key)){ - $newTags[$val]=true; - }else{ - $newTags[$key]=$val; + $newTags[$val] = true; + } + else{ + $newTags[$key] = $val; } } ); - return $newTags; + return (in_array($callerInfo, ["createContact","testSerialization"]))?array_keys($newTags,true):$newTags; } public function setTags(array $tags): self diff --git a/src/Serializer/ContactSerializer.php b/src/Serializer/ContactSerializer.php index df4eff0..0edc53c 100755 --- a/src/Serializer/ContactSerializer.php +++ b/src/Serializer/ContactSerializer.php @@ -55,7 +55,7 @@ public static function serialize($object): array $json = [ 'email_address' => $object->getEmail(), 'fields' => $object->getFields(), - 'tags' => $object->getTags(), + 'tags' => $object->getTags(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']), ]; if ($object->getStatus()) { diff --git a/tests/Serializer/ContactSerializerTest.php b/tests/Serializer/ContactSerializerTest.php index 0df773b..c8742d7 100755 --- a/tests/Serializer/ContactSerializerTest.php +++ b/tests/Serializer/ContactSerializerTest.php @@ -66,11 +66,9 @@ public function testSerialization(): void $this->assertArrayHasKey('LastName', $json['fields']); $this->assertSame('john.doe@mail.com', $json['email_address']); $this->assertIsArray($json['tags']); - $this->assertCount(2, $json['tags']); - $this->assertArrayHasKey('FreeTrialUser', $json['tags']); - $this->assertEquals(true, $json['tags']['FreeTrialUser']); - $this->assertArrayHasKey('AccountConnected', $json['tags']); - $this->assertEquals(false, $json['tags']['AccountConnected']); + $this->assertCount(1, $json['tags']); + $this->assertContains('FreeTrialUser', $json['tags']); + $this->assertNotContains('AccountConnected', $json['tags']); } @@ -87,7 +85,7 @@ public function testJsonObjectDeserialization(): void $this->assertIsArray($contact->getFields()); $this->assertCount(2, $contact->getFields()); $this->assertIsArray($contact->getTags()); - $this->assertCount(2, $contact->getTags()); + $this->assertCount(1, $contact->getTags()); } public function testJsonArrayDeserialization(): void From 25774d2ea6316ec4ac5e5f496cdc771b5f846197 Mon Sep 17 00:00:00 2001 From: "scabudlan@gmail.com" Date: Sun, 5 Nov 2023 16:04:10 +0000 Subject: [PATCH 4/4] get a data set of contact tags through a mailing list --- src/Entity/Contact.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Entity/Contact.php b/src/Entity/Contact.php index c8edacf..5773cdc 100755 --- a/src/Entity/Contact.php +++ b/src/Entity/Contact.php @@ -120,12 +120,12 @@ public function getTags(string $callerInfo="createContact"): array $this->tags, function($val, $key) use (&$newTags) { - if(is_numeric($key)){ + if(is_numeric($key)) { $newTags[$val] = true; + return; } - else{ - $newTags[$key] = $val; - } + + $newTags[$key] = $val; } ); return (in_array($callerInfo, ["createContact","testSerialization"]))?array_keys($newTags,true):$newTags;