*/ #[ORM\OneToMany(targetEntity: UserPreference::class, mappedBy: 'user', cascade: ['persist', 'remove'])] private Collection $userPreferences; public function __construct() { $this->userPreferences = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getEmail(): ?string { return $this->email; } public function setEmail(string $email): self { $this->email = $email; return $this; } public function getName(): ?string { return $this->name; } public function setName(?string $name): self { $this->name = $name; return $this; } /** * The public representation of the user (e.g. a username, an email address, etc.) * * @see UserInterface */ public function getUserIdentifier(): string { return (string) $this->email; } /** * @see UserInterface */ public function getRoles(): array { $roles = $this->roles; // guarantee every user at least has ROLE_USER $roles[] = 'ROLE_USER'; return array_unique($roles); } public function setRoles(array $roles): self { $this->roles = $roles; return $this; } /** * @see PasswordAuthenticatedUserInterface */ public function getPassword(): string { return $this->password; } public function setPassword(string $password): self { $this->password = $password; return $this; } /** * @see UserInterface */ public function eraseCredentials(): void { // If you store any temporary, sensitive data on the user, clear it here // $this->plainPassword = null; } /** * @return Collection */ public function getUserPreferences(): Collection { return $this->userPreferences; } public function getUserPreference(string $preferenceName) { foreach ($this->userPreferences as $userPreference) { if ($userPreference->getPreference()->getName() === $preferenceName) { return $userPreference->getPreference(); } } } public function hasUserPreference(string $preferenceName): bool { foreach ($this->userPreferences as $userPreference) { if ($userPreference->getPreference()->getId() === $preferenceName) { return true; } } return false; } public function updateUserPreference(string $preferenceName, mixed $preferenceValue): static { foreach ($this->userPreferences as $userPreference) { if ($userPreference->getPreference()->getId() === $preferenceName) { $userPreference->setPreferenceValue($preferenceValue); } } return $this; } public function addUserPreference(UserPreference $userPreference): static { if (!$this->userPreferences->contains($userPreference)) { $this->userPreferences->add($userPreference); $userPreference->setUser($this); } return $this; } public function removeUserPreference(UserPreference $userPreference): static { if ($this->userPreferences->removeElement($userPreference)) { // set the owning side to null (unless already changed) if ($userPreference->getUser() === $this) { $userPreference->setUser(null); } } return $this; } public function getUserPreferenceValues() { return Map::from($this->userPreferences) ->rekey(fn(UserPreference $userPreference) => $userPreference->getPreference()->getId()) ->map(function (UserPreference $userPreference) { if (in_array($userPreference->getPreference()->getId(), ['language', 'provider'])) { return $userPreference->getPreferenceValue(); } foreach ($userPreference->getPreference()->getPreferenceOptions() as $preferenceOption) { // dd((int) $userPreference->getPreferenceValue(), $preferenceOption->getId(), $preferenceOption->getValue()); if ($preferenceOption->getId() === (int) $userPreference->getPreferenceValue()) { return $preferenceOption->getValue(); } } return null; }) ->toArray(); } }