1 <?php
declare(strict_types
=1);
4 * this file is part of the rapsys packbundle package.
6 * (c) raphaël gertz <symfony@rapsys.eu>
8 * for the full copyright and license information, please view the license
9 * file that was distributed with this source code.
12 namespace Rapsys\AirBundle\Entity
;
14 use Doctrine\Common\Collections\ArrayCollection
;
15 use Doctrine\ORM\Event\PreUpdateEventArgs
;
89 private $temperaturemin;
94 private $temperaturemax;
114 private $application;
127 * @var ArrayCollection
129 private $applications;
134 public function __construct() {
138 $this->length
= null;
140 $this->premium
= null;
141 $this->rainfall
= null;
142 $this->rainrisk
= null;
143 $this->realfeel
= null;
144 $this->realfeelmin
= null;
145 $this->realfeelmax
= null;
146 $this->temperature
= null;
147 $this->temperaturemin
= null;
148 $this->temperaturemax
= null;
149 $this->locked
= null;
150 $this->created
= new \
DateTime('now');
151 $this->updated
= new \
DateTime('now');
152 $this->application
= null;
153 $this->applications
= new ArrayCollection();
161 public function getId(): int {
168 * @param \DateTime $date
172 public function setDate(\DateTime
$date): Session
{
183 public function getDate(): \DateTime
{
190 * @param \DateTime $begin
194 public function setBegin(?\DateTime
$begin): Session
{
195 $this->begin
= $begin;
205 public function getBegin(): ?\DateTime
{
214 public function getStart(): \DateTime
{
216 if ($this->start
!== null) {
221 $this->start
= clone $this->date
;
223 //Check if after slot
224 //XXX: id=4 <=> title=After
225 if ($this->slot
->getId() == 4) {
227 $this->start
->add(new \
DateInterval('P1D'));
231 if ($this->begin
!== null) {
233 $this->start
->setTime(intval($this->begin
->format('H')), intval($this->begin
->format('i')), intval($this->begin
->format('s')));
243 * @param \DateTime $length
247 public function setLength(?\DateTime
$length): Session
{
248 $this->length
= $length;
258 public function getLength(): ?\DateTime
{
259 return $this->length
;
267 public function getStop(): \DateTime
{
269 if ($this->stop
!== null) {
274 $this->stop
= clone $this->getStart();
277 if ($this->length
!== null) {
279 $this->stop
->add(new \
DateInterval('PT'.$this->length
->format('H').'H'.$this->length
->format('i').'M'.$this->length
->format('s').'S'));
289 * @param bool $premium
293 public function setPremium(bool $premium): Session
{
294 $this->premium
= $premium;
304 public function getPremium(): bool {
305 return $this->premium
;
311 * @param float $rainfall
315 public function setRainfall(?float $rainfall): Session
{
316 $this->rainfall
= $rainfall;
326 public function getRainfall(): ?float {
327 return $this->rainfall
;
333 * @param float $rainrisk
337 public function setRainrisk(?float $rainrisk): Session
{
338 $this->rainrisk
= $rainrisk;
348 public function getRainrisk(): ?float {
349 return $this->rainrisk
;
355 * @param float $realfeel
359 public function setRealfeel(?float $realfeel): Session
{
360 $this->realfeel
= $realfeel;
370 public function getRealfeel(): ?float {
371 return $this->realfeel
;
377 * @param float $realfeelmin
381 public function setRealfeelmin(?float $realfeelmin): Session
{
382 $this->realfeelmin
= $realfeelmin;
392 public function getRealfeelmin(): ?float {
393 return $this->realfeelmin
;
399 * @param float $realfeelmax
403 public function setRealfeelmax(?float $realfeelmax): Session
{
404 $this->realfeelmax
= $realfeelmax;
414 public function getRealfeelmax(): ?float {
415 return $this->realfeelmax
;
421 * @param float $temperature
425 public function setTemperature(?float $temperature): Session
{
426 $this->temperature
= $temperature;
436 public function getTemperature(): ?float {
437 return $this->temperature
;
443 * @param float $temperaturemin
447 public function setTemperaturemin(?float $temperaturemin): Session
{
448 $this->temperaturemin
= $temperaturemin;
458 public function getTemperaturemin(): ?float {
459 return $this->temperaturemin
;
465 * @param float $temperaturemax
469 public function setTemperaturemax(?float $temperaturemax): Session
{
470 $this->temperaturemax
= $temperaturemax;
480 public function getTemperaturemax(): ?float {
481 return $this->temperaturemax
;
487 * @param \DateTime $locked
491 public function setLocked(?\DateTime
$locked): Session
{
492 $this->locked
= $locked;
502 public function getLocked(): ?\DateTime
{
503 return $this->locked
;
509 * @param \DateTime $created
513 public function setCreated(\DateTime
$created): Session
{
514 $this->created
= $created;
524 public function getCreated(): \DateTime
{
525 return $this->created
;
531 * @param \DateTime $updated
535 public function setUpdated(\DateTime
$updated): Session
{
536 $this->updated
= $updated;
546 public function getUpdated(): \DateTime
{
547 return $this->updated
;
553 * @param Application $application
557 public function addApplication(Application
$application): Session
{
558 $this->applications
[] = $application;
566 * @param Application $application
568 public function removeApplication(Application
$application): bool {
569 return $this->applications
->removeElement($application);
575 * @return ArrayCollection
577 public function getApplications(): ArrayCollection
{
578 return $this->applications
;
584 * @param Location $location
588 public function setLocation(Location
$location): Session
{
589 $this->location
= $location;
599 public function getLocation(): Location
{
600 return $this->location
;
610 public function setSlot(Slot
$slot): Session
{
621 public function getSlot(): Slot
{
628 * @param Application $application
632 public function setApplication(?Application
$application): Session
{
633 $this->application
= $application;
641 * @return Application
643 public function getApplication(): ?Application
{
644 return $this->application
;
650 public function preUpdate(PreUpdateEventArgs
$eventArgs) {
651 //Check that we have a session instance
652 if (($session = $eventArgs->getEntity()) instanceof Session
) {
654 $session->setUpdated(new \
DateTime('now'));
659 * Wether if session is a premium day
661 * Consider as premium a day off for afternoon, the eve for evening and after
662 * Store computed result in premium member for afternoon and evening
664 * @return bool Whether the date is day off or not
666 public function isPremium(): bool {
668 if (empty($date = $this->date
)) {
669 throw new \
LogicException('Property date is empty');
673 if (empty($slot = $this->slot
) || empty($slotTitle = $slot->getTitle())) {
674 throw new \
LogicException('Property slot is empty');
677 //With evening and after slot
678 if ($slotTitle == 'Evening' || $slotTitle == 'After') {
679 //Evening and after session is considered premium when the eve is a day off
680 $date = (clone $date)->add(new \
DateInterval('P1D'));
684 $w = $date->format('w');
686 //Check if weekend day
687 if ($w == 0 || $w == 6) {
688 //With afternoon and evening slot
689 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
691 $this->premium
= true;
694 //Date is weekend day
699 $d = $date->format('d');
702 $m = $date->format('m');
704 //Check if fixed holiday
706 //Check if 1st january
707 ($d == 1 && $m == 1) ||
709 ($d == 1 && $m == 5) ||
711 ($d == 8 && $m == 5) ||
713 ($d == 14 && $m == 7) ||
714 //Check if 15st august
715 ($d == 15 && $m == 8) ||
716 //Check if 1st november
717 ($d == 1 && $m == 11) ||
718 //Check if 11st november
719 ($d == 11 && $m == 11) ||
720 //Check if 25st december
721 ($d == 25 && $m == 12)
723 //With afternoon and evening slot
724 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
726 $this->premium
= true;
729 //Date is a fixed holiday
734 $eastern = $this->getEastern($date->format('Y'));
736 //Check dynamic holidays
738 (clone $eastern)->add(new \
DateInterval('P1D')) == $date ||
739 (clone $eastern)->add(new \
DateInterval('P39D')) == $date ||
740 (clone $eastern)->add(new \
DateInterval('P50D')) == $date
742 //With afternoon and evening slot
743 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
745 $this->premium
= true;
748 //Date is a dynamic holiday
752 //With afternoon and evening slot
753 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
755 $this->premium
= false;
758 //Date is not a holiday and week day
763 * Compute eastern for selected year
765 * @param string $year The eastern year
767 * @return DateTime The eastern date
769 private function getEastern(string $year): \DateTime
{
773 //Check if already computed
774 if (isset($data[$year])) {
775 //Return computed eastern
777 //Check if data is null
778 } elseif (is_null($data)) {
783 $d = (19 * ($year %
19) +
24) %
30;
785 $e = (2 * ($year %
4) +
4 * ($year %
7) +
6 * $d +
5) %
7;
794 } elseif ($d == 29 && $e == 6) {
797 } elseif ($d == 28 && $e == 6) {
802 //Store eastern in data
803 return ($data[$year] = new \
DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));