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() {
137 $this->length
= null;
139 $this->rainfall
= null;
140 $this->rainrisk
= null;
141 $this->realfeel
= null;
142 $this->realfeelmin
= null;
143 $this->realfeelmax
= null;
144 $this->temperature
= null;
145 $this->temperaturemin
= null;
146 $this->temperaturemax
= null;
147 $this->locked
= null;
148 $this->premium
= null;
149 $this->applications
= new ArrayCollection();
157 public function getId(): int {
164 * @param \DateTime $date
168 public function setDate(\DateTime
$date): Session
{
179 public function getDate(): \DateTime
{
186 * @param \DateTime $begin
190 public function setBegin(?\DateTime
$begin): Session
{
191 $this->begin
= $begin;
201 public function getBegin(): ?\DateTime
{
210 public function getStart(): \DateTime
{
212 if ($this->start
!== null) {
217 $this->start
= clone $this->date
;
219 //Check if after slot
220 //XXX: id=4 <=> title=After
221 if ($this->slot
->getId() == 4) {
223 $this->start
->add(new \
DateInterval('P1D'));
227 if ($this->begin
!== null) {
229 $this->start
->setTime(intval($this->begin
->format('H')), intval($this->begin
->format('i')), intval($this->begin
->format('s')));
239 * @param \DateTime $length
243 public function setLength(?\DateTime
$length): Session
{
244 $this->length
= $length;
254 public function getLength(): ?\DateTime
{
255 return $this->length
;
263 public function getStop(): \DateTime
{
265 if ($this->stop
!== null) {
270 $this->stop
= clone $this->getStart();
273 if ($this->length
!== null) {
275 $this->stop
->add(new \
DateInterval('PT'.$this->length
->format('H').'H'.$this->length
->format('i').'M'.$this->length
->format('s').'S'));
285 * @param boolean $premium
289 public function setPremium(bool $premium): Session
{
290 $this->premium
= $premium;
300 public function getPremium(): bool {
301 return $this->premium
;
307 * @param float $rainfall
311 public function setRainfall(?float $rainfall): Session
{
312 $this->rainfall
= $rainfall;
322 public function getRainfall(): ?float {
323 return $this->rainfall
;
329 * @param float $rainrisk
333 public function setRainrisk(?float $rainrisk): Session
{
334 $this->rainrisk
= $rainrisk;
344 public function getRainrisk(): ?float {
345 return $this->rainrisk
;
351 * @param float $realfeel
355 public function setRealfeel(?float $realfeel): Session
{
356 $this->realfeel
= $realfeel;
366 public function getRealfeel(): ?float {
367 return $this->realfeel
;
373 * @param float $realfeelmin
377 public function setRealfeelmin(?float $realfeelmin): Session
{
378 $this->realfeelmin
= $realfeelmin;
388 public function getRealfeelmin(): ?float {
389 return $this->realfeelmin
;
395 * @param float $realfeelmax
399 public function setRealfeelmax(?float $realfeelmax): Session
{
400 $this->realfeelmax
= $realfeelmax;
410 public function getRealfeelmax(): ?float {
411 return $this->realfeelmax
;
417 * @param float $temperature
421 public function setTemperature(?float $temperature): Session
{
422 $this->temperature
= $temperature;
432 public function getTemperature(): ?float {
433 return $this->temperature
;
439 * @param float $temperaturemin
443 public function setTemperaturemin(?float $temperaturemin): Session
{
444 $this->temperaturemin
= $temperaturemin;
454 public function getTemperaturemin(): ?float {
455 return $this->temperaturemin
;
461 * @param float $temperaturemax
465 public function setTemperaturemax(?float $temperaturemax): Session
{
466 $this->temperaturemax
= $temperaturemax;
476 public function getTemperaturemax(): ?float {
477 return $this->temperaturemax
;
483 * @param \DateTime $locked
487 public function setLocked(?\DateTime
$locked): Session
{
488 $this->locked
= $locked;
498 public function getLocked(): ?\DateTime
{
499 return $this->locked
;
505 * @param \DateTime $created
509 public function setCreated(\DateTime
$created): Session
{
510 $this->created
= $created;
520 public function getCreated(): \DateTime
{
521 return $this->created
;
527 * @param \DateTime $updated
531 public function setUpdated(\DateTime
$updated): Session
{
532 $this->updated
= $updated;
542 public function getUpdated(): \DateTime
{
543 return $this->updated
;
549 * @param Application $application
553 public function addApplication(Application
$application): Session
{
554 $this->applications
[] = $application;
562 * @param Application $application
564 public function removeApplication(Application
$application): bool {
565 return $this->applications
->removeElement($application);
571 * @return ArrayCollection
573 public function getApplications(): ArrayCollection
{
574 return $this->applications
;
580 * @param Location $location
584 public function setLocation(Location
$location): Session
{
585 $this->location
= $location;
595 public function getLocation(): Location
{
596 return $this->location
;
606 public function setSlot(Slot
$slot): Session
{
617 public function getSlot(): Slot
{
624 * @param Application $application
628 public function setApplication(Application
$application): Session
{
629 $this->application
= $application;
637 * @return Application
639 public function getApplication(): ?Application
{
640 return $this->application
;
646 public function preUpdate(PreUpdateEventArgs
$eventArgs) {
647 //Check that we have a session instance
648 if (($session = $eventArgs->getEntity()) instanceof Session
) {
650 $session->setUpdated(new \
DateTime('now'));
655 * Wether if session is a premium day
657 * Consider as premium a day off for afternoon, the eve for evening and after
658 * Store computed result in premium member for afternoon and evening
660 * @return bool Whether the date is day off or not
662 public function isPremium(): bool {
664 if (empty($date = $this->date
)) {
665 throw new \
LogicException('Property date is empty');
669 if (empty($slot = $this->slot
) || empty($slotTitle = $slot->getTitle())) {
670 throw new \
LogicException('Property slot is empty');
673 //With evening and after slot
674 if ($slotTitle == 'Evening' || $slotTitle == 'After') {
675 //Evening and after session is considered premium when the eve is a day off
676 $date = (clone $date)->add(new \
DateInterval('P1D'));
680 $w = $date->format('w');
682 //Check if weekend day
683 if ($w == 0 || $w == 6) {
684 //With afternoon and evening slot
685 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
687 $this->premium
= true;
690 //Date is weekend day
695 $d = $date->format('d');
698 $m = $date->format('m');
700 //Check if fixed holiday
702 //Check if 1st january
703 ($d == 1 && $m == 1) ||
705 ($d == 1 && $m == 5) ||
707 ($d == 8 && $m == 5) ||
709 ($d == 14 && $m == 7) ||
710 //Check if 15st august
711 ($d == 15 && $m == 8) ||
712 //Check if 1st november
713 ($d == 1 && $m == 11) ||
714 //Check if 11st november
715 ($d == 11 && $m == 11) ||
716 //Check if 25st december
717 ($d == 25 && $m == 12)
719 //With afternoon and evening slot
720 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
722 $this->premium
= true;
725 //Date is a fixed holiday
730 $eastern = $this->getEastern($date->format('Y'));
732 //Check dynamic holidays
734 (clone $eastern)->add(new \
DateInterval('P1D')) == $date ||
735 (clone $eastern)->add(new \
DateInterval('P39D')) == $date ||
736 (clone $eastern)->add(new \
DateInterval('P50D')) == $date
738 //With afternoon and evening slot
739 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
741 $this->premium
= true;
744 //Date is a dynamic holiday
748 //With afternoon and evening slot
749 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
751 $this->premium
= false;
754 //Date is not a holiday and week day
759 * Compute eastern for selected year
761 * @param string $year The eastern year
763 * @return DateTime The eastern date
765 private function getEastern(string $year): \DateTime
{
769 //Check if already computed
770 if (isset($data[$year])) {
771 //Return computed eastern
773 //Check if data is null
774 } elseif (is_null($data)) {
779 $d = (19 * ($year %
19) +
24) %
30;
781 $e = (2 * ($year %
4) +
4 * ($year %
7) +
6 * $d +
5) %
7;
790 } elseif ($d == 29 && $e == 6) {
793 } elseif ($d == 28 && $e == 6) {
798 //Store eastern in data
799 return ($data[$year] = new \
DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));