| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace JsonFieldCast\Json; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Carbon\Carbon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Carbon\Exceptions\InvalidFormatException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | trait HasDateAttributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 | 2 |  |     public function setDateAttribute(string $key, ?\DateTimeInterface $datetime = null, string $format = 'Y-m-d H:i:s'): static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 | 2 |  |         return $this->setAttribute($key, $datetime?->format($format)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 2 |  |     public function setDate(string $key, ?\DateTimeInterface $datetime = null, string $format = 'Y-m-d H:i:s'): static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 2 |  |         return $this->setDateAttribute($key, $datetime, $format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 2 |  |     public function setNow(string $key, string $format = 'Y-m-d H:i:s'): static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 2 |  |         return $this->setDateAttribute($key, Carbon::now()->setTimezone(config('app.timezone')), $format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 6 |  |     public function getDateAttribute(string $key, ?Carbon $default = null): ?Carbon | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 6 |  |         $value = $this->getAttribute($key); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 6 |  |         if (is_string($value) && !empty($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 6 |  |                 return Carbon::parse($value)->setTimezone(config('app.timezone')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 2 |  |             } catch (InvalidFormatException) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 // Fail silently and return default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 4 |  |         return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Trying to get date from one of passed formats. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param string|array $formats | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param Carbon|null $default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return Carbon|null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 12 |  |     public function getDateTimeFromFormats(string $key, string|array $formats = 'Y-m-d H:i:s', ?Carbon $default = null): ?Carbon | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 12 |  |         $value = $this->getAttribute($key); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 12 |  |         if (is_string($value) && !empty($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 12 |  |             $formats       = Arr::wrap($formats); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 12 |  |             $lastException = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 12 |  |             foreach ($formats as $format) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 try { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 12 |  |                     return Carbon::createFromFormat($format, $value)->setTimezone(config('app.timezone')); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 8 |  |                 } catch (InvalidFormatException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 8 |  |                     $lastException = $e; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 6 |  |             if ($lastException instanceof InvalidFormatException) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 6 |  |                 throw $lastException; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 4 |  |         return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 4 |  |     public function getDateTimeFromFormat(string $key, string $format = 'Y-m-d H:i:s', ?Carbon $default = null): ?Carbon | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 4 |  |         return $this->getDateTimeFromFormats($key, $format, $default); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  |