From 86dd5ae0788fddb73e5fa14a2435dbb0d86a0fef Mon Sep 17 00:00:00 2001 From: tracer Date: Mon, 11 Apr 2022 17:36:01 +0200 Subject: [PATCH] changed to attributes --- src/Entity/User.php | 267 +++++++++++++------------------------------- 1 file changed, 78 insertions(+), 189 deletions(-) diff --git a/src/Entity/User.php b/src/Entity/User.php index 767e18e..a775c90 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -6,88 +6,53 @@ use App\Repository\UserRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -use JetBrains\PhpStorm\Pure; -use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; /** - * @ORM\Entity(repositoryClass=UserRepository::class) - * @UniqueEntity(fields={"username"}, message="There is already an account with this username") - * @ORM\HasLifecycleCallbacks - * @method string getUserIdentifier() + * */ +#[ORM\Entity(repositoryClass: UserRepository::class)] +#[ORM\Table(name: '`user`')] class User implements UserInterface, PasswordAuthenticatedUserInterface { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ - private $id; + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] + private int $id; - /** - * @ORM\Column(type="string", length=180, unique=true) - */ - private $username; + #[ORM\Column(type: 'string', length: 180, unique: true)] + private string $username; - /** - * @ORM\Column(type="json") - */ - private $roles = []; + #[ORM\Column(type: 'json')] + private array $roles = []; - /** - * @var string The hashed password - * @ORM\Column(type="string") - */ - private $password; + #[ORM\Column(type: 'string')] + private string $password; - /** - * @ORM\Column(type="string", length=255, nullable=true) - */ - private $firstName; + private string $plainPassword; - /** - * @ORM\Column(type="string", length=255, nullable=true) - */ - private $lastName; - /** - * @ORM\Column(type="string", length=255) - */ - private $email; + #[ORM\Column(type: 'string', length: 255)] + private string $email; - /** - * @ORM\Column(type="datetime") - */ - private $createdAt; + #[ORM\Column(type: 'string', length: 255, nullable: true)] + private ?string $firstName = ''; - /** - * @ORM\Column(type="datetime", nullable=true) - */ - private $lastLoginAt; + #[ORM\Column(type: 'string', length: 255, nullable: true)] + private ?string $lastName = ''; - /** - * @ORM\OneToMany(targetEntity=Blog::class, mappedBy="author") - */ - private $blogs; + #[ORM\ManyToMany(targetEntity: Projects::class, mappedBy: 'developer')] + private $projects; - /** - * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="author") - */ - private $comments; - - /** - * @ORM\Column(type="boolean") - */ - private $isVerified = false; - - #[Pure] public function __construct() + public function __construct() { - $this->blogs = new ArrayCollection(); - $this->comments = new ArrayCollection(); + $this->projects = new ArrayCollection(); } + /** + * @return null|string + */ public function __toString() { return $this->username; @@ -99,13 +64,16 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface } /** - * A visual identifier that represents this user. - * - * @see UserInterface + * @return string */ - public function getUsername(): string + public function getPlainPassword(): string { - return (string)$this->username; + return $this->plainPassword; + } + + public function getUsername(): ?string + { + return $this->username; } public function setUsername(string $username): self @@ -115,6 +83,16 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface return $this; } + /** + * A visual identifier that represents this user. + * + * @see UserInterface + */ + public function getUserIdentifier(): string + { + return $this->username; + } + /** * @see UserInterface */ @@ -124,7 +102,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface // guarantee every user at least has ROLE_USER $roles[] = 'ROLE_USER'; - return array_unique($roles); + return array_unique(array: $roles); } public function setRoles(array $roles): self @@ -135,7 +113,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface } /** - * @see UserInterface + * @see PasswordAuthenticatedUserInterface */ public function getPassword(): string { @@ -149,24 +127,32 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface return $this; } - /** - * Returning a salt is only needed, if you are not using a modern - * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml. - * - * @see UserInterface - */ - public function getSalt(): ?string - { - return null; - } - /** * @see UserInterface */ public function eraseCredentials() { - // If you store any temporary, sensitive data on the user, clear it here - // $this->plainPassword = null; + $this->plainPassword = ''; + } + + public function getEmail(): ?string + { + return $this->email; + } + + public function setEmail(string $email): self + { + $this->email = $email; + + return $this; + } + + /** + * @param string $plainPassword + */ + public function setPlainPassword(string $plainPassword): void + { + $this->plainPassword = $plainPassword; } public function getFirstName(): ?string @@ -193,127 +179,30 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface return $this; } - public function getEmail(): ?string - { - return $this->email; - } - - public function setEmail(string $email): self - { - $this->email = $email; - - return $this; - } - - public function getCreatedAt(): ?\DateTimeInterface - { - return $this->createdAt; - } - - public function setCreatedAt(\DateTimeInterface $createdAt): self - { - $this->createdAt = $createdAt; - - return $this; - } - - public function getLastLoginAt(): ?\DateTimeInterface - { - return $this->lastLoginAt; - } - - public function setLastLoginAt(?\DateTimeInterface $lastLoginAt): self - { - $this->lastLoginAt = $lastLoginAt; - - return $this; - } - /** - * @return Collection|Blog[] + * @return Collection */ - public function getBlogs(): Collection + public function getProjects(): Collection { - return $this->blogs; + return $this->projects; } - public function addBlog(Blog $blog): self + public function addProject(Projects $project): self { - if (!$this->blogs->contains($blog)) { - $this->blogs[] = $blog; - $blog->setAuthor($this); + if (!$this->projects->contains(element: $project)) { + $this->projects[] = $project; + $project->addDeveloper(developer: $this); } return $this; } - public function removeBlog(Blog $blog): self + public function removeProject(Projects $project): self { - if ($this->blogs->removeElement($blog)) { - // set the owning side to null (unless already changed) - if ($blog->getAuthor() === $this) { - $blog->setAuthor(null); - } + if ($this->projects->removeElement(element: $project)) { + $project->removeDeveloper(developer: $this); } return $this; } - - /** - * @return Collection|Comment[] - */ - public function getComments(): Collection - { - return $this->comments; - } - - public function addComment(Comment $comment): self - { - if (!$this->comments->contains($comment)) { - $this->comments[] = $comment; - $comment->setAuthor($this); - } - - return $this; - } - - public function removeComment(Comment $comment): self - { - if ($this->comments->removeElement($comment)) { - // set the owning side to null (unless already changed) - if ($comment->getAuthor() === $this) { - $comment->setAuthor(null); - } - } - - return $this; - } - - public function isVerified(): bool - { - return $this->isVerified; - } - - public function setIsVerified(bool $isVerified): self - { - $this->isVerified = $isVerified; - - return $this; - } - - public function __call(string $name, array $arguments) - { - // TODO: Implement @method string getUserIdentifier() - } - - /** - * Gets triggered only on insert - - * @ORM\PrePersist - */ - public function onPrePersist() - { - $this->createdAt = new \DateTime(); - } - }