<?php
namespace App\Entity\Bus;
use App\Entity\Station;
use App\Entity\User;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
/**
* @ORM\Entity(repositoryClass="App\Repository\Bus\RouteRepository")
* @ORM\Table(name="bus_route")
*/
class Route {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", name="route_name")
*/
private string $routeName;
/**
* @var ?Station
*
* @ORM\ManyToOne(targetEntity="App\Entity\Station", cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="origin", referencedColumnName="id")
* })
* @JMS\MaxDepth(1)
*/
private ?Station $origin;
/**
*
* @ORM\ManyToOne(targetEntity="App\Entity\Station", cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="destination", referencedColumnName="id")
* })
* @JMS\MaxDepth(1)
*/
private ?Station $destination = null;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Bus\Stop", mappedBy="route", cascade={"persist", "remove"})
*/
private Collection $stops;
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="created_by", referencedColumnName="id")
* })
* @JMS\MaxDepth(0)
*/
private ?User $createdBy = null;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime")
*/
private $departureTime;
/**
* @ORM\Column(type="datetime")
*/
private $eta;
public function __construct()
{
$this->stops = new ArrayCollection();
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id): void
{
$this->id = $id;
}
/**
* @return string
*/
public function getRouteName(): string
{
return $this->routeName;
}
/**
* @param string $routeName
*/
public function setRouteName(string $routeName): void
{
$this->routeName = $routeName;
}
/**
* @return ?Station
*/
public function getOrigin(): ?Station
{
return $this->origin;
}
/**
* @param ?Station $origin
*/
public function setOrigin(?Station $origin): void
{
$this->origin = $origin;
}
/**
* @return ?Station
*/
public function getDestination(): ?Station
{
return $this->destination;
}
/**
* @param ?Station $destination
*/
public function setDestination(?Station $destination): void
{
$this->destination = $destination;
}
/**
* @return Collection<int, Stop>
*/
public function getStops()
{
return $this->stops;
}
public function addStop(Stop $stop): self
{
$this->stops[] = $stop;
$stop->setCreatedAt(new DateTime());
$stop->setCreatedBy($this->getCreatedBy());
$stop->setRoute($this);
dump($stop);
return $this;
}
public function removeStop(Stop $stop): self
{
if ($this->stops->removeElement($stop)) {
// set the owning side to null (unless already changed)
if ($stop->getRoute() === $this) {
$stop->setRoute(null);
}
}
return $this;
}
/**
* @param Collection $stopsCollection
*/
public function setStop88s(Collection $stopsCollection): void
{
$originStop = new Stop();
$originStop->setStation($this->getOrigin());
$originStop->setDepartureTime($this->getDepartureTime());
$originStop->setRouteType('MAIN');
$originStop->setCreatedAt(new DateTime());
$originStop->setCreatedBy($this->getCreatedBy());
$originStop->setRoute($this);
$newStops[] = $originStop;
foreach ($stopsCollection as $index => $stop) {
$stop->setCreatedAt(new DateTime());
$stop->setCreatedBy($this->getCreatedBy());
$stop->setRoute($this);
$stops[] = $stop;
}
$destinationStop = new Stop();
$destinationStop->setCreatedAt(new DateTime());
$destinationStop->setCreatedBy($this->getCreatedBy());
$destinationStop->setRoute($this);
$destinationStop->setStation($this->getDestination());
$destinationStop->setDepartureTime($this->getEta());
$destinationStop->setRouteType('MAIN');
$newStops[] = $destinationStop;
dump($newStops);
$this->stops = $stops;
}
/**
* @return User
*/
public function getCreatedBy(): User
{
if(!$this->createdBy) return new User();
return $this->createdBy;
}
/**
* @param User $createdBy
*/
public function setCreatedBy(User $createdBy): void
{
$this->createdBy = $createdBy;
}
/**
* @return mixed
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* @param mixed $createdAt
*/
public function setCreatedAt($createdAt): void
{
$this->createdAt = $createdAt;
}
/**
* @return mixed
*/
public function getDepartureTime()
{
return $this->departureTime;
}
/**
* @param mixed $departureTime
*/
public function setDepartureTime($departureTime): void
{
$this->departureTime = $departureTime;
}
/**
* @return mixed
*/
public function getEta()
{
return $this->eta;
}
/**
* @param mixed $eta
*/
public function setEta($eta): void
{
$this->eta = $eta;
}
public function __toString()
{
// TODO: Implement __toString() method.
return $this->getRouteName();
}
}