johnykvsky /
spisywarka
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Entity; |
||
| 4 | |||
| 5 | use Doctrine\ORM\Mapping as ORM; |
||
| 6 | use Gedmo\Mapping\Annotation as Gedmo; |
||
| 7 | use Assert\Assertion; |
||
| 8 | use Assert\AssertionFailedException; |
||
| 9 | use App\Traits\HasTimestamps; |
||
| 10 | use Ramsey\Uuid\UuidInterface; |
||
| 11 | use Ramsey\Uuid\Uuid; |
||
| 12 | use Swagger\Annotations as SWG; |
||
| 13 | use Doctrine\Common\Collections\Collection; |
||
|
0 ignored issues
–
show
|
|||
| 14 | |||
| 15 | /** |
||
| 16 | * @ORM\Table(name="category") |
||
| 17 | * @ORM\HasLifecycleCallbacks() |
||
| 18 | * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository") |
||
| 19 | * @Gedmo\SoftDeleteable(fieldName="deletedAt", hardDelete=false) |
||
| 20 | */ |
||
| 21 | class Category implements \JsonSerializable, UserAwareInterface |
||
| 22 | { |
||
| 23 | use HasTimestamps; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var UuidInterface |
||
| 27 | * @ORM\Id() |
||
| 28 | * @ORM\Column(type="uuid", unique=true) |
||
| 29 | * @ORM\GeneratedValue(strategy="NONE") |
||
| 30 | * @SWG\Property(description="UUID", type="string", readOnly=true) |
||
| 31 | */ |
||
| 32 | private $id; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @ORM\Column(type="string", length=255) |
||
| 36 | */ |
||
| 37 | private $name; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 41 | */ |
||
| 42 | private $description; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @ORM\Column(type="datetime", nullable=true) |
||
| 46 | */ |
||
| 47 | private $deletedAt; |
||
|
0 ignored issues
–
show
|
|||
| 48 | |||
| 49 | /** |
||
| 50 | * @Gedmo\Slug(fields={"name"}) |
||
| 51 | * @ORM\Column(length=128, unique=true) |
||
| 52 | */ |
||
| 53 | private $slug; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @ORM\OneToMany(targetEntity="App\Entity\Item", mappedBy="category") |
||
| 57 | */ |
||
| 58 | protected $items; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @ORM\ManyToOne(targetEntity="User", inversedBy="categories") |
||
| 62 | */ |
||
| 63 | private $user; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param UuidInterface $id |
||
| 67 | * @param string $name |
||
| 68 | * @param ?string $description |
||
| 69 | * @param User $user |
||
| 70 | * @throws \Assert\AssertionFailedException |
||
| 71 | */ |
||
| 72 | 27 | public function __construct( |
|
| 73 | UuidInterface $id, |
||
| 74 | string $name, |
||
| 75 | ?string $description, |
||
| 76 | User $user |
||
| 77 | ) |
||
| 78 | { |
||
| 79 | 27 | $this->setId($id); |
|
| 80 | 27 | $this->setName($name); |
|
| 81 | 27 | $this->setDescription($description); |
|
| 82 | 27 | $this->setUser($user); |
|
| 83 | 27 | } |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param UuidInterface $id |
||
| 87 | * @return Category |
||
| 88 | */ |
||
| 89 | 27 | public function setId(UuidInterface $id): self |
|
| 90 | { |
||
| 91 | 27 | $this->id = $id; |
|
| 92 | |||
| 93 | 27 | return $this; |
|
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return UuidInterface |
||
| 98 | */ |
||
| 99 | 14 | public function getId(): UuidInterface |
|
| 100 | { |
||
| 101 | 14 | return $this->id; |
|
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param User $user |
||
| 106 | * @return Category |
||
| 107 | */ |
||
| 108 | 27 | public function setUser(User $user): self |
|
| 109 | { |
||
| 110 | 27 | $this->user = $user; |
|
| 111 | |||
| 112 | 27 | return $this; |
|
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return User |
||
| 117 | */ |
||
| 118 | 4 | public function getUser(): User |
|
| 119 | { |
||
| 120 | 4 | return $this->user; |
|
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | 6 | public function getName(): string |
|
| 127 | { |
||
| 128 | 6 | return $this->name; |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $name |
||
| 133 | * @return Category |
||
| 134 | */ |
||
| 135 | 27 | public function setName(string $name): self |
|
| 136 | { |
||
| 137 | 27 | Assertion::notEmpty($name, 'Category name is required'); |
|
| 138 | 27 | Assertion::maxLength($name, 255, 'Category name must less than 255 characters'); |
|
| 139 | 27 | $this->name = $name; |
|
| 140 | |||
| 141 | 27 | return $this; |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | 6 | public function getDescription(): ?string |
|
| 148 | { |
||
| 149 | 6 | return $this->description; |
|
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function getSlug(): string |
||
| 156 | { |
||
| 157 | return $this->slug; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return Collection|null |
||
| 162 | */ |
||
| 163 | 1 | public function getItems(): ?Collection |
|
| 164 | { |
||
| 165 | 1 | return $this->items; |
|
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param ?string $description |
||
| 170 | * @return Category |
||
| 171 | */ |
||
| 172 | 27 | public function setDescription(?string $description): self |
|
| 173 | { |
||
| 174 | 27 | $this->description = $description; |
|
| 175 | |||
| 176 | 27 | return $this; |
|
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | 1 | public function hasItems(): bool |
|
| 183 | { |
||
| 184 | 1 | return !($this->getItems() === null || $this->getItems()->isEmpty()); |
|
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | 4 | public function jsonSerialize() |
|
| 191 | { |
||
| 192 | return [ |
||
| 193 | 4 | 'id' => $this->getId()->toString(), |
|
| 194 | 4 | 'name' => $this->getName(), |
|
| 195 | 4 | 'description' => $this->getDescription(), |
|
| 196 | 4 | 'userId' => $this->getUser()->getId()->toString(), |
|
| 197 | ]; |
||
| 198 | } |
||
| 199 | |||
| 200 | public static function checkOrderField(string $orderField): bool |
||
| 201 | { |
||
| 202 | if (!in_array($orderField, ['name'], true)) { |
||
| 203 | return false; |
||
| 204 | } |
||
| 205 | |||
| 206 | return true; |
||
| 207 | } |
||
| 208 | } |
||
| 209 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: