Remove getters/setters from other related entities

This commit is contained in:
Timothy Warren 2022-10-14 11:54:45 -04:00
parent 05adea010e
commit 2fd60cb4b3
7 changed files with 52 additions and 186 deletions

View File

@ -2,7 +2,6 @@
namespace App\Entity; namespace App\Entity;
use App\Repository\BrandRepository;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
@ -39,23 +38,6 @@ class Brand
return $this->name; 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<int, BrandCategory> * @return Collection<int, BrandCategory>
*/ */

View File

@ -7,23 +7,13 @@ use Doctrine\ORM\Mapping as ORM;
#[ORM\Table(name: 'brand_category', schema: 'collection')] #[ORM\Table(name: 'brand_category', schema: 'collection')]
#[ORM\Entity] #[ORM\Entity]
class BrandCategory { class BrandCategory {
use GetSetTrait;
#[ORM\Id] #[ORM\Id]
#[ORM\Column(name: 'category_name')] #[ORM\Column(name: 'category_name')]
#[ORM\OrderBy(['name' => 'asc'])] #[ORM\OrderBy(['name' => 'asc'])]
private string $name; 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 public function __toString(): string
{ {
return $this->name; return $this->name;

View File

@ -2,7 +2,6 @@
namespace App\Entity; namespace App\Entity;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
#[ORM\Table(name: 'cpu', schema: 'collection')] #[ORM\Table(name: 'cpu', schema: 'collection')]
@ -19,21 +18,4 @@ class Cpu {
#[ORM\OrderBy(['name' => 'asc'])] #[ORM\OrderBy(['name' => 'asc'])]
#[ORM\JoinColumn(name: 'brand_id', referencedColumnName: 'id', nullable: FALSE)] #[ORM\JoinColumn(name: 'brand_id', referencedColumnName: 'id', nullable: FALSE)]
private Brand $brand; 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;
}
} }

View File

@ -7,49 +7,62 @@ use InvalidArgumentException;
/** /**
* Remove the need for all the Doctrine getter/setter Entity boilerplate * Remove the need for all the Doctrine getter/setter Entity boilerplate
*/ */
trait GetSetTrait 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 public function __call(string $name, array $arguments): mixed
{ {
if (method_exists($this, $name)) { if (method_exists($this, $name))
return $this->{$name}(...$arguments); {
return $this->$name(...$arguments);
} }
// Getters // Getters
if (empty($arguments)) if (empty($arguments))
{ {
// Apparently Doctrine first tries the method with the same // The property as a method is required for Twig it appears
// name as the property
if (property_exists($this, $name)) if (property_exists($this, $name))
{ {
return $this->{$name}; return $this->$name;
} }
if (str_starts_with($name, 'get')) if (str_starts_with($name, 'get'))
{ {
$var = lcfirst(substr($name, 3)); return $this->__get(lcfirst(substr($name, 3)));
if (property_exists($this, $var))
{
return $this->{$var};
}
} }
if (str_starts_with($name, 'is')) if (str_starts_with($name, 'is'))
{ {
$var = lcfirst(substr($name, 2)); return $this->__get(lcfirst(substr($name, 2)));
if (property_exists($this, $var))
{
return $this->{$var};
}
} }
throw new InvalidArgumentException("Undefined method: {$name}");
} }
// Setters // Setters
if (str_starts_with($name, 'set')) { if (str_starts_with($name, 'set'))
{
$var = lcfirst(substr($name, 3)); $var = lcfirst(substr($name, 3));
if (property_exists($this, $var)) {
$this->{$name} = $arguments[0]; $this->__set($var, ...$arguments);
}
return $this; return $this;
} }

View File

@ -113,7 +113,7 @@ class Gpu
private ?string $shaderModel; private ?string $shaderModel;
#[ORM\Column(name: 'link')] #[ORM\Column(name: 'link')]
private readonly string $link; private string $link;
#[ORM\Column(name: 'count', nullable: FALSE)] #[ORM\Column(name: 'count', nullable: FALSE)]
private int $count = 1; private int $count = 1;

View File

@ -50,105 +50,4 @@ class GpuCore implements Stringable
return "{$name} - [{$this->brand}] $this->generationName"; 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;
}
} }

View File

@ -2,7 +2,7 @@
namespace App\Form; namespace App\Form;
use App\Enum\SlotKeyEnum; use App\Enum\{CardBusEnum, SlotKeyEnum};
use App\Entity\{Brand, Gpu, GpuCore}; use App\Entity\{Brand, Gpu, GpuCore};
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;