Last active
May 19, 2017 09:57
-
-
Save Itach1Uchixa/1c5cd1489d440bb3a612f33b23193744 to your computer and use it in GitHub Desktop.
How to avoid setters
This file contains 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 | |
/** | |
* @author Kakhramonov Javlonbek <kakjavlon@gmail.com> | |
*/ | |
namespace Module\Entity; | |
use Application\Entity\TranslatableEntityTrait; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\ORM\Mapping as ORM; | |
use Gedmo\Mapping\Annotation as Gedmo; | |
/** | |
* @ORM\MappedSuperclass | |
*/ | |
abstract class AbstractClient | |
{ | |
use TranslatableEntityTrait; | |
/** | |
* @var Account | |
* @ORM\OneToOne(targetEntity="Account", cascade={"persist", "remove"}) | |
* @ORM\JoinColumn(name="account_id", onDelete="CASCADE") | |
*/ | |
protected $account; | |
/** | |
* @var string | |
* @ORM\Column(name="`first_name`", type="string", nullable=true) | |
*/ | |
protected $firstName; | |
/** | |
* @var string | |
* @ORM\Column(name="`last_name`", type="string", nullable=true) | |
*/ | |
protected $lastName; | |
/** | |
* @var string | |
* @ORM\Column(name="`third_name`", type="string", nullable=true) | |
*/ | |
protected $thirdName; | |
/** | |
* @var \DateTime | |
* @ORM\Column(name="`birthday`", type="datetimetz", nullable=true) | |
*/ | |
protected $birthday; | |
/** | |
* @var ArrayCollection | |
* @ORM\ManyToMany(targetEntity="PhoneNumber", cascade={"persist"}) | |
* @ORM\JoinTable( | |
* joinColumns={@ORM\JoinColumn(name="client_id", referencedColumnName="id", onDelete="CASCADE")}, | |
* inverseJoinColumns={@ORM\JoinColumn(name="phone_number", referencedColumnName="id", onDelete="CASCADE", unique=true)} | |
* ) | |
*/ | |
protected $phones; | |
/** | |
* @var \Users\Entity\User | |
* @ORM\OneToOne(targetEntity="\Users\Entity\User") | |
* @ORM\JoinColumn(name="user_id", onDelete="CASCADE") | |
*/ | |
protected $user; | |
/** | |
* AbstractClient constructor. | |
*/ | |
public function __construct() | |
{ | |
$this->phones = new ArrayCollection(); | |
} | |
/** | |
* @return Account | |
*/ | |
public function getAccount() | |
{ | |
return $this->account; | |
} | |
/** | |
* @param Account $account | |
* @return AbstractClient | |
*/ | |
public function setAccount($account) | |
{ | |
$this->account = $account; | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function getFirstName() | |
{ | |
return $this->firstName; | |
} | |
/** | |
* @param string $firstName | |
* @return AbstractClient | |
*/ | |
public function setFirstName($firstName) | |
{ | |
$this->firstName = $firstName; | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function getLastName() | |
{ | |
return $this->lastName; | |
} | |
/** | |
* @param string $lastName | |
* @return AbstractClient | |
*/ | |
public function setLastName($lastName) | |
{ | |
$this->lastName = $lastName; | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function getThirdName() | |
{ | |
return $this->thirdName; | |
} | |
/** | |
* @param string $thirdName | |
* @return AbstractClient | |
*/ | |
public function setThirdName($thirdName) | |
{ | |
$this->thirdName = $thirdName; | |
return $this; | |
} | |
/** | |
* @return \DateTime | |
*/ | |
public function getBirthday() | |
{ | |
return $this->birthday; | |
} | |
/** | |
* @param \DateTime $birthday | |
* @return AbstractClient | |
*/ | |
public function setBirthday($birthday) | |
{ | |
$this->birthday = $birthday; | |
return $this; | |
} | |
/** | |
* @return ArrayCollection | |
*/ | |
public function getPhones() | |
{ | |
return $this->phones; | |
} | |
/** | |
* @param ArrayCollection $phones | |
* @return AbstractClient | |
*/ | |
public function setPhones($phones) | |
{ | |
$this->phones = $phones; | |
return $this; | |
} | |
/** | |
* @return \Users\Entity\User | |
*/ | |
public function getUser() | |
{ | |
return $this->user; | |
} | |
/** | |
* @param \Users\Entity\User $user | |
* @return AbstractClient | |
*/ | |
public function setUser($user) | |
{ | |
$this->user = $user; | |
return $this; | |
} | |
} |
This file contains 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 | |
/** | |
* @author Kakhramonov Javlonbek <kakjavlon@gmail.com> | |
*/ | |
namespace Module\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Module\Exception; | |
use Users\Entity\User; | |
use Application\Uuid; | |
/** | |
* @ORM\Entity | |
* @ORM\Table(name="drivers") | |
*/ | |
class Driver extends AbstractClient | |
{ | |
/** | |
* @var string | |
* @ORM\Id | |
* @ORM\Column(type="string", length=36) | |
*/ | |
protected $id; | |
/** | |
* @var DriverTariff | |
* @ORM\ManyToOne(targetEntity="DriverTariff") | |
* @ORM\JoinColumn(name="tariff_id", onDelete="SET NULL") | |
*/ | |
protected $tariff; | |
/** | |
* @var Vehicle | |
* @ORM\OneToOne(targetEntity="Vehicle", inversedBy="driver") | |
* @ORM\JoinColumn(name="vehicle_id", onDelete="SET NULL") | |
*/ | |
protected $vehicle; | |
/** | |
* @var Dealer | |
* @ORM\ManyToOne(targetEntity="Dealer", inversedBy="drivers") | |
* @ORM\JoinColumn(name="dealer_id", onDelete="SET NULL") | |
*/ | |
protected $dealer; | |
/** | |
* @param array $options | |
* @return static | |
*/ | |
public static function factory(array $options = []) | |
{ | |
$instance = new static(); | |
if (!empty($options)) { | |
$instance->exchangeArray($options); | |
} | |
return $instance; | |
} | |
public function __construct() | |
{ | |
parent::__construct(); | |
$this->id = Uuid::v4(); | |
} | |
/** | |
* Returns array representation of object | |
* | |
* @return array | |
*/ | |
public function toArray() | |
{ | |
$array = ['id' => $this->getId()]; | |
if ($this->getAccount()) { | |
$array['account'] = $this->getAccount()->getId(); | |
} | |
if ($this->getFirstName()) { | |
$array['firstName'] = $this->getFirstName(); | |
} | |
if ($this->getLastName()) { | |
$array['lastName'] = $this->getLastName(); | |
} | |
if ($this->getThirdName()) { | |
$array['thirdName'] = $this->getThirdName(); | |
} | |
if ($this->getBirthday()) { | |
$array['birthday'] = $this->getBirthday()->format('c'); | |
} | |
if ($this->getPhones()) { | |
/** | |
* @var PhoneNumber $phone | |
*/ | |
foreach ($this->getPhones() as $phone) { | |
$array['phones'] = $phone->getNumber(); | |
} | |
} | |
if ($this->getUser()) { | |
$array['user'] = $this->getUser()->getId(); | |
} | |
if ($this->getTariff()) { | |
$array['tariff'] = $this->getTariff()->getId(); | |
} | |
if ($this->getVehicle()) { | |
$array['vehicle'] = $this->getVehicle()->getId(); | |
} | |
if ($this->getDealer()) { | |
$array['dealer'] = $this->getDealer()->getId(); | |
} | |
return $array; | |
} | |
/** | |
* Sets array to object | |
* | |
* @param array $data | |
* @return self | |
* @throws Exception\InvalidArgumentException | |
*/ | |
public function exchangeArray(array $data) | |
{ | |
if (isset($data['account']) && $data['account'] instanceof Account) { | |
$this->setAccount($data['account']); | |
} elseif (!$data['account'] instanceof Account) { | |
throw new Exception\InvalidArgumentException('account must be instance of '. Account::class); | |
} | |
if (isset($data['firstName'])) { | |
$this->setFirstName($data['firstName']); | |
} | |
if (isset($data['lastName'])) { | |
$this->setFirstName($data['lastName']); | |
} | |
if (isset($data['thirdName'])) { | |
$this->setFirstName($data['thirdName']); | |
} | |
if (isset($data['birthday']) && $data['birthday'] instanceof \DateTime) { | |
$this->setBirthday($data['birthday']); | |
} elseif (!$data['birthday'] instanceof \DateTime) { | |
throw new Exception\InvalidArgumentException("birthday must be instance of DateTime"); | |
} | |
if (isset($data['phones']) && !empty($data['phones'])) { | |
/** | |
* @var PhoneNumber $phone | |
*/ | |
foreach ($data['phones'] as $phone) { | |
$this->getPhones()->add($phone); | |
} | |
} | |
if (isset($data['user']) && $data['user'] instanceof User) { | |
$this->setUser($data['user']); | |
} elseif (!$data['user'] instanceof User) { | |
throw new Exception\InvalidArgumentException("user must be instance of ". User::class); | |
} | |
if (isset($data['tariff']) && $data['tariff'] instanceof DriverTariff) { | |
$this->setUser($data['tariff']); | |
} elseif (!$data['tariff'] instanceof DriverTariff) { | |
throw new Exception\InvalidArgumentException("tariff must be instance of ". DriverTariff::class); | |
} | |
if (isset($data['vehicle']) && $data['vehicle'] instanceof Vehicle) { | |
$this->setUser($data['vehicle']); | |
} elseif (!$data['vehicle'] instanceof Vehicle) { | |
throw new Exception\InvalidArgumentException("vehicle must be instance of ". Vehicle::class); | |
} | |
if (isset($data['dealer']) && $data['dealer'] instanceof Dealer) { | |
$this->setUser($data['dealer']); | |
} elseif (!$data['dealer'] instanceof Dealer) { | |
throw new Exception\InvalidArgumentException("dealer must be instance of ". Dealer::class); | |
} | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* @return DriverTariff | |
*/ | |
public function getTariff() | |
{ | |
return $this->tariff; | |
} | |
/** | |
* @param DriverTariff $tariff | |
* @return Driver | |
*/ | |
public function setTariff($tariff) | |
{ | |
$this->tariff = $tariff; | |
return $this; | |
} | |
/** | |
* @return Vehicle | |
*/ | |
public function getVehicle() | |
{ | |
return $this->vehicle; | |
} | |
/** | |
* @param Vehicle $vehicle | |
* @return Driver | |
*/ | |
public function setVehicle($vehicle) | |
{ | |
$this->vehicle = $vehicle; | |
return $this; | |
} | |
/** | |
* @return Dealer | |
*/ | |
public function getDealer() | |
{ | |
return $this->dealer; | |
} | |
/** | |
* @param Dealer $dealer | |
* @return Driver | |
*/ | |
public function setDealer($dealer) | |
{ | |
$this->dealer = $dealer; | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment