src/Entity/Bus/Route.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Bus;
  3. use App\Entity\Station;
  4. use App\Entity\User;
  5. use DateTime;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use JMS\Serializer\Annotation as JMS;
  10. /**
  11. * @ORM\Entity(repositoryClass="App\Repository\Bus\RouteRepository")
  12. * @ORM\Table(name="bus_route")
  13. */
  14. class Route {
  15. /**
  16. * @ORM\Id
  17. * @ORM\GeneratedValue(strategy="AUTO")
  18. * @ORM\Column(type="integer")
  19. */
  20. private $id;
  21. /**
  22. * @ORM\Column(type="string", name="route_name")
  23. */
  24. private string $routeName;
  25. /**
  26. * @var ?Station
  27. *
  28. * @ORM\ManyToOne(targetEntity="App\Entity\Station", cascade={"persist"})
  29. * @ORM\JoinColumns({
  30. * @ORM\JoinColumn(name="origin", referencedColumnName="id")
  31. * })
  32. * @JMS\MaxDepth(1)
  33. */
  34. private ?Station $origin;
  35. /**
  36. *
  37. * @ORM\ManyToOne(targetEntity="App\Entity\Station", cascade={"persist"})
  38. * @ORM\JoinColumns({
  39. * @ORM\JoinColumn(name="destination", referencedColumnName="id")
  40. * })
  41. * @JMS\MaxDepth(1)
  42. */
  43. private ?Station $destination = null;
  44. /**
  45. * @ORM\OneToMany(targetEntity="App\Entity\Bus\Stop", mappedBy="route", cascade={"persist", "remove"})
  46. */
  47. private Collection $stops;
  48. /**
  49. * @var User
  50. *
  51. * @ORM\ManyToOne(targetEntity="App\Entity\User")
  52. * @ORM\JoinColumns({
  53. * @ORM\JoinColumn(name="created_by", referencedColumnName="id")
  54. * })
  55. * @JMS\MaxDepth(0)
  56. */
  57. private ?User $createdBy = null;
  58. /**
  59. * @ORM\Column(type="datetime")
  60. */
  61. private $createdAt;
  62. /**
  63. * @ORM\Column(type="datetime")
  64. */
  65. private $departureTime;
  66. /**
  67. * @ORM\Column(type="datetime")
  68. */
  69. private $eta;
  70. public function __construct()
  71. {
  72. $this->stops = new ArrayCollection();
  73. }
  74. /**
  75. * @return mixed
  76. */
  77. public function getId()
  78. {
  79. return $this->id;
  80. }
  81. /**
  82. * @param mixed $id
  83. */
  84. public function setId($id): void
  85. {
  86. $this->id = $id;
  87. }
  88. /**
  89. * @return string
  90. */
  91. public function getRouteName(): string
  92. {
  93. return $this->routeName;
  94. }
  95. /**
  96. * @param string $routeName
  97. */
  98. public function setRouteName(string $routeName): void
  99. {
  100. $this->routeName = $routeName;
  101. }
  102. /**
  103. * @return ?Station
  104. */
  105. public function getOrigin(): ?Station
  106. {
  107. return $this->origin;
  108. }
  109. /**
  110. * @param ?Station $origin
  111. */
  112. public function setOrigin(?Station $origin): void
  113. {
  114. $this->origin = $origin;
  115. }
  116. /**
  117. * @return ?Station
  118. */
  119. public function getDestination(): ?Station
  120. {
  121. return $this->destination;
  122. }
  123. /**
  124. * @param ?Station $destination
  125. */
  126. public function setDestination(?Station $destination): void
  127. {
  128. $this->destination = $destination;
  129. }
  130. /**
  131. * @return Collection<int, Stop>
  132. */
  133. public function getStops()
  134. {
  135. return $this->stops;
  136. }
  137. public function addStop(Stop $stop): self
  138. {
  139. $this->stops[] = $stop;
  140. $stop->setCreatedAt(new DateTime());
  141. $stop->setCreatedBy($this->getCreatedBy());
  142. $stop->setRoute($this);
  143. dump($stop);
  144. return $this;
  145. }
  146. public function removeStop(Stop $stop): self
  147. {
  148. if ($this->stops->removeElement($stop)) {
  149. // set the owning side to null (unless already changed)
  150. if ($stop->getRoute() === $this) {
  151. $stop->setRoute(null);
  152. }
  153. }
  154. return $this;
  155. }
  156. /**
  157. * @param Collection $stopsCollection
  158. */
  159. public function setStop88s(Collection $stopsCollection): void
  160. {
  161. $originStop = new Stop();
  162. $originStop->setStation($this->getOrigin());
  163. $originStop->setDepartureTime($this->getDepartureTime());
  164. $originStop->setRouteType('MAIN');
  165. $originStop->setCreatedAt(new DateTime());
  166. $originStop->setCreatedBy($this->getCreatedBy());
  167. $originStop->setRoute($this);
  168. $newStops[] = $originStop;
  169. foreach ($stopsCollection as $index => $stop) {
  170. $stop->setCreatedAt(new DateTime());
  171. $stop->setCreatedBy($this->getCreatedBy());
  172. $stop->setRoute($this);
  173. $stops[] = $stop;
  174. }
  175. $destinationStop = new Stop();
  176. $destinationStop->setCreatedAt(new DateTime());
  177. $destinationStop->setCreatedBy($this->getCreatedBy());
  178. $destinationStop->setRoute($this);
  179. $destinationStop->setStation($this->getDestination());
  180. $destinationStop->setDepartureTime($this->getEta());
  181. $destinationStop->setRouteType('MAIN');
  182. $newStops[] = $destinationStop;
  183. dump($newStops);
  184. $this->stops = $stops;
  185. }
  186. /**
  187. * @return User
  188. */
  189. public function getCreatedBy(): User
  190. {
  191. if(!$this->createdBy) return new User();
  192. return $this->createdBy;
  193. }
  194. /**
  195. * @param User $createdBy
  196. */
  197. public function setCreatedBy(User $createdBy): void
  198. {
  199. $this->createdBy = $createdBy;
  200. }
  201. /**
  202. * @return mixed
  203. */
  204. public function getCreatedAt()
  205. {
  206. return $this->createdAt;
  207. }
  208. /**
  209. * @param mixed $createdAt
  210. */
  211. public function setCreatedAt($createdAt): void
  212. {
  213. $this->createdAt = $createdAt;
  214. }
  215. /**
  216. * @return mixed
  217. */
  218. public function getDepartureTime()
  219. {
  220. return $this->departureTime;
  221. }
  222. /**
  223. * @param mixed $departureTime
  224. */
  225. public function setDepartureTime($departureTime): void
  226. {
  227. $this->departureTime = $departureTime;
  228. }
  229. /**
  230. * @return mixed
  231. */
  232. public function getEta()
  233. {
  234. return $this->eta;
  235. }
  236. /**
  237. * @param mixed $eta
  238. */
  239. public function setEta($eta): void
  240. {
  241. $this->eta = $eta;
  242. }
  243. public function __toString()
  244. {
  245. // TODO: Implement __toString() method.
  246. return $this->getRouteName();
  247. }
  248. }