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; 
 132          * @var ArrayCollection 
 134         private $applications; 
 139         public function __construct() { 
 142                 $this->length 
= null; 
 144                 $this->rainfall 
= null; 
 145                 $this->rainrisk 
= null; 
 146                 $this->realfeel 
= null; 
 147                 $this->realfeelmin 
= null; 
 148                 $this->realfeelmax 
= null; 
 149                 $this->temperature 
= null; 
 150                 $this->temperaturemin 
= null; 
 151                 $this->temperaturemax 
= null; 
 152                 $this->locked 
= null; 
 153                 $this->premium 
= null; 
 154                 $this->applications 
= new ArrayCollection(); 
 162         public function getId(): int { 
 169          * @param \DateTime $date 
 173         public function setDate(\DateTime 
$date): Session 
{ 
 184         public function getDate(): \DateTime 
{ 
 191          * @param \DateTime $begin 
 195         public function setBegin(?\DateTime 
$begin): Session 
{ 
 196                 $this->begin 
= $begin; 
 206         public function getBegin(): ?\DateTime 
{ 
 215         public function getStart(): \DateTime 
{ 
 217                 if ($this->start 
!== null) { 
 222                 $this->start 
= clone $this->date
; 
 224                 //Check if after slot 
 225                 //XXX: id=4 <=> title=After 
 226                 if ($this->slot
->getId() == 4) { 
 228                         $this->start
->add(new \
DateInterval('P1D')); 
 232                 if ($this->begin 
!== null) { 
 234                         $this->start
->setTime(intval($this->begin
->format('H')), intval($this->begin
->format('i')), intval($this->begin
->format('s'))); 
 244          * @param \DateTime $length 
 248         public function setLength(?\DateTime 
$length): Session 
{ 
 249                 $this->length 
= $length; 
 259         public function getLength(): ?\DateTime 
{ 
 260                 return $this->length
; 
 268         public function getStop(): \DateTime 
{ 
 270                 if ($this->stop 
!== null) { 
 275                 $this->stop 
= clone $this->getStart(); 
 278                 if ($this->length 
!== null) { 
 280                         $this->stop
->add(new \
DateInterval('PT'.$this->length
->format('H').'H'.$this->length
->format('i').'M'.$this->length
->format('s').'S')); 
 290          * @param boolean $premium 
 294         public function setPremium(bool $premium): Session 
{ 
 295                 $this->premium 
= $premium; 
 305         public function getPremium(): bool { 
 306                 return $this->premium
; 
 312          * @param float $rainfall 
 316         public function setRainfall(?float $rainfall): Session 
{ 
 317                 $this->rainfall 
= $rainfall; 
 327         public function getRainfall(): ?float { 
 328                 return $this->rainfall
; 
 334          * @param float $rainrisk 
 338         public function setRainrisk(?float $rainrisk): Session 
{ 
 339                 $this->rainrisk 
= $rainrisk; 
 349         public function getRainrisk(): ?float { 
 350                 return $this->rainrisk
; 
 356          * @param float $realfeel 
 360         public function setRealfeel(?float $realfeel): Session 
{ 
 361                 $this->realfeel 
= $realfeel; 
 371         public function getRealfeel(): ?float { 
 372                 return $this->realfeel
; 
 378          * @param float $realfeelmin 
 382         public function setRealfeelmin(?float $realfeelmin): Session 
{ 
 383                 $this->realfeelmin 
= $realfeelmin; 
 393         public function getRealfeelmin(): ?float { 
 394                 return $this->realfeelmin
; 
 400          * @param float $realfeelmax 
 404         public function setRealfeelmax(?float $realfeelmax): Session 
{ 
 405                 $this->realfeelmax 
= $realfeelmax; 
 415         public function getRealfeelmax(): ?float { 
 416                 return $this->realfeelmax
; 
 422          * @param float $temperature 
 426         public function setTemperature(?float $temperature): Session 
{ 
 427                 $this->temperature 
= $temperature; 
 437         public function getTemperature(): ?float { 
 438                 return $this->temperature
; 
 444          * @param float $temperaturemin 
 448         public function setTemperaturemin(?float $temperaturemin): Session 
{ 
 449                 $this->temperaturemin 
= $temperaturemin; 
 459         public function getTemperaturemin(): ?float { 
 460                 return $this->temperaturemin
; 
 466          * @param float $temperaturemax 
 470         public function setTemperaturemax(?float $temperaturemax): Session 
{ 
 471                 $this->temperaturemax 
= $temperaturemax; 
 481         public function getTemperaturemax(): ?float { 
 482                 return $this->temperaturemax
; 
 488          * @param \DateTime $locked 
 492         public function setLocked(?\DateTime 
$locked): Session 
{ 
 493                 $this->locked 
= $locked; 
 503         public function getLocked(): ?\DateTime 
{ 
 504                 return $this->locked
; 
 510          * @param \DateTime $created 
 514         public function setCreated(\DateTime 
$created): Session 
{ 
 515                 $this->created 
= $created; 
 525         public function getCreated(): \DateTime 
{ 
 526                 return $this->created
; 
 532          * @param \DateTime $updated 
 536         public function setUpdated(\DateTime 
$updated): Session 
{ 
 537                 $this->updated 
= $updated; 
 547         public function getUpdated(): \DateTime 
{ 
 548                 return $this->updated
; 
 554          * @param Application $application 
 558         public function addApplication(Application 
$application): Session 
{ 
 559                 $this->applications
[] = $application; 
 567          * @param Application $application 
 569         public function removeApplication(Application 
$application): bool { 
 570                 return $this->applications
->removeElement($application); 
 576          * @return ArrayCollection 
 578         public function getApplications(): ArrayCollection 
{ 
 579                 return $this->applications
; 
 585          * @param Dance $dance 
 589         public function setDance(Dance 
$dance): Session 
{ 
 590                 $this->dance 
= $dance; 
 600         public function getDance(): Dance 
{ 
 607          * @param Location $location 
 611         public function setLocation(Location 
$location): Session 
{ 
 612                 $this->location 
= $location; 
 622         public function getLocation(): Location 
{ 
 623                 return $this->location
; 
 633         public function setSlot(Slot 
$slot): Session 
{ 
 644         public function getSlot(): Slot 
{ 
 651          * @param Application $application 
 655         public function setApplication(Application 
$application): Session 
{ 
 656                 $this->application 
= $application; 
 664          * @return Application 
 666         public function getApplication(): ?Application 
{ 
 667                 return $this->application
; 
 673         public function preUpdate(PreUpdateEventArgs 
$eventArgs) { 
 674                 //Check that we have a session instance 
 675                 if (($user = $eventArgs->getEntity()) instanceof Session
) { 
 677                         $user->setUpdated(new \
DateTime('now')); 
 682          * Wether if session is a premium day 
 684          * Consider as premium a day off for afternoon, the eve for evening and after 
 685          * Store computed result in premium member for afternoon and evening 
 687          * @return bool Whether the date is day off or not 
 689         public function isPremium(): bool { 
 691                 if (empty($date = $this->date
)) { 
 692                         throw new \
LogicException('Property date is empty'); 
 696                 if (empty($slot = $this->slot
) || empty($slotTitle = $slot->getTitle())) { 
 697                         throw new \
LogicException('Property slot is empty'); 
 700                 //With evening and after slot 
 701                 if ($slotTitle == 'Evening' || $slotTitle == 'After') { 
 702                         //Evening and after session is considered premium when the eve is a day off 
 703                         $date = (clone $date)->add(new \
DateInterval('P1D')); 
 707                 $w = $date->format('w'); 
 709                 //Check if weekend day 
 710                 if ($w == 0 || $w == 6) { 
 711                         //With afternoon and evening slot 
 712                         if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') { 
 714                                 $this->premium 
= true; 
 717                         //Date is weekend day 
 722                 $d = $date->format('d'); 
 725                 $m = $date->format('m'); 
 727                 //Check if fixed holiday 
 729                         //Check if 1st january 
 730                         ($d == 1 && $m == 1) || 
 732                         ($d == 1 && $m == 5) || 
 734                         ($d == 8 && $m == 5) || 
 736                         ($d == 14 && $m == 7) || 
 737                         //Check if 15st august 
 738                         ($d == 15 && $m == 8) || 
 739                         //Check if 1st november 
 740                         ($d == 1 && $m == 11) || 
 741                         //Check if 11st november 
 742                         ($d == 11 && $m == 11) || 
 743                         //Check if 25st december 
 744                         ($d == 25 && $m == 12) 
 746                         //With afternoon and evening slot 
 747                         if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') { 
 749                                 $this->premium 
= true; 
 752                         //Date is a fixed holiday 
 757                 $eastern = $this->getEastern($date->format('Y')); 
 759                 //Check dynamic holidays 
 761                         (clone $eastern)->add(new \
DateInterval('P1D')) == $date || 
 762                         (clone $eastern)->add(new \
DateInterval('P39D')) == $date || 
 763                         (clone $eastern)->add(new \
DateInterval('P50D')) == $date 
 765                         //With afternoon and evening slot 
 766                         if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') { 
 768                                 $this->premium 
= true; 
 771                         //Date is a dynamic holiday 
 775                 //With afternoon and evening slot 
 776                 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') { 
 778                         $this->premium 
= false; 
 781                 //Date is not a holiday and week day 
 786          * Compute eastern for selected year 
 788          * @param string $year The eastern year 
 790          * @return DateTime The eastern date 
 792         private function getEastern(string $year): \DateTime 
{ 
 796                 //Check if already computed 
 797                 if (isset($data[$year])) { 
 798                         //Return computed eastern 
 800                 //Check if data is null 
 801                 } elseif (is_null($data)) { 
 806                 $d = (19 * ($year % 
19) + 
24) % 
30; 
 808                 $e = (2 * ($year % 
4) + 
4 * ($year % 
7) + 
6 * $d + 
5) % 
7; 
 817                 } elseif ($d == 29 && $e == 6) { 
 820                 } elseif ($d == 28 && $e == 6) { 
 825                 //Store eastern in data 
 826                 return ($data[$year] = new \
DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));