| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Webfactor\Laravel\OpeningHours\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Webfactor\Laravel\OpeningHours\Entities\OpeningHours; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Webfactor\Laravel\OpeningHours\Entities\OpeningHoursForDay; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Webfactor\Laravel\OpeningHours\Entities\TimeRange; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Webfactor\Laravel\OpeningHours\Exceptions\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Webfactor\Laravel\OpeningHours\Models\DayOpenTimeRange; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | trait OpeningHoursAttribute | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 | 6 |  |     public function getOpeningHoursAttribute() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 | 6 |  |         if (!key_exists('opening_hours', $this->attributes) || !$this->attributes['opening_hours']) { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 | 4 |  |             $hours = $this->dayOpenTimeRanges | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |                 ->groupBy('day')->map(function ($day) { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 2 |  |                     return $day->map(function (DayOpenTimeRange $range) { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 | 2 |  |                         return $range->start.'-'.$range->end; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 | 2 |  |                     }); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 4 |  |                 }); | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 4 |  |             $this->attributes['opening_hours'] = OpeningHours::create($hours->toArray()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 6 |  |         return $this->attributes['opening_hours']; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 14 |  |     public function setOpeningHoursAttribute($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         // clear previous open times | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 14 |  |         $this->attributes['opening_hours'] = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 14 |  |         $this->dayOpenTimeRanges()->delete(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 14 |  |         if ($data == null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 2 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 12 |  |         if ($data instanceof OpeningHours) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |             $this->applyOpeningHours($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 11 |  |         if (is_array($data) || is_object($data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 11 |  |             $this->applyOpeningHours(OpeningHours::create((array) $data)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 11 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         throw new Exception("Invalid argument `{$data}` applied to opening hours attribute."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 12 |  |     private function applyOpeningHours(OpeningHours $openingHours) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 12 |  |         $entries = $this->parseOpeningHoursForDB($openingHours); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 12 |  |         $this->dayOpenTimeRanges()->saveMany($entries); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 12 |  |         $this->attributes['opening_hours'] = $openingHours; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 12 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     private function parseOpeningHoursForDB(OpeningHours $openingHours) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $ranges = $openingHours->flatMap(function (OpeningHoursForDay $openingHoursForDay, string $day) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 12 |  |             return $openingHoursForDay->map(function (TimeRange $timeRange) use ($day) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 12 |  |                     'day'   => $day, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 12 |  |                     'start' => $timeRange->start(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 12 |  |                     'end'   => $timeRange->end() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 12 |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 12 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 12 |  |         return collect($ranges)->map(function ($range) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 12 |  |             return new DayOpenTimeRange($range); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 12 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |  | 
            
                        
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.