diff --git a/src/Entity/Brand.php b/src/Entity/Brand.php index c3282c9..eaaae06 100644 --- a/src/Entity/Brand.php +++ b/src/Entity/Brand.php @@ -2,7 +2,6 @@ namespace App\Entity; -use App\Repository\BrandRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; @@ -39,23 +38,6 @@ class Brand return $this->name; } - public function getId(): ?int - { - return $this->id; - } - - public function getName(): ?string - { - return $this->name; - } - - public function setName(string $name): self - { - $this->name = $name; - - return $this; - } - /** * @return Collection */ diff --git a/src/Entity/BrandCategory.php b/src/Entity/BrandCategory.php index afbc4ba..6c8f718 100644 --- a/src/Entity/BrandCategory.php +++ b/src/Entity/BrandCategory.php @@ -7,23 +7,13 @@ use Doctrine\ORM\Mapping as ORM; #[ORM\Table(name: 'brand_category', schema: 'collection')] #[ORM\Entity] class BrandCategory { + use GetSetTrait; + #[ORM\Id] #[ORM\Column(name: 'category_name')] #[ORM\OrderBy(['name' => 'asc'])] private string $name; - public function getName(): ?string - { - return $this->name; - } - - public function setName(string $name): self - { - $this->name = $name; - - return $this; - } - public function __toString(): string { return $this->name; diff --git a/src/Entity/Cpu.php b/src/Entity/Cpu.php index c134f91..f394194 100644 --- a/src/Entity/Cpu.php +++ b/src/Entity/Cpu.php @@ -2,7 +2,6 @@ namespace App\Entity; -use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Table(name: 'cpu', schema: 'collection')] @@ -11,29 +10,12 @@ class Cpu { use GetSetTrait; #[ORM\Column(name: 'id', type: 'integer', nullable: FALSE)] - #[ORM\Id] - #[ORM\GeneratedValue(strategy: 'IDENTITY')] - private int $id; + #[ORM\Id] + #[ORM\GeneratedValue(strategy: 'IDENTITY')] + private int $id; #[ORM\ManyToOne(targetEntity: 'Brand')] - #[ORM\OrderBy(['name' => 'asc'])] - #[ORM\JoinColumn(name: 'brand_id', referencedColumnName: 'id', nullable: FALSE)] - private Brand $brand; - - public function getId(): ?int - { - return $this->id; - } - - public function getBrand(): ?Brand - { - return $this->brand; - } - - public function setBrand(?Brand $brand): self - { - $this->brand = $brand; - - return $this; - } + #[ORM\OrderBy(['name' => 'asc'])] + #[ORM\JoinColumn(name: 'brand_id', referencedColumnName: 'id', nullable: FALSE)] + private Brand $brand; } diff --git a/src/Entity/GetSetTrait.php b/src/Entity/GetSetTrait.php index 09d6021..93c021a 100644 --- a/src/Entity/GetSetTrait.php +++ b/src/Entity/GetSetTrait.php @@ -7,53 +7,66 @@ use InvalidArgumentException; /** * Remove the need for all the Doctrine getter/setter Entity boilerplate */ -trait GetSetTrait -{ - public function __call(string $name, array $arguments): mixed - { - if (method_exists($this, $name)) { - return $this->{$name}(...$arguments); - } +trait GetSetTrait { + public function __get(string $name): mixed + { + if (property_exists($this, $name)) + { + return $this->$name; + } + + return NULL; + } + + public function __set(string $name, mixed $value): void + { + if ( ! property_exists($this, $name)) + { + throw new InvalidArgumentException("Undefined property: {$name}"); + } + + $this->$name = $value; + } + + public function __call(string $name, array $arguments): mixed + { + if (method_exists($this, $name)) + { + return $this->$name(...$arguments); + } // Getters if (empty($arguments)) { - // Apparently Doctrine first tries the method with the same - // name as the property + // The property as a method is required for Twig it appears if (property_exists($this, $name)) { - return $this->{$name}; + return $this->$name; } if (str_starts_with($name, 'get')) { - $var = lcfirst(substr($name, 3)); - if (property_exists($this, $var)) - { - return $this->{$var}; - } + return $this->__get(lcfirst(substr($name, 3))); } if (str_starts_with($name, 'is')) { - $var = lcfirst(substr($name, 2)); - if (property_exists($this, $var)) - { - return $this->{$var}; - } + return $this->__get(lcfirst(substr($name, 2))); } + + throw new InvalidArgumentException("Undefined method: {$name}"); } // Setters - if (str_starts_with($name, 'set')) { - $var = lcfirst(substr($name, 3)); - if (property_exists($this, $var)) { - $this->{$name} = $arguments[0]; - } + if (str_starts_with($name, 'set')) + { + $var = lcfirst(substr($name, 3)); - return $this; - } + $this->__set($var, ...$arguments); - throw new InvalidArgumentException("Undefined method: {$name}"); - } + return $this; + } + + throw new InvalidArgumentException("Undefined method: {$name}"); + } } diff --git a/src/Entity/Gpu.php b/src/Entity/Gpu.php index 8039221..9fbcd6d 100644 --- a/src/Entity/Gpu.php +++ b/src/Entity/Gpu.php @@ -113,7 +113,7 @@ class Gpu private ?string $shaderModel; #[ORM\Column(name: 'link')] - private readonly string $link; + private string $link; #[ORM\Column(name: 'count', nullable: FALSE)] private int $count = 1; diff --git a/src/Entity/GpuCore.php b/src/Entity/GpuCore.php index 612a493..60a80ff 100644 --- a/src/Entity/GpuCore.php +++ b/src/Entity/GpuCore.php @@ -50,105 +50,4 @@ class GpuCore implements Stringable return "{$name} - [{$this->brand}] $this->generationName"; } - - public function getId(): ?int - { - return $this->id; - } - - public function getName(): ?string - { - return $this->name; - } - - public function setName(string $name): self - { - $this->name = $name; - - return $this; - } - - public function getVariant(): ?string - { - return $this->variant; - } - - public function setVariant(string $variant): self - { - $this->variant = $variant; - - return $this; - } - - public function getGenerationName(): ?string - { - return $this->generationName; - } - - public function setGenerationName(string $generationName): self - { - $this->generationName = $generationName; - - return $this; - } - - public function getGenerationLink(): ?string - { - return $this->generationLink; - } - - public function setGenerationLink(?string $generationLink): self - { - $this->generationLink = $generationLink; - - return $this; - } - - public function getArchitecture(): ?string - { - return $this->architecture; - } - - public function setArchitecture(string $architecture): self - { - $this->architecture = $architecture; - - return $this; - } - - public function getArchitectureLink(): ?string - { - return $this->architectureLink; - } - - public function setArchitectureLink(string $architectureLink): self - { - $this->architectureLink = $architectureLink; - - return $this; - } - - public function getProcessNode(): ?int - { - return $this->processNode; - } - - public function setProcessNode(int $processNode): self - { - $this->processNode = $processNode; - - return $this; - } - - public function getBrand(): ?Brand - { - return $this->brand; - } - - public function setBrand(?Brand $brand): self - { - $this->brand = $brand; - - return $this; - } } diff --git a/src/Form/GpuType.php b/src/Form/GpuType.php index eb64632..ae0c24b 100644 --- a/src/Form/GpuType.php +++ b/src/Form/GpuType.php @@ -2,7 +2,7 @@ namespace App\Form; -use App\Enum\SlotKeyEnum; +use App\Enum\{CardBusEnum, SlotKeyEnum}; use App\Entity\{Brand, Gpu, GpuCore}; use Doctrine\ORM\EntityRepository; use Symfony\Bridge\Doctrine\Form\Type\EntityType;