| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace BusinessDays; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Class Calculator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package BusinessDays | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class Calculator { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     const MONDAY    = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     const TUESDAY   = 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     const WEDNESDAY = 3; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     const THURSDAY  = 4; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     const FRIDAY    = 5; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     const SATURDAY  = 6; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     const SUNDAY    = 7; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     const WEEK_DAY_FORMAT = 'N'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     const HOLIDAY_FORMAT  = 'm-d'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     const FREE_DAY_FORMAT = 'Y-m-d'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var \DateTime[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $holidays = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var \DateTime[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $freeDays = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** @var int[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $freeWeekDays = array(self::SATURDAY, self::SUNDAY); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param \DateTime[] $holidays Array of holidays that repeats each year. (Only month and date is used to match). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function setHolidays(array $holidays) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->holidays = $holidays; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return \DateTime[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private function getHolidays() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         return $this->holidays; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param \DateTime[] $freeDays Array of free days that dose not repeat. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function setFreeDays(array $freeDays) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->freeDays = $freeDays; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return \DateTime[] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     private function getFreeDays() { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         return $this->freeDays; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param int[] $freeWeekDays Array of days of the week which are not business days. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |     public function setFreeWeekDays(array $freeWeekDays) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         $this->freeWeekDays = $freeWeekDays; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return int[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |     private function getFreeWeekDays() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |         if (count($this->freeWeekDays) >= 7) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |             throw new \InvalidArgumentException('Too many non business days provided'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return $this->freeWeekDays; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param int $x | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return $x | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |     private function determineSign($x) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |     	 return $x > 0 ? 1 : -1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param int $howManyDays | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @return $newBusinessDate | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |     public function addBusinessDays(\DateTime $date, $amount) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |         $dates = $this->getBusinessDays($date, $amount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return $dates[array_key_last($dates)]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param datetime $startDate | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @param int $howManyDays | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return array $businessDates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |     public function getBusinessDays(\DateTime $date, $amount) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |         if ($amount === 0 || is_nan($amount)) { return array($date); } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |         $dates = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |         $sign = $this->determineSign($amount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |         $absIncrement = abs($amount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |         $newdate = clone $date; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |         $iterator = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |         while ($iterator < $absIncrement) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |             $newdate->modify($sign . ' day'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |             if ($this->isBusinessDay($newdate)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 $dates[] = clone $newdate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 $iterator++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         return $dates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @param \DateTime $date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |     public function isBusinessDay(\DateTime $date) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |         if ($this->isFreeWeekDayDay($date) || $this->isHoliday($date) || $this->isFreeDay($date)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @param \DateTime $date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 1 |  |     public function isFreeWeekDayDay(\DateTime $date) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 1 |  |         $currentWeekDay = (int)$date->format(self::WEEK_DAY_FORMAT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 1 |  |         if (in_array($currentWeekDay, $this->getFreeWeekDays())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param \DateTime $date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     public function isHoliday(\DateTime $date) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $holidayFormatValue = $date->format(self::HOLIDAY_FORMAT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         foreach ($this->getHolidays() as $holiday) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             if ($holidayFormatValue == $holiday->format(self::HOLIDAY_FORMAT)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @param \DateTime $date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     public function isFreeDay(\DateTime $date) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         $freeDayFormatValue = $date->format(self::FREE_DAY_FORMAT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         foreach ($this->getFreeDays() as $freeDay) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             if ($freeDayFormatValue == $freeDay->format(self::FREE_DAY_FORMAT)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 177 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 178 |  |  |  | 
            
                        
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.