From 207eeb39ca6aa544cb713e334f1bf6b55c2fe24d Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sat, 25 Jul 2020 17:53:37 +0000 Subject: [PATCH] [SELFTAGS] Add Profile::{set,get}SelfTags --- src/Entity/Profile.php | 25 +++++++++++++++++++++++++ src/Entity/ProfileTag.php | 14 ++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/Entity/Profile.php b/src/Entity/Profile.php index 20a0f0fa89..d498780e29 100644 --- a/src/Entity/Profile.php +++ b/src/Entity/Profile.php @@ -19,9 +19,11 @@ namespace App\Entity; +use App\Core\DB\DB; use App\Core\UserRoles; use DateTime; use DateTimeInterface; +use Functional as F; /** * Entity for user profiles @@ -228,4 +230,27 @@ class Profile return $def; } + + public function getSelfTags(): array + { + return DB::findBy('profile_tag', ['tagger' => $this->id, 'tagged' => $this->id]); + } + + public function setSelfTags(array $tags, array $pt_existing, bool $flush = true): void + { + $tag_existing = F\map($pt_existing, function ($pt) { return $pt->getTag(); }); + $tag_to_add = array_diff($tags, $tag_existing); + $tag_to_remove = array_diff($tag_existing, $tags); + $pt_to_remove = F\filter($pt_existing, function ($pt) use ($tag_to_remove) { return in_array($pt->getTag(), $tag_to_remove); }); + foreach ($tag_to_add as $tag) { + $pt = new ProfileTag($this->id, $this->id, $tag); + DB::persist($pt); + } + foreach ($pt_to_remove as $pt) { + DB::remove($pt); + } + if ($flush) { + DB::flush(); + } + } } diff --git a/src/Entity/ProfileTag.php b/src/Entity/ProfileTag.php index 8292a48e30..b44a53a7e3 100644 --- a/src/Entity/ProfileTag.php +++ b/src/Entity/ProfileTag.php @@ -19,6 +19,7 @@ namespace App\Entity; +use DateTime; use DateTimeInterface; /** @@ -86,6 +87,14 @@ class ProfileTag // }}} Autocode + public function __construct(int $tagger, int $tagged, string $tag) + { + $this->tagger = $tagger; + $this->tagged = $tagged; + $this->tag = preg_replace('/ /', '_', trim($tag)); + $this->modified = new DateTime(); + } + public static function schemaDef(): array { return [ @@ -108,4 +117,9 @@ class ProfileTag ], ]; } + + public function __toString() + { + return $this->tag; + } }