Ecodev /
graphql-doctrine
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace GraphQLTests\Doctrine\Blog\Model; |
||
| 6 | |||
| 7 | use Doctrine\Common\Collections\ArrayCollection; |
||
| 8 | use Doctrine\Common\Collections\Collection; |
||
| 9 | use Doctrine\ORM\Mapping as ORM; |
||
| 10 | use GraphQL\Doctrine\Attribute as API; |
||
| 11 | use GraphQLTests\Doctrine\Blog\Repository\UserRepository; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * A blog author or visitor. |
||
| 15 | */ |
||
| 16 | #[ORM\Entity(repositoryClass: UserRepository::class)] |
||
| 17 | final class User extends AbstractModel |
||
| 18 | { |
||
| 19 | #[ORM\Column(name: 'custom_column_name', type: 'string', length: 50, options: ['default' => ''])] |
||
| 20 | private string $name = ''; |
||
| 21 | |||
| 22 | #[ORM\Column(type: 'string', length: 50, nullable: true)] |
||
| 23 | private ?string $email = null; |
||
| 24 | |||
| 25 | #[ORM\Column(name: 'password', type: 'string', length: 255)] |
||
| 26 | #[API\Exclude] |
||
| 27 | private string $password; |
||
| 28 | |||
| 29 | #[ORM\Column(type: 'boolean', options: ['default' => false])] |
||
| 30 | private bool $isAdministrator = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var Collection<int, Post> |
||
| 34 | */ |
||
| 35 | #[ORM\OneToMany(targetEntity: Post::class, mappedBy: 'user')] |
||
| 36 | private Collection $posts; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var Collection<int, Post> |
||
| 40 | */ |
||
| 41 | #[ORM\ManyToMany(targetEntity: Post::class)] |
||
| 42 | private Collection $favoritePosts; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 43 | |||
| 44 | #[ORM\ManyToOne(targetEntity: self::class)] |
||
| 45 | private ?User $manager = null; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructor. |
||
| 49 | */ |
||
| 50 | public function __construct(?int $id = null) |
||
| 51 | { |
||
| 52 | // This is a bad idea in real world, but we are just testing stuff here |
||
| 53 | if ($id) { |
||
|
0 ignored issues
–
show
The expression
$id of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
Loading history...
|
|||
| 54 | $this->id = $id; |
||
| 55 | } |
||
| 56 | |||
| 57 | $this->posts = new ArrayCollection(); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Set name. |
||
| 62 | */ |
||
| 63 | public function setName(string $name): void |
||
| 64 | { |
||
| 65 | $this->name = $name; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get the user real name. |
||
| 70 | */ |
||
| 71 | public function getName(): string |
||
| 72 | { |
||
| 73 | return $this->name; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Set a valid email or null. |
||
| 78 | */ |
||
| 79 | public function setEmail(?string $email): void |
||
| 80 | { |
||
| 81 | $this->email = $email; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get the validated email or null. |
||
| 86 | */ |
||
| 87 | public function getEmail(): ?string |
||
| 88 | { |
||
| 89 | return $this->email; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Encrypt and change the user password. |
||
| 94 | */ |
||
| 95 | public function setPassword(string $password): void |
||
| 96 | { |
||
| 97 | $this->password = password_hash($password, PASSWORD_DEFAULT) ?: ''; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns the hashed password. |
||
| 102 | */ |
||
| 103 | #[API\Exclude] |
||
| 104 | public function getPassword(): string |
||
| 105 | { |
||
| 106 | return $this->password; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Set whether the user is an administrator. |
||
| 111 | */ |
||
| 112 | #[API\Exclude] |
||
| 113 | public function setIsAdministrator(bool $isAdministrator): void |
||
| 114 | { |
||
| 115 | $this->isAdministrator = $isAdministrator; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get whether the user is an administrator. |
||
| 120 | */ |
||
| 121 | public function isAdministrator(): bool |
||
| 122 | { |
||
| 123 | return $this->isAdministrator; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns all posts of the specified status. |
||
| 128 | * |
||
| 129 | * @param null|string $status the status of posts as defined in \GraphQLTests\Doctrine\Blog\Model\Post |
||
| 130 | */ |
||
| 131 | public function getPosts( |
||
| 132 | #[API\Argument(type: '?GraphQLTests\Doctrine\Blog\Types\PostStatusType')] |
||
| 133 | ?string $status = Post::STATUS_PUBLIC, |
||
| 134 | ): Collection { |
||
| 135 | // Return unfiltered collection |
||
| 136 | if ($status === null) { |
||
| 137 | return $this->posts; |
||
| 138 | } |
||
| 139 | |||
| 140 | return $this->posts->filter(fn (Post $post) => $post->getStatus() === $status); |
||
| 141 | } |
||
| 142 | |||
| 143 | #[API\Field(type: 'GraphQLTests\Doctrine\Blog\Model\Post[]')] |
||
| 144 | public function getPostsWithIds(#[API\Argument(type: 'id[]')] array $ids): Collection |
||
| 145 | { |
||
| 146 | return $this->posts->filter(fn (Post $post) => in_array($post->getId(), $ids, true)); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function setManager(?self $manager): void |
||
| 150 | { |
||
| 151 | $this->manager = $manager; |
||
| 152 | } |
||
| 153 | |||
| 154 | public function getManager(): ?self |
||
| 155 | { |
||
| 156 | return $this->manager; |
||
| 157 | } |
||
| 158 | } |
||
| 159 |