Skip to content

Instantly share code, notes, and snippets.

@telless
Created March 23, 2018 09:51
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 telless/1fe5f15166caa7c84cd77f6c200cdb5e to your computer and use it in GitHub Desktop.
Save telless/1fe5f15166caa7c84cd77f6c200cdb5e to your computer and use it in GitHub Desktop.
Example symfony entity without typical getter\setter boilerplate
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
/**
* Class OtpState
* @package AppBundle\Entity
*
* @ORM\Entity(repositoryClass="AppBundle\Repository\OtpStateRepository")
* @ORM\Table(
* indexes={
* @ORM\Index(name="SEARCH_INDEX", columns={"code", "phone"}),
* @ORM\Index(name="OTP_ID_INDEX", columns={"otp_id"})
* }
* )
*
* @JMS\ExclusionPolicy("ALL")
*/
class OtpState
{
/**
* @var int
*
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var string
*
* @ORM\Column(type="string", length=45, options={"fixed" = true})
*/
private $otpId;
/**
* @var string
*
* @ORM\Column(type="string", length=6)
*
* @JMS\Expose()
*/
private $code;
/**
* @var string
*
* @ORM\Column(type="string", length=10)
*
* @JMS\Expose()
*/
private $phone;
/**
* @var int|null
*
* @ORM\Column(type="integer", nullable=true)
*/
private $orderId;
/**
* @var string|null
*
* @ORM\Column(type="string", length=10, nullable=true)
*/
private $orderType;
/**
* @var int|null
*
* @ORM\Column(type="integer", nullable=true)
*/
private $partnerId;
/**
* @var \DateTimeImmutable
*
* @ORM\Column(type="datetimetz_immutable")
*
* @JMS\Expose()
* @JMS\Type("DateTimeImmutable<'c'>")
*/
private $createdTime;
/**
* @var bool
*
* @ORM\Column(type="boolean")
*
* @JMS\Expose()
*/
private $validated = false;
/**
* @var \DateTimeImmutable|null
*
* @ORM\Column(type="datetimetz_immutable", nullable=true)
*
* @JMS\Expose()
* @JMS\Type("DateTimeImmutable<'c'>")
*/
private $validatedTime;
public function __construct(string $code, string $phone, string $otpId)
{
$this->code = $code;
$this->phone = $phone;
$this->otpId = $otpId;
$this->createdTime = $this->getCurrentTime();
}
public function checkOtpId(string $otpId): bool
{
return $this->otpId === $otpId;
}
public function bindOrder(BrokerOrderInterface $brokerOrder): void
{
$this->orderId = $brokerOrder->getId();
$this->orderType = $brokerOrder->getType();
$this->partnerId = $brokerOrder->getPartnerId();
}
public function validate(string $status): void
{
$this->validatedTime = $this->getCurrentTime();
if ($status === Otp::STATUS_SUCCESS) {
$this->validated = true;
}
}
private function getCurrentTime(): \DateTimeImmutable
{
return new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment