Last active
June 9, 2020 08:18
-
-
Save erop/d090dabe04fc6ba4e3178353efcf07e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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