Skip to content

Instantly share code, notes, and snippets.

@erop
Last active June 9, 2020 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erop/d090dabe04fc6ba4e3178353efcf07e9 to your computer and use it in GitHub Desktop.
Save erop/d090dabe04fc6ba4e3178353efcf07e9 to your computer and use it in GitHub Desktop.
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use MyVendor\Contracts\Entity\VehicleInterface;
use MyVendor\Contracts\Entity\VehicleOwnerInterface;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ApiResource(
* normalizationContext={"groups"={"vehicle:read"}},
* denormalizationContext={"groups"={"vehicle:write"}}
* )
* @ORM\Entity(repositoryClass="App\Repository\VehicleRepository")
* @ORM\Table(name="vehicles")
* @Assert\Expression(expression="this.hasSingleOwner()", message="Владельцем автомобиля должно быть либо физическое, либо юридическое лицо")
*/
class Vehicle implements VehicleInterface
{
use TimestampableEntity;
use SoftDeleteableEntity;
use OneOfTrait;
/**
* @ORM\Id()
* @ORM\Column(type="uuid")
*/
private UuidInterface $id;
/**
* @ORM\Column(type="integer")
* @Groups({"vehicle:read", "vehicle:write"})
*/
private int $year;
/**
* @ORM\Column(type="string", nullable=true)
* @Groups({"vehicle:read", "vehicle:write"})
*/
private ?string $plate;
/**
* @ORM\Column(type="string", length=17)
* @Groups({"vehicle:read", "vehicle:write"})
*/
private ?string $vin = null;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\VehicleModel")
* @ORM\JoinColumn(nullable=false)
* @Groups({"vehicle:read", "vehicle:write"})
*/
private VehicleModel $model;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Person", cascade={"persist"})
* @ORM\JoinColumn(name="person_id")
* @Groups({"vehicle:write"})
* @Assert\Valid()
*/
private ?Person $ownerPerson = null;
public function __construct(VehicleModel $model, int $year)
{
$this->id = Uuid::uuid4();
$this->model = $model;
$this->year = $year;
}
public function getOwnerPerson(): ?Person
{
return $this->ownerPerson;
}
public function setOwnerPerson(?Person $ownerPerson): self
{
$this->ownerPerson = $ownerPerson;
return $this;
}
/**
* @Groups({"vehicle:read"})
*/
public function getOwner(): VehicleOwnerInterface
{
return $this->getOneOf(VehicleOwnerInterface::class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment