Ecodev /
graphql-doctrine
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace GraphQLTests\Doctrine\Blog\Model; |
||
| 6 | |||
| 7 | use DateTimeImmutable; |
||
| 8 | use Doctrine\ORM\Mapping as ORM; |
||
| 9 | use GraphQL\Doctrine\Attribute as API; |
||
| 10 | use GraphQLTests\Doctrine\Blog\Filtering\SearchOperatorType; |
||
| 11 | use GraphQLTests\Doctrine\Blog\Model\Special\NoInversedBy; |
||
| 12 | use GraphQLTests\Doctrine\Blog\Sorting\PostType; |
||
| 13 | use GraphQLTests\Doctrine\Blog\Sorting\UserName; |
||
| 14 | use GraphQLTests\Doctrine\Blog\Types\PostStatusType; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * A blog post with title and body. |
||
| 18 | */ |
||
| 19 | #[ORM\Entity] |
||
| 20 | #[API\Sorting(UserName::class)] |
||
| 21 | #[API\Sorting(PostType::class)] |
||
| 22 | #[API\Filter(field: 'custom', operator: SearchOperatorType::class, type: 'string')] |
||
| 23 | final class Post extends AbstractModel |
||
| 24 | { |
||
| 25 | public const STATUS_PRIVATE = 'private'; |
||
| 26 | public const STATUS_PUBLIC = 'public'; |
||
| 27 | |||
| 28 | #[ORM\Column(type: 'string', length: 50, options: ['default' => ''])] |
||
| 29 | private string $title = ''; |
||
| 30 | |||
| 31 | #[ORM\Column(type: 'text')] |
||
| 32 | private string $body = ''; |
||
| 33 | |||
| 34 | #[ORM\Column(type: 'datetime_immutable')] |
||
| 35 | private DateTimeImmutable $publicationDate; |
||
| 36 | |||
| 37 | #[API\FilterGroupCondition(type: '?GraphQLTests\Doctrine\Blog\Types\PostStatusType')] |
||
| 38 | #[ORM\Column(type: 'string', options: ['default' => self::STATUS_PRIVATE])] |
||
| 39 | private string $status = self::STATUS_PRIVATE; |
||
| 40 | |||
| 41 | #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'posts')] |
||
| 42 | private User $user; |
||
| 43 | |||
| 44 | #[ORM\ManyToOne(targetEntity: NoInversedBy::class)] |
||
| 45 | private NoInversedBy $noInversedBy; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 46 | |||
| 47 | /** |
||
| 48 | * Set title. |
||
| 49 | */ |
||
| 50 | public function setTitle(string $title): void |
||
| 51 | { |
||
| 52 | $this->title = $title; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get title. |
||
| 57 | */ |
||
| 58 | public function getTitle(): string |
||
| 59 | { |
||
| 60 | return $this->title; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Set the body. |
||
| 65 | */ |
||
| 66 | public function setBody(string $body): void |
||
| 67 | { |
||
| 68 | $this->body = $body; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Returns the body. |
||
| 73 | */ |
||
| 74 | #[API\Field(name: 'content', description: 'The post content.')] |
||
| 75 | public function getBody(): string |
||
| 76 | { |
||
| 77 | return $this->body; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set status. |
||
| 82 | */ |
||
| 83 | #[API\Input(type: PostStatusType::class)] |
||
| 84 | public function setStatus(string $status = self::STATUS_PUBLIC): void |
||
| 85 | { |
||
| 86 | $this->status = $status; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get status. |
||
| 91 | */ |
||
| 92 | #[API\Field(type: PostStatusType::class)] |
||
| 93 | public function getStatus(): string |
||
| 94 | { |
||
| 95 | return $this->status; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Set author of post. |
||
| 100 | */ |
||
| 101 | public function setUser(User $user): void |
||
| 102 | { |
||
| 103 | $this->user = $user; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get author of post. |
||
| 108 | */ |
||
| 109 | public function getUser(): User |
||
| 110 | { |
||
| 111 | return $this->user; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Set date of publication. |
||
| 116 | */ |
||
| 117 | public function setPublicationDate(DateTimeImmutable $publicationDate): void |
||
| 118 | { |
||
| 119 | $this->publicationDate = $publicationDate; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get date of publication. |
||
| 124 | */ |
||
| 125 | public function getPublicationDate(): DateTimeImmutable |
||
| 126 | { |
||
| 127 | return $this->publicationDate; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return string[] |
||
| 132 | */ |
||
| 133 | public function getWords(): array |
||
| 134 | { |
||
| 135 | return explode(' ', $this->getBody()); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param string[] $words |
||
| 140 | */ |
||
| 141 | public function hasWords($words): bool |
||
| 142 | { |
||
| 143 | return count(array_diff($words, $this->getWords())) > 0; |
||
| 144 | } |
||
| 145 | |||
| 146 | public function isLong(int $wordLimit = 50): bool |
||
| 147 | { |
||
| 148 | return count($this->getWords()) > $wordLimit; |
||
| 149 | } |
||
| 150 | |||
| 151 | public function isAllowedEditing(User $user): bool |
||
| 152 | { |
||
| 153 | return $this->getUser() === $user; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * This should be silently ignored. |
||
| 158 | */ |
||
| 159 | public function setNothing(): void {} |
||
| 160 | } |
||
| 161 |