[SELFTAGS] Add Profile::{set,get}SelfTags

This commit is contained in:
Hugo Sales 2020-07-25 17:53:37 +00:00 committed by Hugo Sales
parent 1e911f1ba4
commit 207eeb39ca
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 39 additions and 0 deletions

View File

@ -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();
}
}
}

View File

@ -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;
}
}